Skip to content

Commit

Permalink
Use Lax Cookie for Preview Mode (#11495)
Browse files Browse the repository at this point in the history
* Use Lax Cookie for Preview Mode

* Fix tests
  • Loading branch information
Timer authored Mar 30, 2020
1 parent 2713369 commit c2c4241
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions packages/next/next-server/server/api-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,15 @@ 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)
: undefined),
}),
serialize(COOKIE_NAME_PRERENDER_DATA, payload, {
httpOnly: true,
sameSite: 'strict',
sameSite: 'lax',
path: '/',
...(options.maxAge !== undefined
? ({ maxAge: options.maxAge } as CookieSerializeOptions)
Expand Down Expand Up @@ -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, '', {
Expand All @@ -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: '/',
}),
])
Expand Down
12 changes: 6 additions & 6 deletions test/integration/getserversideprops-preview/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)

Expand Down
12 changes: 6 additions & 6 deletions test/integration/prerender-preview/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit c2c4241

Please sign in to comment.