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')
} }
return await nextBuildSpan if (config.deploymentId) {
.traceChild('generate-buildid') // Skew protection is enabled and NEXT_NAV_DEPLOYMENT_ID_HEADER will be used instead. Set a
.traceAsyncFn(() => generateBuildId(config.generateBuildId, nanoid)) // 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
.traceChild('generate-buildid')
.traceAsyncFn(() => generateBuildId(config.generateBuildId, nanoid))
}
} }
export default async function build( export default async function build(
......
...@@ -760,22 +760,33 @@ export default async function loadCustomRoutes( ...@@ -760,22 +760,33 @@ export default async function loadCustomRoutes(
}) })
} }
headers.unshift({ headers.unshift(
source: '/:path*', {
has: [ source: '/:path*',
{ has: [
type: 'header', {
key: 'rsc', type: 'header',
value: '1', key: 'rsc',
}, value: '1',
], },
headers: [ ],
{ headers: [
key: NEXT_NAV_DEPLOYMENT_ID_HEADER, {
value: config.deploymentId, key: NEXT_NAV_DEPLOYMENT_ID_HEADER,
}, 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,145 +50,174 @@ const runTests = (switchDeployment: (bool) => void) => { ...@@ -52,145 +50,174 @@ 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
} }
describe('development mode', () => { if (isNextDev) {
let should404Data = false describe('development mode', () => {
let apps = [] let should404Data = false
let proxyServer let apps = []
let proxyServer
beforeAll(async () => { beforeAll(async () => {
const appPort = await findPort() const appPort = await findPort()
apps.push(await launchApp(appDir, appPort)) apps.push(await launchApp(appDir, appPort))
const proxy = httpProxy.createProxyServer({ const proxy = httpProxy.createProxyServer({
target: `http://localhost:${appPort}`, target: `http://localhost:${appPort}`,
}) })
proxyPort = await findPort() proxyPort = await findPort()
proxyServer = http.createServer((req, res) => { proxyServer = http.createServer((req, res) => {
req.on('error', (e) => { req.on('error', (e) => {
require('console').error(e) require('console').error(e)
})
res.on('error', (e) => {
require('console').error(e)
})
if (should404Data && req.url.match(/\/_next\/data/)) {
res.statusCode = 404
return res.end('not found')
}
proxy.web(req, res)
}) })
res.on('error', (e) => {
require('console').error(e) await new Promise<void>((resolve) => {
proxyServer.listen(proxyPort, () => resolve())
}) })
if (should404Data && req.url.match(/\/_next\/data/)) { })
res.statusCode = 404 afterAll(async () => {
return res.end('not found') for (const app of apps) {
await killApp(app)
} }
proxy.web(req, res) proxyServer.close()
}) })
await new Promise<void>((resolve) => { runTests((v) => {
proxyServer.listen(proxyPort, () => resolve()) should404Data = v
}) })
}) })
afterAll(async () => { }
for (const app of apps) {
await killApp(app)
}
proxyServer.close()
})
runTests((v) => {
should404Data = v
})
})
describe.each([
{ name: 'with build id' },
{
name: 'with deployment id',
NEXT_DEPLOYMENT_ID1: 'deployment-id-1',
NEXT_DEPLOYMENT_ID2: 'deployment-id-2',
},
])(
'production mode $name',
({ NEXT_DEPLOYMENT_ID1, NEXT_DEPLOYMENT_ID2 }) => {
let shouldSwitchDeployment = false
let apps = []
let proxyServer
beforeAll(async () => { if (isNextStart) {
await nextBuild(appDir, [], { describe.each([
env: { { name: 'with build id' },
DIST_DIR: '1', {
NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID1, name: 'with deployment id',
}, NEXT_DEPLOYMENT_ID1: 'deployment-id-1',
}) NEXT_DEPLOYMENT_ID2: 'deployment-id-2',
let appPort1 = await findPort() },
apps.push( { name: 'with build id (output export)', OUTPUT_MODE: 'export' },
await nextStart(appDir, appPort1, { ])(
'production mode $name',
({ NEXT_DEPLOYMENT_ID1, NEXT_DEPLOYMENT_ID2, OUTPUT_MODE }) => {
let shouldSwitchDeployment = false
let apps = []
let proxyServer
beforeAll(async () => {
await nextBuild(appDir, [], {
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()
apps.push(
await nextStart(appDir, appPort1, {
env: {
DIST_DIR: '1',
NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID1,
OUTPUT_MODE,
},
})
)
await nextBuild(appDir, [], { await nextBuild(appDir, [], {
env: {
DIST_DIR: '2',
NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID2,
},
})
let appPort2 = await findPort()
apps.push(
await nextStart(appDir, appPort2, {
env: { env: {
DIST_DIR: '2', DIST_DIR: '2',
NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID2, NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID2,
}, },
}) })
) let appPort2 = await findPort()
apps.push(
const proxy1 = httpProxy.createProxyServer({ await nextStart(appDir, appPort2, {
target: `http://localhost:${appPort1}`, env: {
}) DIST_DIR: '2',
const proxy2 = httpProxy.createProxyServer({ NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID2,
target: `http://localhost:${appPort2}`, },
}) })
proxyPort = await findPort() )
proxyServer = http.createServer((req, res) => { const proxy1 = httpProxy.createProxyServer({
req.on('error', (e) => { target: `http://localhost:${appPort1}`,
require('console').error(e)
}) })
res.on('error', (e) => { const proxy2 = httpProxy.createProxyServer({
require('console').error(e) target: `http://localhost:${appPort2}`,
}) })
proxyPort = await findPort()
if (shouldSwitchDeployment) { proxyServer = http.createServer((req, res) => {
proxy2.web(req, res, undefined, (e) => { req.on('error', (e) => {
require('console').error(e)
})
res.on('error', (e) => {
require('console').error(e) require('console').error(e)
}) })
return
}
proxy1.web(req, res, undefined, (e) => { if (shouldSwitchDeployment) {
require('console').error(e) proxy2.web(req, res, undefined, (e) => {
require('console').error(e)
})
return
}
proxy1.web(req, res, undefined, (e) => {
require('console').error(e)
})
}) })
await new Promise<void>((resolve) => {
proxyServer.listen(proxyPort, () => resolve())
})
})
afterAll(async () => {
for (const app of apps) {
await killApp(app)
}
proxyServer.close()
}) })
await new Promise<void>((resolve) => { runTests((v) => {
proxyServer.listen(proxyPort, () => resolve()) shouldSwitchDeployment = v
}) })
}) }
afterAll(async () => { )
for (const app of apps) { }
await killApp(app)
}
proxyServer.close()
})
runTests((v) => { describe('header with deployment id', () => {
shouldSwitchDeployment = v 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