Skip to content

Commit

Permalink
Fix Cookie Expiration (#10634)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer committed Feb 21, 2020
1 parent ee7ea03 commit 43a70c7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/next/next-server/server/api-utils.ts
Expand Up @@ -414,13 +414,19 @@ function clearPreviewData<T>(res: NextApiResponse<T>): NextApiResponse<T> {
? previous
: []),
serialize(COOKIE_NAME_PRERENDER_BYPASS, '', {
maxAge: 0,
// To delete a cookie, set `expires` to a date in the past:
// https://tools.ietf.org/html/rfc6265#section-4.1.1
// `Max-Age: 0` is not valid, thus ignored, and the cookie is persisted.
expires: new Date(0),
httpOnly: true,
sameSite: 'strict',
path: '/',
}),
serialize(COOKIE_NAME_PRERENDER_DATA, '', {
maxAge: 0,
// To delete a cookie, set `expires` to a date in the past:
// https://tools.ietf.org/html/rfc6265#section-4.1.1
// `Max-Age: 0` is not valid, thus ignored, and the cookie is persisted.
expires: new Date(0),
httpOnly: true,
sameSite: 'strict',
path: '/',
Expand Down
7 changes: 5 additions & 2 deletions test/integration/prerender-preview/test/index.test.js
Expand Up @@ -106,22 +106,25 @@ function runTests() {

const cookies = res.headers
.get('set-cookie')
.replace(/(=\w{3}),/g, '$1')
.split(',')
.map(cookie.parse)

expect(cookies.length).toBe(2)
expect(cookies[0]).toMatchObject({
Path: '/',
SameSite: 'Strict',
'Max-Age': '0',
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',
'Max-Age': '0',
Expires: 'Thu 01 Jan 1970 00:00:00 GMT',
})
expect(cookies[1]).toHaveProperty('__next_preview_data')
expect(cookies[1]).not.toHaveProperty('Max-Age')
})

/** @type import('next-webdriver').Chain */
Expand Down

0 comments on commit 43a70c7

Please sign in to comment.