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>( ...@@ -394,7 +394,7 @@ function setPreviewData<T>(
: []), : []),
serialize(COOKIE_NAME_PRERENDER_BYPASS, options.previewModeId, { serialize(COOKIE_NAME_PRERENDER_BYPASS, options.previewModeId, {
httpOnly: true, httpOnly: true,
sameSite: 'strict', sameSite: 'lax',
path: '/', path: '/',
...(options.maxAge !== undefined ...(options.maxAge !== undefined
? ({ maxAge: options.maxAge } as CookieSerializeOptions) ? ({ maxAge: options.maxAge } as CookieSerializeOptions)
...@@ -402,7 +402,7 @@ function setPreviewData<T>( ...@@ -402,7 +402,7 @@ function setPreviewData<T>(
}), }),
serialize(COOKIE_NAME_PRERENDER_DATA, payload, { serialize(COOKIE_NAME_PRERENDER_DATA, payload, {
httpOnly: true, httpOnly: true,
sameSite: 'strict', sameSite: 'lax',
path: '/', path: '/',
...(options.maxAge !== undefined ...(options.maxAge !== undefined
? ({ maxAge: options.maxAge } as CookieSerializeOptions) ? ({ maxAge: options.maxAge } as CookieSerializeOptions)
...@@ -431,7 +431,7 @@ function clearPreviewData<T>(res: NextApiResponse<T>): NextApiResponse<T> { ...@@ -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. // `Max-Age: 0` is not valid, thus ignored, and the cookie is persisted.
expires: new Date(0), expires: new Date(0),
httpOnly: true, httpOnly: true,
sameSite: 'strict', sameSite: 'lax',
path: '/', path: '/',
}), }),
serialize(COOKIE_NAME_PRERENDER_DATA, '', { serialize(COOKIE_NAME_PRERENDER_DATA, '', {
...@@ -440,7 +440,7 @@ function clearPreviewData<T>(res: NextApiResponse<T>): NextApiResponse<T> { ...@@ -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. // `Max-Age: 0` is not valid, thus ignored, and the cookie is persisted.
expires: new Date(0), expires: new Date(0),
httpOnly: true, httpOnly: true,
sameSite: 'strict', sameSite: 'lax',
path: '/', path: '/',
}), }),
]) ])
......
...@@ -74,10 +74,10 @@ function runTests(startServer = nextStart) { ...@@ -74,10 +74,10 @@ function runTests(startServer = nextStart) {
.map(cookie.parse) .map(cookie.parse)
expect(cookies.length).toBe(2) 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]).toHaveProperty('__prerender_bypass')
expect(cookies[0]).not.toHaveProperty('Max-Age') 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]).toHaveProperty('__next_preview_data')
expect(cookies[1]).not.toHaveProperty('Max-Age') expect(cookies[1]).not.toHaveProperty('Max-Age')
...@@ -135,21 +135,21 @@ function runTests(startServer = nextStart) { ...@@ -135,21 +135,21 @@ function runTests(startServer = nextStart) {
const cookies = res.headers const cookies = res.headers
.get('set-cookie') .get('set-cookie')
.replace(/(=\w{3}),/g, '$1') .replace(/(=(?!Lax)\w{3}),/g, '$1')
.split(',') .split(',')
.map(cookie.parse) .map(cookie.parse)
expect(cookies.length).toBe(2) expect(cookies.length).toBe(2)
expect(cookies[0]).toMatchObject({ expect(cookies[0]).toMatchObject({
Path: '/', Path: '/',
SameSite: 'Strict', SameSite: 'Lax',
Expires: 'Thu 01 Jan 1970 00:00:00 GMT', Expires: 'Thu 01 Jan 1970 00:00:00 GMT',
}) })
expect(cookies[0]).toHaveProperty('__prerender_bypass') expect(cookies[0]).toHaveProperty('__prerender_bypass')
expect(cookies[0]).not.toHaveProperty('Max-Age') expect(cookies[0]).not.toHaveProperty('Max-Age')
expect(cookies[1]).toMatchObject({ expect(cookies[1]).toMatchObject({
Path: '/', Path: '/',
SameSite: 'Strict', SameSite: 'Lax',
Expires: 'Thu 01 Jan 1970 00:00:00 GMT', Expires: 'Thu 01 Jan 1970 00:00:00 GMT',
}) })
expect(cookies[1]).toHaveProperty('__next_preview_data') expect(cookies[1]).toHaveProperty('__next_preview_data')
...@@ -266,7 +266,7 @@ describe('ServerSide Props Preview Mode', () => { ...@@ -266,7 +266,7 @@ describe('ServerSide Props Preview Mode', () => {
const cookies = res.headers const cookies = res.headers
.get('set-cookie') .get('set-cookie')
.replace(/(=\w{3}),/g, '$1') .replace(/(=(?!Lax)\w{3}),/g, '$1')
.split(',') .split(',')
.map(cookie.parse) .map(cookie.parse)
......
...@@ -80,10 +80,10 @@ function runTests(startServer = nextStart) { ...@@ -80,10 +80,10 @@ function runTests(startServer = nextStart) {
.map(cookie.parse) .map(cookie.parse)
expect(cookies.length).toBe(2) 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]).toHaveProperty('__prerender_bypass')
expect(cookies[0]).not.toHaveProperty('Max-Age') 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]).toHaveProperty('__next_preview_data')
expect(cookies[1]).not.toHaveProperty('Max-Age') expect(cookies[1]).not.toHaveProperty('Max-Age')
...@@ -141,21 +141,21 @@ function runTests(startServer = nextStart) { ...@@ -141,21 +141,21 @@ function runTests(startServer = nextStart) {
const cookies = res.headers const cookies = res.headers
.get('set-cookie') .get('set-cookie')
.replace(/(=\w{3}),/g, '$1') .replace(/(=(?!Lax)\w{3}),/g, '$1')
.split(',') .split(',')
.map(cookie.parse) .map(cookie.parse)
expect(cookies.length).toBe(2) expect(cookies.length).toBe(2)
expect(cookies[0]).toMatchObject({ expect(cookies[0]).toMatchObject({
Path: '/', Path: '/',
SameSite: 'Strict', SameSite: 'Lax',
Expires: 'Thu 01 Jan 1970 00:00:00 GMT', Expires: 'Thu 01 Jan 1970 00:00:00 GMT',
}) })
expect(cookies[0]).toHaveProperty('__prerender_bypass') expect(cookies[0]).toHaveProperty('__prerender_bypass')
expect(cookies[0]).not.toHaveProperty('Max-Age') expect(cookies[0]).not.toHaveProperty('Max-Age')
expect(cookies[1]).toMatchObject({ expect(cookies[1]).toMatchObject({
Path: '/', Path: '/',
SameSite: 'Strict', SameSite: 'Lax',
Expires: 'Thu 01 Jan 1970 00:00:00 GMT', Expires: 'Thu 01 Jan 1970 00:00:00 GMT',
}) })
expect(cookies[1]).toHaveProperty('__next_preview_data') expect(cookies[1]).toHaveProperty('__next_preview_data')
...@@ -266,7 +266,7 @@ describe('Prerender Preview Mode', () => { ...@@ -266,7 +266,7 @@ describe('Prerender Preview Mode', () => {
const cookies = res.headers const cookies = res.headers
.get('set-cookie') .get('set-cookie')
.replace(/(=\w{3}),/g, '$1') .replace(/(=(?!Lax)\w{3}),/g, '$1')
.split(',') .split(',')
.map(cookie.parse) .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