Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defaultHeaders revision #11

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/middleware/default-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ export function defaultHeaders(
): Middleware {
return (request, next) => {
// IMPORTANT: for avoid mutate request, just create new Headers and Request here
const headers = new Headers(defaults);
const headers = new Headers(request.headers);

if (request.headers) {
new Headers(request.headers).forEach((value, key) => {
headers[strategy](key, value);
});
}
/*
Previously, there was a different approach here: headers were created based on "defaults" argument, then headers from the request were added to them.

This was done so that the "default headers" were truly default and were overridden by what was set by the developer in the request itself.

But it didn't work well because browser always had the "Content-Type" header set by default, which always overridden the option that was in the middleware factory arguments.

To fix this, default headers are now added to the request headers
*/
new Headers(defaults).forEach((value, key) => {
headers[strategy](key, value);
});

return next(new Request(request, { headers }));
};
Expand Down
Loading