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

Endpoint Response.redirect header modification #9871

Closed
1 task done
friedemannsommer opened this issue Jan 29, 2024 · 2 comments · Fixed by #9876
Closed
1 task done

Endpoint Response.redirect header modification #9871

friedemannsommer opened this issue Jan 29, 2024 · 2 comments · Fixed by #9876
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) regression

Comments

@friedemannsommer
Copy link
Contributor

Astro Info

Astro                    v4.2.6
Node                     v20.11.0
System                   Linux (x64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/node
Integrations             @astrojs/tailwind
                         @astrojs/react

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

When returning Response.redirect from an endpoint, the renderEndpoint function tries to set an HTTP response header on the Response object of the handler. This will throw a TypeError: immutable and return an HTTP 500 error to the client.

Note that new Response(..) and Response.json are not affected by this issue, even though they technically should be 🤔.


This is a relatively new change introduced by 075706f26d2e11e66ef8b52288d07e3c0fa97eb1.
These changes are included in Astro from version 4.2.4 and later.

What's the expected result?

The client should receive a HTTP 30X response with a Location header.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/withastro-astro-2fktbw?file=README.md

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 Jan 29, 2024
@florian-lefebvre florian-lefebvre added - P3: minor bug An edge case that only affects very specific usage (priority) regression and removed needs triage Issue needs to be triaged labels Jan 29, 2024
@lilnasy lilnasy assigned lilnasy and unassigned lilnasy Jan 29, 2024
@lilnasy
Copy link
Contributor

lilnasy commented Jan 29, 2024

Thanks for looking into it, this is all spot on!

I didn't know Response.redirect would have immutable headers, but it would make sense. The directive would have to be something other than a header.

@lilnasy
Copy link
Contributor

lilnasy commented Jan 29, 2024

I have written a test based on your repro here: main...lilnasy:astro:fix/9871

Feel free to take that and attempt a fix. I would guess putting the .set() in a try-catch block helps.

friedemannsommer added a commit to friedemannsommer/astro that referenced this issue Jan 30, 2024
@lilnasy lilnasy linked a pull request Jan 30, 2024 that will close this issue
lilnasy pushed a commit that referenced this issue Jan 30, 2024
* add test

* added runtime endpoint test for `new Response`

* added `try..catch` for reroute directive handling
Fixes #9871

* added changeset

* replaced `try..catch` with HTTP status code check
based on the suggestion of @lilnasy

* updated changeset description

* added more tests for the endpoint reroute header

* fixed grammar  in `renderEndpoint` comment

* updated endpoint tests to check for the reroute directive header in lower-case

* updated changeset description
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) regression
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants