markus
markus

Reputation: 485

Angular universal HttpInterceptor runs twice

i'm currently switching my angular app to an universal ssr app. my last problem is that my custom HttpInterceptor fires twice. i'm checking the http response code of my api calls and if there is an error a popup gets displayed. since i switched to universal i get the same error popup twice.

@Injectable()
export class HttpSetHeaders implements HttpInterceptor {
  constructor(
    @Inject(PLATFORM_ID) private platformId,
    @Inject(LOCAL_STORAGE) private localStorage: any,
    private authService: AuthService,
    private router: Router,
    private notifierService: NotifierService
  ) { }
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    console.log(isPlatformServer(this.platformId))
    ......
}

after every HTTPClient call i see the console log output twice.

i've importet the TransferHttpCacheModule in my app.module and TransferHttpCacheModule in my app.server.module

thanks markus

Upvotes: 3

Views: 1683

Answers (1)

axelrotter
axelrotter

Reputation: 724

According to https://angular.io/api/common/http/HttpInterceptor

To use the same instance of HttpInterceptors for the entire app, import the HttpClientModule only in your AppModule, and add the interceptors to the root application injector . If you import HttpClientModule multiple times across different modules (for example, in lazy loading modules), each import creates a new copy of the HttpClientModule, which overwrites the interceptors provided in the root module.

Let me know if this solves your problem. May be that you just instantiate the HttpInterceptor more than once.

Upvotes: 6

Related Questions