Unverified Commit 49e0a8a8 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Handle Pages data route skew with deployment id header, take 2 (#89325)

Reapply https://github.com/vercel/next.js/pull/88959
Revert https://github.com/vercel/next.js/pull/89323


Closes PACK-6537
Closes PACK-6756

- Deployment skew (forcing a MPA nav instead of a SPA nav if a deployment happened inbetween) is handled by comparing a header on the client side.
    - The other case that used build ids was app router, which was changed to the same approach in https://github.com/vercel/next.js/pull/88855
- Then, when a deployment id is available, make the build id a constant
	- Particularly, this makes the rollout of this much easier as the data route paths stay the same (both in Next.js itself as well as in the builder)


If you use `output:export` and set `config.deploymentId` (i.e. enable skew protection) you are still expected to set these headers.
parent 9e63d487
...@@ -986,5 +986,6 @@ ...@@ -986,5 +986,6 @@
"985": "No response is returned from route handler '%s'. Expected a Response object but received '%s' (method: %s, url: %s). Ensure you return a \\`Response\\` or a \\`NextResponse\\` in all branches of your handler.", "985": "No response is returned from route handler '%s'. Expected a Response object but received '%s' (method: %s, url: %s). Ensure you return a \\`Response\\` or a \\`NextResponse\\` in all branches of your handler.",
"986": "Server Action arguments list is too long (%s). Maximum allowed is %s.", "986": "Server Action arguments list is too long (%s). Maximum allowed is %s.",
"987": "Invalid \\`deploymentId\\` configuration: must be a string. See https://nextjs.org/docs/messages/deploymentid-not-a-string", "987": "Invalid \\`deploymentId\\` configuration: must be a string. See https://nextjs.org/docs/messages/deploymentid-not-a-string",
"988": "Invalid \\`deploymentId\\` configuration: contains invalid characters. Only alphanumeric characters, hyphens, and underscores are allowed. See https://nextjs.org/docs/messages/deploymentid-invalid-characters" "988": "Invalid \\`deploymentId\\` configuration: contains invalid characters. Only alphanumeric characters, hyphens, and underscores are allowed. See https://nextjs.org/docs/messages/deploymentid-invalid-characters",
"989": "Loaded static props were from an outdated deployment, forcing a hard reload"
} }
...@@ -903,9 +903,16 @@ async function getBuildId( ...@@ -903,9 +903,16 @@ async function getBuildId(
if (isGenerateMode) { if (isGenerateMode) {
return await fs.readFile(path.join(distDir, BUILD_ID_FILE), 'utf8') return await fs.readFile(path.join(distDir, BUILD_ID_FILE), 'utf8')
} }
if (config.deploymentId) {
// Skew protection is enabled and NEXT_NAV_DEPLOYMENT_ID_HEADER will be used instead. Set a
// constant but "random" string because various tools perform `.replace(escapedBuildId, ....)`
// which would fail if this were something like "build-id" instead.
return 'build-TfctsWXpff2fKS'
} else {
return await nextBuildSpan return await nextBuildSpan
.traceChild('generate-buildid') .traceChild('generate-buildid')
.traceAsyncFn(() => generateBuildId(config.generateBuildId, nanoid)) .traceAsyncFn(() => generateBuildId(config.generateBuildId, nanoid))
}
} }
export default async function build( export default async function build(
......
...@@ -760,7 +760,8 @@ export default async function loadCustomRoutes( ...@@ -760,7 +760,8 @@ export default async function loadCustomRoutes(
}) })
} }
headers.unshift({ headers.unshift(
{
source: '/:path*', source: '/:path*',
has: [ has: [
{ {
...@@ -775,7 +776,17 @@ export default async function loadCustomRoutes( ...@@ -775,7 +776,17 @@ export default async function loadCustomRoutes(
value: config.deploymentId, value: config.deploymentId,
}, },
], ],
}) },
{
source: '/_next/data/(.*)',
headers: [
{
key: NEXT_NAV_DEPLOYMENT_ID_HEADER,
value: config.deploymentId,
},
],
}
)
} }
if (!config.skipTrailingSlashRedirect) { if (!config.skipTrailingSlashRedirect) {
......
...@@ -31,6 +31,7 @@ import { ...@@ -31,6 +31,7 @@ import {
CACHE_ONE_YEAR_SECONDS, CACHE_ONE_YEAR_SECONDS,
HTML_CONTENT_TYPE_HEADER, HTML_CONTENT_TYPE_HEADER,
JSON_CONTENT_TYPE_HEADER, JSON_CONTENT_TYPE_HEADER,
NEXT_NAV_DEPLOYMENT_ID_HEADER,
} from '../../../lib/constants' } from '../../../lib/constants'
import path from 'path' import path from 'path'
import { sendRenderResult } from '../../send-payload' import { sendRenderResult } from '../../send-payload'
...@@ -634,6 +635,10 @@ export const getHandler = ({ ...@@ -634,6 +635,10 @@ export const getHandler = ({
res.statusCode = 404 res.statusCode = 404
if (isNextDataRequest) { if (isNextDataRequest) {
const deploymentId = getDeploymentId()
if (deploymentId) {
res.setHeader(NEXT_NAV_DEPLOYMENT_ID_HEADER, deploymentId)
}
res.end('{"notFound":true}') res.end('{"notFound":true}')
return return
} }
...@@ -642,6 +647,10 @@ export const getHandler = ({ ...@@ -642,6 +647,10 @@ export const getHandler = ({
if (result.value.kind === CachedRouteKind.REDIRECT) { if (result.value.kind === CachedRouteKind.REDIRECT) {
if (isNextDataRequest) { if (isNextDataRequest) {
const deploymentId = getDeploymentId()
if (deploymentId) {
res.setHeader(NEXT_NAV_DEPLOYMENT_ID_HEADER, deploymentId)
}
res.setHeader('content-type', JSON_CONTENT_TYPE_HEADER) res.setHeader('content-type', JSON_CONTENT_TYPE_HEADER)
res.end(JSON.stringify(result.value.props)) res.end(JSON.stringify(result.value.props))
return return
...@@ -714,6 +723,14 @@ export const getHandler = ({ ...@@ -714,6 +723,14 @@ export const getHandler = ({
return null return null
} }
// Add deployment ID header for data requests
if (isNextDataRequest && !isErrorPage && !is500Page) {
const deploymentId = getDeploymentId()
if (deploymentId) {
res.setHeader(NEXT_NAV_DEPLOYMENT_ID_HEADER, deploymentId)
}
}
await sendRenderResult({ await sendRenderResult({
req, req,
res, res,
......
...@@ -42,7 +42,10 @@ import { omit } from './utils/omit' ...@@ -42,7 +42,10 @@ import { omit } from './utils/omit'
import { interpolateAs } from './utils/interpolate-as' import { interpolateAs } from './utils/interpolate-as'
import { disableSmoothScrollDuringRouteTransition } from './utils/disable-smooth-scroll' import { disableSmoothScrollDuringRouteTransition } from './utils/disable-smooth-scroll'
import type { Params } from '../../../server/request/params' import type { Params } from '../../../server/request/params'
import { MATCHED_PATH_HEADER } from '../../../lib/constants' import {
MATCHED_PATH_HEADER,
NEXT_NAV_DEPLOYMENT_ID_HEADER,
} from '../../../lib/constants'
import { getDeploymentId } from '../deployment-id' import { getDeploymentId } from '../deployment-id'
import { isJavaScriptURLString } from '../../../client/lib/javascript-url' import { isJavaScriptURLString } from '../../../client/lib/javascript-url'
...@@ -556,6 +559,25 @@ function fetchNextData({ ...@@ -556,6 +559,25 @@ function fetchNextData({
throw error throw error
} }
let dplResponseHeader = response.headers.get(
NEXT_NAV_DEPLOYMENT_ID_HEADER
)
if (dplResponseHeader != null && dplResponseHeader !== deploymentId) {
// When not found, or we want to force a MPA navigation because of Skew Protection
const error = new Error(
`Loaded static props were from an outdated deployment, forcing a hard reload`
)
/**
* We should only trigger a server-side transition if this was
* caused on a client-side transition. Otherwise, we'd get into
* an infinite loop.
*/
if (!isServerRender) {
markAssetError(error)
}
throw error
}
return { return {
dataHref, dataHref,
json: parseJSON ? tryToParseAsJSON(text) : null, json: parseJSON ? tryToParseAsJSON(text) : null,
......
/* eslint-env jest */
import { join } from 'path'
import http from 'http' import http from 'http'
import httpProxy from 'http-proxy' import httpProxy from 'http-proxy'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
...@@ -11,8 +8,9 @@ import { ...@@ -11,8 +8,9 @@ import {
nextBuild, nextBuild,
nextStart, nextStart,
} from 'next-test-utils' } from 'next-test-utils'
import { isNextDeploy, isNextDev, isNextStart, nextTestSetup } from 'e2e-utils'
const appDir = join(__dirname, '..') const appDir = __dirname
let proxyPort let proxyPort
...@@ -52,12 +50,13 @@ const runTests = (switchDeployment: (bool) => void) => { ...@@ -52,12 +50,13 @@ const runTests = (switchDeployment: (bool) => void) => {
}) })
} }
describe('SSG data 404 - hard navigate when a new deployment occurs', () => { describe('pages ssg data deployment skew - hard navigate when a new deployment occurs', () => {
if (process.platform === 'win32') { if (process.platform === 'win32') {
it('should skip this suite on Windows', () => {}) it('should skip this suite on Windows', () => {})
return return
} }
if (isNextDev) {
describe('development mode', () => { describe('development mode', () => {
let should404Data = false let should404Data = false
let apps = [] let apps = []
...@@ -101,7 +100,9 @@ describe('SSG data 404 - hard navigate when a new deployment occurs', () => { ...@@ -101,7 +100,9 @@ describe('SSG data 404 - hard navigate when a new deployment occurs', () => {
should404Data = v should404Data = v
}) })
}) })
}
if (isNextStart) {
describe.each([ describe.each([
{ name: 'with build id' }, { name: 'with build id' },
{ {
...@@ -109,9 +110,10 @@ describe('SSG data 404 - hard navigate when a new deployment occurs', () => { ...@@ -109,9 +110,10 @@ describe('SSG data 404 - hard navigate when a new deployment occurs', () => {
NEXT_DEPLOYMENT_ID1: 'deployment-id-1', NEXT_DEPLOYMENT_ID1: 'deployment-id-1',
NEXT_DEPLOYMENT_ID2: 'deployment-id-2', NEXT_DEPLOYMENT_ID2: 'deployment-id-2',
}, },
{ name: 'with build id (output export)', OUTPUT_MODE: 'export' },
])( ])(
'production mode $name', 'production mode $name',
({ NEXT_DEPLOYMENT_ID1, NEXT_DEPLOYMENT_ID2 }) => { ({ NEXT_DEPLOYMENT_ID1, NEXT_DEPLOYMENT_ID2, OUTPUT_MODE }) => {
let shouldSwitchDeployment = false let shouldSwitchDeployment = false
let apps = [] let apps = []
let proxyServer let proxyServer
...@@ -121,6 +123,7 @@ describe('SSG data 404 - hard navigate when a new deployment occurs', () => { ...@@ -121,6 +123,7 @@ describe('SSG data 404 - hard navigate when a new deployment occurs', () => {
env: { env: {
DIST_DIR: '1', DIST_DIR: '1',
NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID1, NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID1,
OUTPUT_MODE,
}, },
}) })
let appPort1 = await findPort() let appPort1 = await findPort()
...@@ -129,6 +132,7 @@ describe('SSG data 404 - hard navigate when a new deployment occurs', () => { ...@@ -129,6 +132,7 @@ describe('SSG data 404 - hard navigate when a new deployment occurs', () => {
env: { env: {
DIST_DIR: '1', DIST_DIR: '1',
NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID1, NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID1,
OUTPUT_MODE,
}, },
}) })
) )
...@@ -193,4 +197,27 @@ describe('SSG data 404 - hard navigate when a new deployment occurs', () => { ...@@ -193,4 +197,27 @@ describe('SSG data 404 - hard navigate when a new deployment occurs', () => {
}) })
} }
) )
}
describe('header with deployment id', () => {
const { next } = nextTestSetup({
files: appDir,
env: {
// rely on skew protection when deployed
NEXT_DEPLOYMENT_ID: isNextDeploy ? undefined : 'test-deployment-id',
},
})
// Deployment skew is hard to properly e2e deploy test, so this just checks for the header
it('header is set on data routes', async () => {
for (const route of ['/gsp', '/gssp']) {
await next.fetch(route)
let res = await next.fetch(`/_next/data/${next.buildId}${route}.json`)
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toStartWith('application/json')
expect(res.headers.get('x-nextjs-deployment-id')).toBeTruthy()
}
})
})
}) })
...@@ -28,7 +28,6 @@ describe('Required Server Files', () => { ...@@ -28,7 +28,6 @@ describe('Required Server Files', () => {
let server: Awaited<ReturnType<typeof startApp>> let server: Awaited<ReturnType<typeof startApp>>
let appPort: number let appPort: number
let buildId: string
let requiredFilesManifest: any let requiredFilesManifest: any
// This suite unfortunately requires a very bespoke setup to set NEXT_PRIVATE_TEST_HEADERS and // This suite unfortunately requires a very bespoke setup to set NEXT_PRIVATE_TEST_HEADERS and
...@@ -37,8 +36,6 @@ describe('Required Server Files', () => { ...@@ -37,8 +36,6 @@ describe('Required Server Files', () => {
await next.build() await next.build()
await next.deleteFile('pages') await next.deleteFile('pages')
buildId = (await next.readFile(join('.next/BUILD_ID'))).trim()
requiredFilesManifest = await next.readJSON( requiredFilesManifest = await next.readJSON(
join('.next/required-server-files.json') join('.next/required-server-files.json')
) )
...@@ -196,7 +193,7 @@ describe('Required Server Files', () => { ...@@ -196,7 +193,7 @@ describe('Required Server Files', () => {
const { pageProps: data4 } = JSON.parse( const { pageProps: data4 } = JSON.parse(
await renderViaHTTP( await renderViaHTTP(
appPort, appPort,
`/_next/data/${buildId}/fallback/third.json`, `/_next/data/${next.buildId}/fallback/third.json`,
undefined, undefined,
withInvocationId() withInvocationId()
) )
...@@ -319,7 +316,7 @@ describe('Required Server Files', () => { ...@@ -319,7 +316,7 @@ describe('Required Server Files', () => {
it('should return data correctly with x-matched-path', async () => { it('should return data correctly with x-matched-path', async () => {
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
appPort, appPort,
`/_next/data/${buildId}/dynamic/first.json?nxtPslug=first`, `/_next/data/${next.buildId}/dynamic/first.json?nxtPslug=first`,
undefined, undefined,
withInvocationId({ withInvocationId({
headers: { headers: {
...@@ -335,11 +332,11 @@ describe('Required Server Files', () => { ...@@ -335,11 +332,11 @@ describe('Required Server Files', () => {
const res2 = await fetchViaHTTP( const res2 = await fetchViaHTTP(
appPort, appPort,
`/_next/data/${buildId}/fallback/[slug].json`, `/_next/data/${next.buildId}/fallback/[slug].json`,
undefined, undefined,
withInvocationId({ withInvocationId({
headers: { headers: {
'x-matched-path': `/_next/data/${buildId}/fallback/[slug].json`, 'x-matched-path': `/_next/data/${next.buildId}/fallback/[slug].json`,
'x-now-route-matches': 'nxtPslug=second', 'x-now-route-matches': 'nxtPslug=second',
}, },
}) })
...@@ -505,7 +502,7 @@ describe('Required Server Files', () => { ...@@ -505,7 +502,7 @@ describe('Required Server Files', () => {
it('should return data correctly with x-matched-path for optional catch-all route', async () => { it('should return data correctly with x-matched-path for optional catch-all route', async () => {
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
appPort, appPort,
`/_next/data/${buildId}/catch-all.json`, `/_next/data/${next.buildId}/catch-all.json`,
undefined, undefined,
withInvocationId({ withInvocationId({
headers: { headers: {
...@@ -521,11 +518,11 @@ describe('Required Server Files', () => { ...@@ -521,11 +518,11 @@ describe('Required Server Files', () => {
const res2 = await fetchViaHTTP( const res2 = await fetchViaHTTP(
appPort, appPort,
`/_next/data/${buildId}/catch-all/[[...rest]].json`, `/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
undefined, undefined,
withInvocationId({ withInvocationId({
headers: { headers: {
'x-matched-path': `/_next/data/${buildId}/catch-all/[[...rest]].json`, 'x-matched-path': `/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
'x-now-route-matches': 'nxtPrest=hello&rest=hello', 'x-now-route-matches': 'nxtPrest=hello&rest=hello',
}, },
}) })
...@@ -538,11 +535,11 @@ describe('Required Server Files', () => { ...@@ -538,11 +535,11 @@ describe('Required Server Files', () => {
const res3 = await fetchViaHTTP( const res3 = await fetchViaHTTP(
appPort, appPort,
`/_next/data/${buildId}/catch-all/[[...rest]].json`, `/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
undefined, undefined,
withInvocationId({ withInvocationId({
headers: { headers: {
'x-matched-path': `/_next/data/${buildId}/catch-all/[[...rest]].json`, 'x-matched-path': `/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
'x-now-route-matches': 'nxtPrest=hello/world&rest=hello/world', 'x-now-route-matches': 'nxtPrest=hello/world&rest=hello/world',
}, },
}) })
......
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