Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(module:icon): do not try to load SVG on the Node.js side since it will throw an error #7290

Merged
merged 1 commit into from Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -25,7 +25,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