2

I'm new to Angular and trying to use HTTP interceptor here.

I just wonder whether should I lump multiple interceptors, like setting headers' token, cache-control, content-type in one file, or should I separate it into token.interceptor.ts for token and put other general headers in headers.interceptor.ts.

Does the later have worse performance since it have to call req.clone() one more time just to set headers, or does Angular injection works the other way? Which is the best practice regarding this topic?

Thanks for your opinion.

KradasA4
  • 165
  • 1
  • 2
  • 11

1 Answers1

0

We can divide this question into 2 sub-questions.

I just wonder whether should I lump multiple interceptors, like setting headers' token, cache-control, content-type in one file, or should I separate it into token.interceptor.ts for token and put other general headers in headers.interceptor.ts.

You can set multiple headers in angular httpInterceptor. It does support multiheader functionality.

Does the later have worse performance since it have to call req.clone() one more time just to set headers, or does Angular injection works the other way? Which is the best practice regarding this topic?

If you must alter a request, clone it first and modify the clone before passing it to next.handle().The clone() method's hash argument allows you to mutate specific properties of the request while copying the others. Refer API documentation

Gajanan Kulkarni
  • 697
  • 6
  • 22
  • 1
    In case we would able to configure the header, before the request was created - which way to go: add an interceptor OR configure the header? Is there some performance consideration about `clone`? – minus one May 06 '22 at 05:05