Unverified Commit c2c42418 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Use Lax Cookie for Preview Mode (#11495)

* Use Lax Cookie for Preview Mode

* Fix tests
parent 2713369e
......@@ -394,7 +394,7 @@ function setPreviewData<T>(
: []),
serialize(COOKIE_NAME_PRERENDER_BYPASS, options.previewModeId, {
httpOnly: true,
sameSite: 'strict',
sameSite: 'lax',
path: '/',
...(options.maxAge !== undefined
? ({ maxAge: options.maxAge } as CookieSerializeOptions)
......@@ -402,7 +402,7 @@ function setPreviewData<T>(
}),
serialize(COOKIE_NAME_PRERENDER_DATA, payload, {
httpOnly: true,
sameSite: 'strict',
sameSite: 'lax',
path: '/',
...(options.maxAge !== undefined
? ({ maxAge: options.maxAge } as CookieSerializeOptions)
......@@ -431,7 +431,7 @@ function clearPreviewData<T>(res: NextApiResponse<T>): NextApiResponse<T> {
// `Max-Age: 0` is not valid, thus ignored, and the cookie is persisted.
expires: new Date(0),
httpOnly: true,
sameSite: 'strict',
sameSite: 'lax',
path: '/',
}),
serialize(COOKIE_NAME_PRERENDER_DATA, '', {
......@@ -440,7 +440,7 @@ function clearPreviewData<T>(res: NextApiResponse<T>): NextApiResponse<T> {
// `Max-Age: 0` is not valid, thus ignored, and the cookie is persisted.
expires: new Date(0),
httpOnly: true,
sameSite: 'strict',
sameSite: 'lax',
path: '/',
}),
])
......
......@@ -74,10 +74,10 @@ function runTests(startServer = nextStart) {
.map(cookie.parse)
expect(cookies.length).toBe(2)
expect(cookies[0]).toMatchObject({ Path: '/', SameSite: 'Strict' })
expect(cookies[0]).toMatchObject({ Path: '/', SameSite: 'Lax' })
expect(cookies[0]).toHaveProperty('__prerender_bypass')
expect(cookies[0]).not.toHaveProperty('Max-Age')
expect(cookies[1]).toMatchObject({ Path: '/', SameSite: 'Strict' })
expect(cookies[1]).toMatchObject({ Path: '/', SameSite: 'Lax' })
expect(cookies[1]).toHaveProperty('__next_preview_data')
expect(cookies[1]).not.toHaveProperty('Max-Age')
......@@ -135,21 +135,21 @@ function runTests(startServer = nextStart) {
const cookies = res.headers
.get('set-cookie')
.replace(/(=\w{3}),/g, '$1')
.replace(/(=(?!Lax)\w{3}),/g, '$1')
.split(',')
.map(cookie.parse)
expect(cookies.length).toBe(2)
expect(cookies[0]).toMatchObject({
Path: '/',
SameSite: 'Strict',
SameSite: 'Lax',
Expires: 'Thu 01 Jan 1970 00:00:00 GMT',
})
expect(cookies[0]).toHaveProperty('__prerender_bypass')
expect(cookies[0]).not.toHaveProperty('Max-Age')
expect(cookies[1]).toMatchObject({
Path: '/',
SameSite: 'Strict',
SameSite: 'Lax',
Expires: 'Thu 01 Jan 1970 00:00:00 GMT',
})
expect(cookies[1]).toHaveProperty('__next_preview_data')
......@@ -266,7 +266,7 @@ describe('ServerSide Props Preview Mode', () => {
const cookies = res.headers
.get('set-cookie')
.replace(/(=\w{3}),/g, '$1')
.replace(/(=(?!Lax)\w{3}),/g, '$1')
.split(',')
.map(cookie.parse)
......
......@@ -80,10 +80,10 @@ function runTests(startServer = nextStart) {
.map(cookie.parse)
expect(cookies.length).toBe(2)
expect(cookies[0]).toMatchObject({ Path: '/', SameSite: 'Strict' })
expect(cookies[0]).toMatchObject({ Path: '/', SameSite: 'Lax' })
expect(cookies[0]).toHaveProperty('__prerender_bypass')
expect(cookies[0]).not.toHaveProperty('Max-Age')
expect(cookies[1]).toMatchObject({ Path: '/', SameSite: 'Strict' })
expect(cookies[1]).toMatchObject({ Path: '/', SameSite: 'Lax' })
expect(cookies[1]).toHaveProperty('__next_preview_data')
expect(cookies[1]).not.toHaveProperty('Max-Age')
......@@ -141,21 +141,21 @@ function runTests(startServer = nextStart) {
const cookies = res.headers
.get('set-cookie')
.replace(/(=\w{3}),/g, '$1')
.replace(/(=(?!Lax)\w{3}),/g, '$1')
.split(',')
.map(cookie.parse)
expect(cookies.length).toBe(2)
expect(cookies[0]).toMatchObject({
Path: '/',
SameSite: 'Strict',
SameSite: 'Lax',
Expires: 'Thu 01 Jan 1970 00:00:00 GMT',
})
expect(cookies[0]).toHaveProperty('__prerender_bypass')
expect(cookies[0]).not.toHaveProperty('Max-Age')
expect(cookies[1]).toMatchObject({
Path: '/',
SameSite: 'Strict',
SameSite: 'Lax',
Expires: 'Thu 01 Jan 1970 00:00:00 GMT',
})
expect(cookies[1]).toHaveProperty('__next_preview_data')
......@@ -266,7 +266,7 @@ describe('Prerender Preview Mode', () => {
const cookies = res.headers
.get('set-cookie')
.replace(/(=\w{3}),/g, '$1')
.replace(/(=(?!Lax)\w{3}),/g, '$1')
.split(',')
.map(cookie.parse)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment