-
Notifications
You must be signed in to change notification settings - Fork 26.1k
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 followed by redirect()
#49965
Merged
kodiakhq
merged 13 commits into
canary
from
styfle/next-1120-draftmode-does-not-set-cookie
May 19, 2023
Merged
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ec57c9e
add failing test
styfle 2a06e4d
fix it
styfle 2dac7d1
rename to `getMutableCookieHeaders()`
styfle d1f1e91
remove console.log()
styfle 0c9449f
Rename helpers
styfle 73eb1b6
Merge branch 'canary' into styfle/next-1120-draftmode-does-not-set-co…
styfle 974643a
Change 302 to 307
styfle 02b0d10
Fix status code in test
styfle e39c2da
Merge branch 'canary' into styfle/next-1120-draftmode-does-not-set-co…
styfle a37769a
Merge branch 'canary' into styfle/next-1120-draftmode-does-not-set-co…
styfle 308143a
fix: simplified cookie access
wyattjoh 2998976
Merge branch 'canary' into styfle/next-1120-draftmode-does-not-set-co…
styfle 9fc31a1
Merge branch 'canary' into styfle/next-1120-draftmode-does-not-set-co…
ijjk File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...ages/next/src/server/future/route-modules/app-route/helpers/get-mutable-cookie-headers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { HeadersAdapter } from '../../../../web/spec-extension/adapters/headers' | ||
import { ResponseCookies } from '../../../../web/spec-extension/cookies' | ||
import { SYMBOL_MODIFY_COOKIE_VALUES } from '../../../../web/spec-extension/adapters/request-cookies' | ||
|
||
export function getMutableCookieHeaders( | ||
headers: Headers, | ||
mutableCookies: ResponseCookies | ||
): Headers { | ||
const modifiedCookieValues = (mutableCookies as any)[ | ||
SYMBOL_MODIFY_COOKIE_VALUES | ||
] as NonNullable<ReturnType<InstanceType<typeof ResponseCookies>['get']>>[] | ||
if (modifiedCookieValues.length) { | ||
// Return a new response that extends the response with | ||
// the modified cookies as fallbacks. `res`' cookies | ||
// will still take precedence. | ||
const resCookies = new ResponseCookies(HeadersAdapter.from(headers)) | ||
const returnedCookies = resCookies.getAll() | ||
|
||
// Set the modified cookies as fallbacks. | ||
for (const cookie of modifiedCookieValues) { | ||
resCookies.set(cookie) | ||
} | ||
// Set the original cookies as the final values. | ||
for (const cookie of returnedCookies) { | ||
resCookies.set(cookie) | ||
} | ||
|
||
const responseHeaders = new Headers({}) | ||
// Set all the headers except for the cookies. | ||
headers.forEach((value, key) => { | ||
if (key.toLowerCase() !== 'set-cookie') { | ||
responseHeaders.append(key, value) | ||
} | ||
}) | ||
// Set the final cookies, need to append cookies one | ||
// at a time otherwise it might not work in some browsers. | ||
resCookies.getAll().forEach((cookie) => { | ||
const tempCookies = new ResponseCookies(new Headers()) | ||
tempCookies.set(cookie) | ||
responseHeaders.append('Set-Cookie', tempCookies.toString()) | ||
}) | ||
return responseHeaders | ||
} | ||
return headers | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 11 additions & 4 deletions
15
packages/next/src/server/future/route-modules/helpers/response-handlers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { draftMode } from 'next/headers' | ||
import { redirect } from 'next/navigation' | ||
|
||
export function GET() { | ||
draftMode().enable() | ||
return redirect('/some-other-page') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm not sure if headers could exist here.
Do we want to do
HeadersAdapter.from(req.headers)
here just in case?