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

fix: set cookies when throwing redirect in handle #8648

Merged
merged 6 commits into from Jan 24, 2023
Merged

fix: set cookies when throwing redirect in handle #8648

merged 6 commits into from Jan 24, 2023

Conversation

eltigerchino
Copy link
Member

@eltigerchino eltigerchino commented Jan 21, 2023

fixes #8643

Adds headers and cookies to the response when using throw redirect in handle so that it behaves similarly to returning a new redirect Response in handle.

I'm sure there's a more elegant approach to this fix and the test, but I can't quite figure it out (this is my first time writing a test). Would appreciate any suggested changes.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@changeset-bot
Copy link

changeset-bot bot commented Jan 21, 2023

🦋 Changeset detected

Latest commit: c60d9b3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

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

@eltigerchino eltigerchino changed the title fix: set headers for throw redirect() response in handle fix: set headers when throwing redirect in handle Jan 21, 2023
Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I think we need to apply the same logic to the case where it's a __data.json request (the if branch above the changed code)

@@ -296,7 +300,15 @@ export async function respond(request, options, manifest, state) {
if (is_data_request) {
return redirect_json_response(e);
} else {
return redirect_response(e.status, e.location);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can keep the redirect_response function and add the headers to it.

const response = redirect_response(..);
add_cookies_to_headers(..);
return response;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to do it this way originally but wasn't sure if only cookies should be added or to include the headers from setHeaders() too.

I also didn't want a location header from setHeaders to overwrite the thrown redirect location.

Maybe I can extend the redirect_response() helper to optionally accept a headers option?
I noticed there were similar redirect responses in the file not using the helper just so that it could add other headers to it.

@dummdidumm
Copy link
Member

Thank you!

@dummdidumm dummdidumm merged commit 3a1ec29 into sveltejs:master Jan 24, 2023
@github-actions github-actions bot mentioned this pull request Jan 24, 2023
@eltigerchino eltigerchino changed the title fix: set headers when throwing redirect in handle fix: set cookies when throwing redirect in handle Jan 24, 2023
@eltigerchino
Copy link
Member Author

eltigerchino commented Jan 24, 2023

Thanks for the guidance!
Note that the current implementation only appends the set-cookie header and not other headers. But I forgot to change the changeset notes. My bad

@ivanhofer ivanhofer mentioned this pull request Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cookies API doesn't work with redirect helper in hooks
2 participants