Reputation: 315
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.
Upvotes: 2
Views: 1007
Reputation: 693
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
Upvotes: 0