Unverified Commit d315ee17 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure On-Demand revalidate does not consider preview cookie (#39313)

parent 6b49d474
...@@ -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
......
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)
......
export default function handler(req, res) {
res.setPreviewData({ hello: 'world' })
res.json({ enabled: true })
}
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>
</>
)
}
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