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
  • Loading branch information
arturovt committed Feb 2, 2022
1 parent f40aad9 commit 33d9aa6
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion components/icon/icon.directive.ts
Expand Up @@ -3,17 +3,20 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { isPlatformServer } from '@angular/common';
import {
AfterContentChecked,
ChangeDetectorRef,
Directive,
ElementRef,
Inject,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Optional,
PLATFORM_ID,
Renderer2,
SimpleChanges
} from '@angular/core';
Expand Down Expand Up @@ -80,7 +83,8 @@ export class NzIconDirective extends IconDirective implements OnInit, OnChanges,
elementRef: ElementRef,
public iconService: NzIconService,
public renderer: Renderer2,
@Optional() iconPatch: NzIconPatchService
@Optional() iconPatch: NzIconPatchService,
@Inject(PLATFORM_ID) private readonly platformId: string
) {
super(iconService, elementRef, renderer);

Expand Down Expand Up @@ -135,6 +139,14 @@ export class NzIconDirective extends IconDirective implements OnInit, OnChanges,
private changeIcon2(): void {
this.setClassName();

// First of all, we can't render the SVG on the server and send it to the browser for now,
// so the browser doesn't have to re-render it. Second, the `IconDirective` will still re-load
// this SVG, so we'll have an unnecessary HTTP request on the server-side. There's no sense to
// call `_changeIcon()` when running the code on the Node.js side.
if (isPlatformServer(this.platformId)) {
return;
}

// We don't need to re-enter the Angular zone for adding classes or attributes through the renderer.
this.ngZone.runOutsideAngular(() => {
from(this._changeIcon())
Expand Down

0 comments on commit 33d9aa6

Please sign in to comment.