components icon indicating copy to clipboard operation
components copied to clipboard

Error when retrieving icon from registry in server side rendering

Open akanass opened this issue 8 years ago • 24 comments

Bug, feature request, or proposal:

When we register some icons in MatIconRegistry and access to them in SSR, we have some errors and icons aren't displayed.

What is the expected behavior?

Display icons and not have error in the console

What is the current behavior?

Icon aren't displayed and we have error in the console

What are the steps to reproduce?

Create a component and register an icon in MatIconRegistry:

import {Component, OnInit} from '@angular/core';
import {MatIconRegistry} from '@angular/material';
import {DomSanitizer} from '@angular/platform-browser';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

  constructor(private _matIconRegistry: MatIconRegistry, private _domSanitizer: DomSanitizer) {
    this._matIconRegistry.addSvgIcon('loader', this._domSanitizer.bypassSecurityTrustResourceUrl('assets/Icons/loader-default.svg'));
  }

  ngOnInit() {
  }

}
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    // Make AppModule compatible with Universal
    BrowserModule.withServerTransition({ appId: 'universal-app' }),
    MatIconModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {
}

Access to it in server side rendering:

<mat-icon svgIcon="loader"></mat-icon>

In the console you'll have this message:

Error retrieving icon:
ERROR [Error]

What is the use-case or motivation for changing an existing behavior?

MatIconRegistry should work in SSR

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

"dependencies": {
    "@angular/animations": "^5.2.2",
    "@angular/cdk": "^5.1.1",
    "@angular/common": "^5.2.2",
    "@angular/compiler": "^5.2.2",
    "@angular/core": "^5.2.2",
    "@angular/forms": "^5.2.2",
    "@angular/http": "^5.2.2",
    "@angular/material": "^5.1.1",
    "@angular/platform-browser": "^5.2.2",
    "@angular/platform-browser-dynamic": "^5.2.2",
    "@angular/platform-server": "^5.2.2",
    "@angular/router": "^5.2.2",
    "@hapiness/config": "^1.1.1",
    "@hapiness/core": "^1.3.0",
    "@hapiness/ng-universal": "^5.2.2",
    "@hapiness/ng-universal-transfer-http": "^5.2.2",
    "@nguniversal/module-map-ngfactory-loader": "^5.0.0-beta.5",
    "core-js": "^2.5.1",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.20"
  },
  "devDependencies": {
    "@angular/cli": "^1.6.6",
    "@angular/compiler-cli": "^5.2.2",
    "@angular/language-service": "^5.2.2",
    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "^9.4.0",
    "codelyzer": "^4.1.0",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-loader": "^3.3.1",
    "ts-node": "^4.0.1",
    "tslint": "^5.9.1",
    "typescript": "^2.6.2"
  }

akanass avatar Feb 01 '18 07:02 akanass

same issue, with addSvgIconSet:

    const host = isPlatformServer(this.platformId) ? `https://localhost:4000` : '.';
    const loadFrom = `${host}/assets/symbol-sprite.svg`;
    iconRegistry.addSvgIconSet(sanitizer.bypassSecurityTrustResourceUrl(loadFrom));

using the nguniversal/*-engine packages

  • the client-side renders
  • SSR fails with this error
Error: Loading icon set URL: https://localhost:4000/assets/symbol-sprite.svg failed: 
Http failure response for https://localhost:4000/assets/symbol-sprite.svg: 0 Unknown Error

btw - requesting the svg-sprite file manually with postman does return the sprite

codeuniquely avatar Jul 14 '21 06:07 codeuniquely

Even this example isn't working.

https://stackblitz.com/angular/ygonjmkebrr

Seconding the concern something is up with mat-icons and svgs, even the angular material examples are broken

https://stackblitz.com/angular/yjprlknbxmgk?file=src%2Fapp%2Ficon-svg-example.html

Assets not properly configured in angular.json. Move assets directory to src then update angular.json and set "assets": ["src/assets"]

I'm also getting the error <svg> tag not found at MatIconRegistry... with just a regular non-SSR Angular project. Like @aparroccini said, even the example in the docs has the same error. Anyone know how to fix?

Make sure of the following:

  • Interceptor that modifies requests (make exception for .svg assets requests)
  • If you use angular-in-memory-web-api make sure to set passThruUnknownUrl: true
  • Assets files exist in dist after running the build in the same path you are trying to register them (you may need to change angular.json)

AliYusuf95 avatar Sep 29 '21 13:09 AliYusuf95

I ran into this issue as well and couldn't solve my problem until I realized that I had an interceptor that was changing all outbound request headers to application/json. I fixed it by adding some extra logic to see if I was requesting a resource from the assets folder to fix it. If any of you are still having this issue you might look at that.

image

This was my issue (see error)! We were accepting only application/json in accept Request Header.

Vitruvius21 avatar Dec 27 '21 11:12 Vitruvius21

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.