-
Notifications
You must be signed in to change notification settings - Fork 29.3k
Deduplication of cookies set by middleware and actions/API routes #69915
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: canary
Are you sure you want to change the base?
Changes from all commits
51f9667
c7000e1
68b9a73
62bfe78
c8042ca
cd3729d
c6028ee
87cbe15
614de35
646300f
ed0c56e
4820df2
78b5dc4
0d68564
14342b8
4341829
eb1d316
0f228bd
7386e55
a1b74c7
df22e72
23fea62
cd89568
2d66bc8
473cd0f
05cbac8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use server' | ||
|
||
import { cookies } from 'next/headers' | ||
|
||
export async function cookieAction() { | ||
const cookieStore = await cookies() | ||
cookieStore.set('common-cookie', 'from-action') | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export async function GET() { | ||
return new Response('', { | ||
status: 200, | ||
headers: { 'Set-Cookie': `common-cookie=from-api; Path=/` }, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from 'react' | ||
|
||
export default function Root({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use client' | ||
|
||
import React from 'react' | ||
import { cookieAction } from './actions' | ||
|
||
export default function Page() { | ||
const api = async () => { | ||
await fetch('/api') | ||
} | ||
|
||
return ( | ||
<> | ||
<button id="action" onClick={() => cookieAction()}> | ||
click | ||
</button> | ||
<button id="api" onClick={() => api()}> | ||
click | ||
</button> | ||
</> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
|
||
describe('cookies-dedup', () => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
it('cookies set by middleware should be removed if action sets the same cookie', async () => { | ||
const browser = await next.browser('/') | ||
const url = await browser.url() | ||
await browser.waitForElementByCss('button#action') | ||
|
||
const actionResponsePromise = browser.waitForResponse(url) | ||
await browser.elementByCss('button#action').click() | ||
const actionResponse = await actionResponsePromise | ||
|
||
const headers = await actionResponse.allHeaders() | ||
const setCookieHeaders = headers['set-cookie'] | ||
expect(setCookieHeaders).toEqual('common-cookie=from-action; Path=/') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems this isn't working as expected when deployed (x-ref)... that's one tricky part about these middleware tests. I've deployed with your changes here so you can see what I mean: https://vtest314-e2e-tests-67c8g3e0e-ztanner.vercel.app/ I think we might need to dig a bit more into what's happening here. One way you can test with your patched version of next.js is to:
I can also try and take a closer look at this in case you're stuck or don't have the bandwidth. |
||
}) | ||
|
||
it('cookies set by middleware should be removed if api route sets the same cookie', async () => { | ||
const browser = await next.browser('/') | ||
const url = await browser.url() | ||
await browser.waitForElementByCss('button#api') | ||
|
||
const apiResponsePromise = browser.waitForResponse(`${url}api`) | ||
await browser.elementByCss('button#api').click() | ||
const apiResponse = await apiResponsePromise | ||
|
||
const headers = await apiResponse.allHeaders() | ||
const setCookieHeaders = headers['set-cookie'] | ||
expect(setCookieHeaders).toEqual('common-cookie=from-api; Path=/') | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { NextResponse } from 'next/server' | ||
|
||
export default function () { | ||
const response = NextResponse.next() | ||
response.cookies.set('common-cookie', 'from-middleware') | ||
return response | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = {} | ||
|
||
module.exports = nextConfig |
Uh oh!
There was an error while loading. Please reload this page.