-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix: cookies and immutable headers #13942
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
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: da51d2b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
When making a request via the event.fetch to the same exact domain the svelte app is running on, it fails returning a 500 due to the header being immutable. This only happens when a cookie is set in the hooks before trying to make a request with the event.fetch
b1d6380
to
c9168d0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR but I don't think we should just ignore the error. It would result in worse behavior, like inconsistent states or you don't realize you need to check your logs and are scratching your head why the header is not there
Yeah, I can look more into it. I never figured out why the header is immutable and where it is initially being set. A better fix may be to check and see if the cookies are already in the header. |
Co-authored-by: Ben McCann <[email protected]>
From my testing, all sub requests have immutable errors and will fail if cookies are added to them. This will add a check to skip adding the cookies if it is a sub request.
Instead of the try catch, I am now checking to see if the request is a sub request. If it is a sub request, I am skipping the attempt to add cookies as the header for sub requests is immutable. Is it supposed to be that the header is immutable for sub requests? If so, why is that? |
I haven't looked closely enough to see why whether this request has immutable headers depends on whether it's a sub-request, but one thing we're doing elsewhere in the codebase already is to create a new I think that would work here. There doesn't seem to be anything in the code being touched here that requires you use the same |
Use a new response object to avoid the headers being immutable when adding new cookies.
Looked more at other code and found another place where a new response object is created. I based these changes on that to limit the amount of code being changed.
I updated it to use a new object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems surprising to me that a new Response
would have mutable headers whereas the original one has immutable headers. If we could avoid making the headers immutable in the first place that seems like a better solution to me than cloning the object. I'm pretty sure we're the ones who create the Response
, so it feels like we should just fix it instead of adding overhead that will make every request slower to serve
Co-authored-by: Ben McCann <[email protected]>
I have now found why the header is immutable. This is happening when the resolve method makes an http request with fetch. The response object that is returned is directly from fetch which will have an immutable header. Would it be good to take that response and return a copy of it with an immutable header instead of the original? |
return await fetch(request); | ||
const fetchResponse = await fetch(request); | ||
|
||
// the header for the response needs to be mutable, so we need to clone it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you expand the comment to explain why they need to be mutated?
// the header for the response needs to be mutable, so we need to clone it | |
// the headers for the response needs to be mutable, so we need to clone it |
Thanks. We should probably have a test for this |
Fixes: #13857
When making a request via the event.fetch to the same exact domain the svelte app is running on, it fails returning a 500 due to the header being immutable.
This only happens when a cookie is set in the hooks before trying to make a request with the event.fetch
This fix just adds a try catch around the code that adds the cookie to the header so that it still attempts to make the request even if it fails to add the cookie.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.Edits