Skip to content

Commit

Permalink
fix(module:icon): do not try to load SVG on the Node.js side since it…
Browse files Browse the repository at this point in the history
… will throw an error (#7290)

close #7240
  • Loading branch information
arturovt committed Mar 3, 2022
1 parent b7433a9 commit fe0484f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
32 changes: 18 additions & 14 deletions components/icon/icon.directive.ts
Expand Up @@ -22,6 +22,7 @@ import { takeUntil } from 'rxjs/operators';

import { IconDirective, ThemeType } from '@ant-design/icons-angular';

import { warn } from 'ng-zorro-antd/core/logger';
import { BooleanInput } from 'ng-zorro-antd/core/types';
import { InputBoolean } from 'ng-zorro-antd/core/util';

Expand Down Expand Up @@ -78,8 +79,8 @@ export class NzIconDirective extends IconDirective implements OnInit, OnChanges,
private readonly ngZone: NgZone,
private readonly changeDetectorRef: ChangeDetectorRef,
elementRef: ElementRef,
public iconService: NzIconService,
public renderer: Renderer2,
public readonly iconService: NzIconService,
public readonly renderer: Renderer2,
@Optional() iconPatch: NzIconPatchService
) {
super(iconService, elementRef, renderer);
Expand Down Expand Up @@ -139,18 +140,21 @@ export class NzIconDirective extends IconDirective implements OnInit, OnChanges,
this.ngZone.runOutsideAngular(() => {
from(this._changeIcon())
.pipe(takeUntil(this.destroy$))
.subscribe(svgOrRemove => {
// The _changeIcon method would call Renderer to remove the element of the old icon,
// which would call `markElementAsRemoved` eventually,
// so we should call `detectChanges` to tell Angular remove the DOM node.
// #7186
this.changeDetectorRef.detectChanges();

if (svgOrRemove) {
this.setSVGData(svgOrRemove);
this.handleSpin(svgOrRemove);
this.handleRotate(svgOrRemove);
}
.subscribe({
next: svgOrRemove => {
// The _changeIcon method would call Renderer to remove the element of the old icon,
// which would call `markElementAsRemoved` eventually,
// so we should call `detectChanges` to tell Angular remove the DOM node.
// #7186
this.changeDetectorRef.detectChanges();

if (svgOrRemove) {
this.setSVGData(svgOrRemove);
this.handleSpin(svgOrRemove);
this.handleRotate(svgOrRemove);
}
},
error: warn
});
});
}
Expand Down
9 changes: 7 additions & 2 deletions components/icon/icon.service.ts
Expand Up @@ -3,6 +3,7 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { Platform } from '@angular/cdk/platform';
import { DOCUMENT } from '@angular/common';
import { HttpBackend } from '@angular/common/http';
import { Inject, Injectable, InjectionToken, OnDestroy, Optional, RendererFactory2, Self } from '@angular/core';
Expand Down Expand Up @@ -34,6 +35,10 @@ export const DEFAULT_TWOTONE_COLOR = '#1890ff';
export class NzIconService extends IconService implements OnDestroy {
configUpdated$ = new Subject<void>();

protected override get _disableDynamicLoading(): boolean {
return !this.platform.isBrowser;
}

private iconfontCache = new Set<string>();
private subscription: Subscription | null = null;

Expand Down Expand Up @@ -76,14 +81,14 @@ export class NzIconService extends IconService implements OnDestroy {
rendererFactory: RendererFactory2,
sanitizer: DomSanitizer,
protected nzConfigService: NzConfigService,
private platform: Platform,
@Optional() handler: HttpBackend,
@Optional() @Inject(DOCUMENT) _document: NzSafeAny,
@Optional() @Inject(NZ_ICONS) icons?: IconDefinition[]
) {
super(rendererFactory, handler, _document, sanitizer);
super(rendererFactory, handler, _document, sanitizer, [...NZ_ICONS_USED_BY_ZORRO, ...(icons || [])]);

this.onConfigChange();
this.addIcon(...NZ_ICONS_USED_BY_ZORRO, ...(icons || []));
this.configDefaultTwotoneColor();
this.configDefaultTheme();
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"@angular/cdk": "^13.0.1",
"@ant-design/icons-angular": "^13.0.1",
"@ant-design/icons-angular": "^13.1.0",
"date-fns": "^2.16.1",
"ngx-hover-preload": "0.0.3"
},
Expand Down

0 comments on commit fe0484f

Please sign in to comment.