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

Cookies from middleware don't get set on custom 404 page #7880

Closed
1 task
xriter opened this issue Jul 31, 2023 · 6 comments
Closed
1 task

Cookies from middleware don't get set on custom 404 page #7880

xriter opened this issue Jul 31, 2023 · 6 comments
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) needs response Issue needs response from OP

Comments

@xriter
Copy link

xriter commented Jul 31, 2023

What version of astro are you using?

2.9.6

Are you using an SSR adapter? If so, which one?

Node

What package manager are you using?

npm

What operating system are you using?

Mac

What browser are you using?

Safari

Describe the Bug

When cookies are being set in middleware (using Astro.cookies.set), but the page request resolves to a (custom) 404 page, the cookies don't get set/send.

What's the expected result?

Even when a page resolves to 404, cookies must be able to be set from middleware.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-bpkd5t?file=src%2Fmiddleware.ts

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jul 31, 2023
@ematipico
Copy link
Member

When cookies are being set in middleware (using Astro.cookies.set), but the page request resolves to a (custom) 404 page, the cookies don't get set/send.

You shared a reproduction, but you didn't specify how we can reproduce the issue. I opened the link but I am really sure what I am supposed to.

@ematipico ematipico added needs response Issue needs response from OP and removed needs triage Issue needs to be triaged labels Jul 31, 2023
@ematipico
Copy link
Member

ematipico commented Jul 31, 2023

FYI, when calling next(), you must return, e.g. return next(). Not doing so will cause issues.

@ematipico
Copy link
Member

I just tried to read the cookie from the 404, and it's correctly set.

@xriter
Copy link
Author

xriter commented Jul 31, 2023

@ematipico Thank you for the tip for adding return. However, this does not solve the issue.

I am sorry if the repro wasn't self explanatory enough.
First open the link, and then navigate to the index.astro file. In the preview window on the right it'll show a link to /doesntexist. Click that. In the inspector you'll be able to see that the cookie that middleware.ts tries to set, never makes it into the response (see screenshot).

Scherm­afbeelding 2023-07-31 om 18 56 07

@natemoo-re natemoo-re added the - P3: minor bug An edge case that only affects very specific usage (priority) label Aug 1, 2023
@natemoo-re
Copy link
Member

Looks like this has been fixed in Astro 2.9.7 by #7754. Please upgrade astro and any integrations to the latest version.

Please see minimal reproduction.

@xriter
Copy link
Author

xriter commented Aug 1, 2023

Looks like this has been fixed in Astro 2.9.7 by #7754. Please upgrade astro and any integrations to the latest version.

Please see minimal reproduction.

The issue still exists on 2.9.7.

Looking at the minimal repro, the cookies are indeed added to the Astro.cookies object (so that's why you can read them), BUT they never make it into the actual http response, and thus they are not being set in the browser.

I think this issue needs to be reopened.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) needs response Issue needs response from OP
Projects
None yet
Development

No branches or pull requests

3 participants