Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
d315ee17
Unverified
Commit
d315ee17
authored
Aug 04, 2022
by
JJ Kasper
Committed by
GitHub
Aug 04, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ensure On-Demand revalidate does not consider preview cookie (#39313)
parent
6b49d474
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
103 additions
and
0 deletions
+103
-0
node.ts
packages/next/server/api-utils/node.ts
+7
-0
prerender.test.ts
test/e2e/prerender.test.ts
+75
-0
enable.js
test/e2e/prerender/pages/api/enable.js
+4
-0
preview.js
test/e2e/prerender/pages/preview.js
+17
-0
No files found.
packages/next/server/api-utils/node.ts
View file @
d315ee17
...
@@ -2,6 +2,7 @@ import type { IncomingMessage, ServerResponse } from 'http'
...
@@ -2,6 +2,7 @@ import type { IncomingMessage, ServerResponse } from 'http'
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'../../shared/lib/utils'
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'../../shared/lib/utils'
import
type
{
PageConfig
}
from
'next/types'
import
type
{
PageConfig
}
from
'next/types'
import
{
import
{
checkIsManualRevalidate
,
PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER
,
PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER
,
__ApiPreviewProps
,
__ApiPreviewProps
,
}
from
'.'
}
from
'.'
...
@@ -41,6 +42,12 @@ export function tryGetPreviewData(
...
@@ -41,6 +42,12 @@ export function tryGetPreviewData(
res
:
ServerResponse
|
BaseNextResponse
,
res
:
ServerResponse
|
BaseNextResponse
,
options
:
__ApiPreviewProps
options
:
__ApiPreviewProps
):
PreviewData
{
):
PreviewData
{
// if an On-Demand revalidation is being done preview mode
// is disabled
if
(
options
&&
checkIsManualRevalidate
(
req
,
options
).
isManualRevalidate
)
{
return
false
}
// Read cached preview data if present
// Read cached preview data if present
if
(
SYMBOL_PREVIEW_DATA
in
req
)
{
if
(
SYMBOL_PREVIEW_DATA
in
req
)
{
return
(
req
as
any
)[
SYMBOL_PREVIEW_DATA
]
as
any
return
(
req
as
any
)[
SYMBOL_PREVIEW_DATA
]
as
any
...
...
test/e2e/prerender.test.ts
View file @
d315ee17
import
fs
from
'fs-extra'
import
fs
from
'fs-extra'
import
cookie
from
'cookie'
import
cheerio
from
'cheerio'
import
cheerio
from
'cheerio'
import
{
join
,
sep
}
from
'path'
import
{
join
,
sep
}
from
'path'
import
escapeRegex
from
'escape-string-regexp'
import
escapeRegex
from
'escape-string-regexp'
...
@@ -178,6 +179,11 @@ describe('Prerender', () => {
...
@@ -178,6 +179,11 @@ describe('Prerender', () => {
initialRevalidateSeconds
:
1
,
initialRevalidateSeconds
:
1
,
srcRoute
:
null
,
srcRoute
:
null
,
},
},
'/preview'
:
{
dataRoute
:
`/_next/data/
${
next
.
buildId
}
/preview.json`
,
initialRevalidateSeconds
:
false
,
srcRoute
:
null
,
},
'/api-docs/first'
:
{
'/api-docs/first'
:
{
dataRoute
:
`/_next/data/
${
next
.
buildId
}
/api-docs/first.json`
,
dataRoute
:
`/_next/data/
${
next
.
buildId
}
/api-docs/first.json`
,
initialRevalidateSeconds
:
false
,
initialRevalidateSeconds
:
false
,
...
@@ -1518,6 +1524,14 @@ describe('Prerender', () => {
...
@@ -1518,6 +1524,14 @@ describe('Prerender', () => {
p
:
'p'
,
p
:
'p'
,
},
},
},
},
{
dataRouteRegex
:
normalizeRegEx
(
`^\\/_next\\/data\\/
${
escapeRegex
(
next
.
buildId
)}
\\/preview.json$`
),
page
:
'/preview'
,
},
{
{
dataRouteRegex
:
normalizeRegEx
(
dataRouteRegex
:
normalizeRegEx
(
`^\\/_next\\/data\\/
${
escapeRegex
(
`^\\/_next\\/data\\/
${
escapeRegex
(
...
@@ -1772,6 +1786,67 @@ describe('Prerender', () => {
...
@@ -1772,6 +1786,67 @@ describe('Prerender', () => {
})
})
}
}
it
(
'should revalidate manual revalidate with preview cookie'
,
async
()
=>
{
const
initialRes
=
await
fetchViaHTTP
(
next
.
url
,
'/preview'
)
expect
(
initialRes
.
status
).
toBe
(
200
)
const
initial$
=
cheerio
.
load
(
await
initialRes
.
text
())
const
initialProps
=
JSON
.
parse
(
initial$
(
'#props'
).
text
())
expect
(
initialProps
).
toEqual
({
preview
:
false
,
previewData
:
null
,
})
const
previewRes
=
await
fetchViaHTTP
(
next
.
url
,
'/api/enable'
)
let
previewCookie
=
''
expect
(
previewRes
.
headers
.
get
(
'set-cookie'
)).
toMatch
(
/
(
__prerender_bypass|__next_preview_data
)
/
)
previewRes
.
headers
.
get
(
'set-cookie'
)
.
split
(
','
)
.
forEach
((
c
)
=>
{
c
=
cookie
.
parse
(
c
)
const
isBypass
=
c
.
__prerender_bypass
if
(
isBypass
||
c
.
__next_preview_data
)
{
if
(
previewCookie
)
previewCookie
+=
'; '
previewCookie
+=
`
${
isBypass
?
'__prerender_bypass'
:
'__next_preview_data'
}
=
${
c
[
isBypass
?
'__prerender_bypass'
:
'__next_preview_data'
]}
`
}
})
const
apiRes
=
await
fetchViaHTTP
(
next
.
url
,
'/api/manual-revalidate'
,
{
pathname
:
'/preview'
},
{
headers
:
{
cookie
:
previewCookie
,
},
}
)
expect
(
apiRes
.
status
).
toBe
(
200
)
expect
(
await
apiRes
.
json
()).
toEqual
({
revalidated
:
true
})
const
postRevalidateRes
=
await
fetchViaHTTP
(
next
.
url
,
'/preview'
)
expect
(
initialRes
.
status
).
toBe
(
200
)
const
postRevalidate$
=
cheerio
.
load
(
await
postRevalidateRes
.
text
())
const
postRevalidateProps
=
JSON
.
parse
(
postRevalidate$
(
'#props'
).
text
())
expect
(
postRevalidateProps
).
toEqual
({
preview
:
false
,
previewData
:
null
,
})
})
it
(
'should handle revalidating HTML correctly'
,
async
()
=>
{
it
(
'should handle revalidating HTML correctly'
,
async
()
=>
{
const
route
=
'/blog/post-2/comment-2'
const
route
=
'/blog/post-2/comment-2'
const
initialHtml
=
await
renderViaHTTP
(
next
.
url
,
route
)
const
initialHtml
=
await
renderViaHTTP
(
next
.
url
,
route
)
...
...
test/e2e/prerender/pages/api/enable.js
0 → 100644
View file @
d315ee17
export
default
function
handler
(
req
,
res
)
{
res
.
setPreviewData
({
hello
:
'world'
})
res
.
json
({
enabled
:
true
})
}
test/e2e/prerender/pages/preview.js
0 → 100644
View file @
d315ee17
export
function
getStaticProps
({
preview
,
previewData
})
{
return
{
props
:
{
preview
:
preview
||
false
,
previewData
:
previewData
||
null
,
},
}
}
export
default
function
Page
(
props
)
{
return
(
<>
<
p
id
=
"page"
>
/preview</
p
>
<
p
id
=
"props"
>
{
JSON
.
stringify
(
props
)}
<
/p
>
<
/
>
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment