Unverified Commit b9adf551 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Allow Draft Mode on unlisted closed routes in adapter deployments (#99370)

On adapter deployments, a Draft Mode request for an unpublished URL
incorrectly returns 404 instead of rendering the preview. This affects
App Router pages with `dynamicParams = false` and Pages Router pages
with `fallback: false` when the build did not generate that URL. An
ordinary request to the same URL should still return 404.

The adapter previously required both `__prerender_bypass` and
`__next_preview_data` to route the request. Pages Router
`setDraftMode()` and App Router `draftMode().enable()` set only
`__prerender_bypass`. The adapter now routes requests whose bypass
cookie matches the build's preview ID. Pages Router still validates
legacy preview data when present and rejects malformed payloads.

The Pages regression tests cover document and data requests, malformed
preview data, and public cache isolation. The App Router draft test now
runs on adapter deployments. These cases also run in dev and `next
start`. The legacy builder deploy jobs skip them.
parent 3932a6bd
......@@ -2100,16 +2100,14 @@ export async function handleBuildComplete({
return '?' + items.map(([key, value]) => `${value}=$${key}`).join('&')
}
// The valid bypass token admits both Draft Mode and legacy Preview Mode.
// Pages Router validates legacy preview data after routing.
const fallbackFalseHasCondition: RouteHas[] = [
{
type: 'cookie',
key: '__prerender_bypass',
value: previewProps.previewModeId,
},
{
type: 'cookie',
key: '__next_preview_data',
},
]
// Without this collapse the loop below emits one entry per shell.
......@@ -2204,7 +2202,7 @@ export async function handleBuildComplete({
// A single entry can serve both forms of the request only when both carry
// the same conditions. A pages router route with `fallback: false` is the
// one case where they differ: it requires the preview cookies on the
// one case where they differ: it requires the bypass cookie on the
// plain form, and not on the suffixed form. An entry holds one set of
// conditions, so that case keeps a separate entry per form.
const canMergeSuffixedAndPlain =
......
......@@ -50,9 +50,9 @@ describe('dynamicParams: false request modes', () => {
return cookie
}
// Existing adapter routing requires legacy preview cookies and rejects App
// Router draft requests to unlisted closed URLs before Next.js handles them.
// @force-gate !deploy
// This test covers Draft Mode only with the adapter on deploy. The legacy
// builder still requires both preview cookies for unlisted closed paths.
// @force-gate !deploy || adapter
it('previews an unlisted path without admitting it for ordinary requests', async () => {
expect((await next.fetch('/products/unpublished')).status).toBe(404)
const cookie = await enableDraftMode()
......
export default function handler(req, res) {
res.setDraftMode({ enable: true })
res.end('draft enabled')
}
......@@ -15,6 +15,20 @@ describe('Preview mode with fallback pages', () => {
let previewCookie: string
async function getDraftModeCookie() {
const response = await next.fetch('/api/enable-draft')
expect(response.status).toBe(200)
const setCookie = response.headers.get('set-cookie')
expect(setCookie).toBeTruthy()
const cookies = setCookie!.split(',').map((value) => cookie.parse(value))
const bypass = cookies.find(
(value) => value.__prerender_bypass
)?.__prerender_bypass
expect(Boolean(bypass)).toBe(true)
expect(cookies.some((value) => value.__next_preview_data)).toBe(false)
return cookie.serialize('__prerender_bypass', bypass!)
}
it('should get preview cookie correctly', async () => {
const res = await next.fetch('/api/enable')
previewCookie = ''
......@@ -293,4 +307,94 @@ describe('Preview mode with fallback pages', () => {
params: { post: 'second' },
})
})
// @force-gate !deploy || adapter
it('should preview an unlisted fallback: false page in Draft Mode', async () => {
const pathname = '/no-fallback/draft-only'
expect((await next.fetch(pathname)).status).toBe(404)
const response = await next.fetch(pathname, {
headers: { cookie: await getDraftModeCookie() },
})
const props = JSON.parse(
cheerio
.load(await response.text())('#props')
.text()
)
expect(props).toEqual({
preview: true,
previewData: {},
params: { post: 'draft-only' },
})
expect((await next.fetch(pathname)).status).toBe(404)
})
// @force-gate !deploy || adapter
it('should preview an unlisted data request in Draft Mode', async () => {
const knownHtml = await next.render('/no-fallback/first')
const buildId = JSON.parse(
cheerio.load(knownHtml)('#__NEXT_DATA__').text()
).buildId
const pathname = '/_next/data/' + buildId + '/no-fallback/draft-only.json'
expect((await next.fetch(pathname)).status).toBe(404)
const response = await next.fetch(pathname, {
headers: { cookie: await getDraftModeCookie() },
})
expect((await response.json()).pageProps).toEqual({
preview: true,
previewData: {},
params: { post: 'draft-only' },
})
expect((await next.fetch(pathname)).status).toBe(404)
})
// @force-gate !deploy || adapter
it('should reject invalid preview data on an unlisted fallback: false path', async () => {
const headers = {
cookie: (await getDraftModeCookie()) + '; __next_preview_data=invalid',
}
const response = await next.fetch('/no-fallback/draft-only', { headers })
expect(response.status).toBe(404)
expect(
cheerio
.load(await response.text())('#props')
.text()
).toBe('')
const knownHtml = await next.render('/no-fallback/first')
const buildId = JSON.parse(
cheerio.load(knownHtml)('#__NEXT_DATA__').text()
).buildId
const dataPath = '/_next/data/' + buildId + '/no-fallback/draft-only.json'
const dataResponse = await next.fetch(dataPath, { headers })
expect(dataResponse.status).toBe(404)
})
// @force-gate !deploy || adapter
it('should not replace a prerendered page when previewing it in Draft Mode', async () => {
const pathname = '/no-fallback/first'
const beforeHtml = await next.render(pathname)
const beforeProps = JSON.parse(cheerio.load(beforeHtml)('#props').text())
expect(beforeProps.preview).toBe(false)
const response = await next.fetch(pathname, {
headers: { cookie: await getDraftModeCookie() },
})
const draftProps = JSON.parse(
cheerio
.load(await response.text())('#props')
.text()
)
expect(draftProps).toEqual({
preview: true,
previewData: {},
params: { post: 'first' },
})
const afterHtml = await next.render(pathname)
expect(JSON.parse(cheerio.load(afterHtml)('#props').text())).toEqual(
beforeProps
)
})
})
......@@ -45,7 +45,7 @@ describe('adapter dynamic routes (legacy)', () => {
/legacy/[id]
^/_next/data/test\\-build\\-id[/]?/legacy/(?<nxtPid>[^/]+?)\\.json(?:/)?$
-> /_next/data/test-build-id/legacy/[id].json?nxtPid=$nxtPid
[has cookie __prerender_bypass, has cookie __next_preview_data]
[has cookie __prerender_bypass]
/static-one
^/_next/data/test\\-build\\-id[/]?/static\\-one\\.json(?:/)?$
......@@ -78,7 +78,7 @@ describe('adapter dynamic routes (legacy)', () => {
/legacy/[id]
^[/]?/legacy/(?<nxtPid>[^/]+?)(?:/)?$
-> /legacy/[id]?nxtPid=$nxtPid
[has cookie __prerender_bypass, has cookie __next_preview_data]"
[has cookie __prerender_bypass]"
`)
})
})
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