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 @@
"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.",
"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(
if (isGenerateMode) {
return await fs.readFile(path.join(distDir, BUILD_ID_FILE), 'utf8')
}
return await nextBuildSpan
.traceChild('generate-buildid')
.traceAsyncFn(() => generateBuildId(config.generateBuildId, nanoid))
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
.traceChild('generate-buildid')
.traceAsyncFn(() => generateBuildId(config.generateBuildId, nanoid))
}
}
export default async function build(
......
......@@ -760,22 +760,33 @@ export default async function loadCustomRoutes(
})
}
headers.unshift({
source: '/:path*',
has: [
{
type: 'header',
key: 'rsc',
value: '1',
},
],
headers: [
{
key: NEXT_NAV_DEPLOYMENT_ID_HEADER,
value: config.deploymentId,
},
],
})
headers.unshift(
{
source: '/:path*',
has: [
{
type: 'header',
key: 'rsc',
value: '1',
},
],
headers: [
{
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) {
......
......@@ -31,6 +31,7 @@ import {
CACHE_ONE_YEAR_SECONDS,
HTML_CONTENT_TYPE_HEADER,
JSON_CONTENT_TYPE_HEADER,
NEXT_NAV_DEPLOYMENT_ID_HEADER,
} from '../../../lib/constants'
import path from 'path'
import { sendRenderResult } from '../../send-payload'
......@@ -634,6 +635,10 @@ export const getHandler = ({
res.statusCode = 404
if (isNextDataRequest) {
const deploymentId = getDeploymentId()
if (deploymentId) {
res.setHeader(NEXT_NAV_DEPLOYMENT_ID_HEADER, deploymentId)
}
res.end('{"notFound":true}')
return
}
......@@ -642,6 +647,10 @@ export const getHandler = ({
if (result.value.kind === CachedRouteKind.REDIRECT) {
if (isNextDataRequest) {
const deploymentId = getDeploymentId()
if (deploymentId) {
res.setHeader(NEXT_NAV_DEPLOYMENT_ID_HEADER, deploymentId)
}
res.setHeader('content-type', JSON_CONTENT_TYPE_HEADER)
res.end(JSON.stringify(result.value.props))
return
......@@ -714,6 +723,14 @@ export const getHandler = ({
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({
req,
res,
......
......@@ -42,7 +42,10 @@ import { omit } from './utils/omit'
import { interpolateAs } from './utils/interpolate-as'
import { disableSmoothScrollDuringRouteTransition } from './utils/disable-smooth-scroll'
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 { isJavaScriptURLString } from '../../../client/lib/javascript-url'
......@@ -556,6 +559,25 @@ function fetchNextData({
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 {
dataHref,
json: parseJSON ? tryToParseAsJSON(text) : null,
......
/* eslint-env jest */
import { join } from 'path'
import http from 'http'
import httpProxy from 'http-proxy'
import webdriver from 'next-webdriver'
......@@ -11,8 +8,9 @@ import {
nextBuild,
nextStart,
} from 'next-test-utils'
import { isNextDeploy, isNextDev, isNextStart, nextTestSetup } from 'e2e-utils'
const appDir = join(__dirname, '..')
const appDir = __dirname
let proxyPort
......@@ -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') {
it('should skip this suite on Windows', () => {})
return
}
describe('development mode', () => {
let should404Data = false
let apps = []
let proxyServer
if (isNextDev) {
describe('development mode', () => {
let should404Data = false
let apps = []
let proxyServer
beforeAll(async () => {
const appPort = await findPort()
apps.push(await launchApp(appDir, appPort))
beforeAll(async () => {
const appPort = await findPort()
apps.push(await launchApp(appDir, appPort))
const proxy = httpProxy.createProxyServer({
target: `http://localhost:${appPort}`,
})
proxyPort = await findPort()
const proxy = httpProxy.createProxyServer({
target: `http://localhost:${appPort}`,
})
proxyPort = await findPort()
proxyServer = http.createServer((req, res) => {
req.on('error', (e) => {
require('console').error(e)
proxyServer = http.createServer((req, res) => {
req.on('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
return res.end('not found')
})
afterAll(async () => {
for (const app of apps) {
await killApp(app)
}
proxy.web(req, res)
proxyServer.close()
})
await new Promise<void>((resolve) => {
proxyServer.listen(proxyPort, () => resolve())
runTests((v) => {
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 () => {
await nextBuild(appDir, [], {
env: {
DIST_DIR: '1',
NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID1,
},
})
let appPort1 = await findPort()
apps.push(
await nextStart(appDir, appPort1, {
if (isNextStart) {
describe.each([
{ name: 'with build id' },
{
name: 'with deployment id',
NEXT_DEPLOYMENT_ID1: 'deployment-id-1',
NEXT_DEPLOYMENT_ID2: 'deployment-id-2',
},
{ name: 'with build id (output export)', OUTPUT_MODE: 'export' },
])(
'production mode $name',
({ NEXT_DEPLOYMENT_ID1, NEXT_DEPLOYMENT_ID2, OUTPUT_MODE }) => {
let shouldSwitchDeployment = false
let apps = []
let proxyServer
beforeAll(async () => {
await nextBuild(appDir, [], {
env: {
DIST_DIR: '1',
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, [], {
env: {
DIST_DIR: '2',
NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID2,
},
})
let appPort2 = await findPort()
apps.push(
await nextStart(appDir, appPort2, {
await nextBuild(appDir, [], {
env: {
DIST_DIR: '2',
NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID2,
},
})
)
const proxy1 = httpProxy.createProxyServer({
target: `http://localhost:${appPort1}`,
})
const proxy2 = httpProxy.createProxyServer({
target: `http://localhost:${appPort2}`,
})
proxyPort = await findPort()
let appPort2 = await findPort()
apps.push(
await nextStart(appDir, appPort2, {
env: {
DIST_DIR: '2',
NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID2,
},
})
)
proxyServer = http.createServer((req, res) => {
req.on('error', (e) => {
require('console').error(e)
const proxy1 = httpProxy.createProxyServer({
target: `http://localhost:${appPort1}`,
})
res.on('error', (e) => {
require('console').error(e)
const proxy2 = httpProxy.createProxyServer({
target: `http://localhost:${appPort2}`,
})
proxyPort = await findPort()
if (shouldSwitchDeployment) {
proxy2.web(req, res, undefined, (e) => {
proxyServer = http.createServer((req, res) => {
req.on('error', (e) => {
require('console').error(e)
})
res.on('error', (e) => {
require('console').error(e)
})
return
}
proxy1.web(req, res, undefined, (e) => {
require('console').error(e)
if (shouldSwitchDeployment) {
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) => {
proxyServer.listen(proxyPort, () => resolve())
runTests((v) => {
shouldSwitchDeployment = v
})
})
afterAll(async () => {
for (const app of apps) {
await killApp(app)
}
proxyServer.close()
})
}
)
}
runTests((v) => {
shouldSwitchDeployment = v
})
}
)
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', () => {
let server: Awaited<ReturnType<typeof startApp>>
let appPort: number
let buildId: string
let requiredFilesManifest: any
// This suite unfortunately requires a very bespoke setup to set NEXT_PRIVATE_TEST_HEADERS and
......@@ -37,8 +36,6 @@ describe('Required Server Files', () => {
await next.build()
await next.deleteFile('pages')
buildId = (await next.readFile(join('.next/BUILD_ID'))).trim()
requiredFilesManifest = await next.readJSON(
join('.next/required-server-files.json')
)
......@@ -196,7 +193,7 @@ describe('Required Server Files', () => {
const { pageProps: data4 } = JSON.parse(
await renderViaHTTP(
appPort,
`/_next/data/${buildId}/fallback/third.json`,
`/_next/data/${next.buildId}/fallback/third.json`,
undefined,
withInvocationId()
)
......@@ -319,7 +316,7 @@ describe('Required Server Files', () => {
it('should return data correctly with x-matched-path', async () => {
const res = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/dynamic/first.json?nxtPslug=first`,
`/_next/data/${next.buildId}/dynamic/first.json?nxtPslug=first`,
undefined,
withInvocationId({
headers: {
......@@ -335,11 +332,11 @@ describe('Required Server Files', () => {
const res2 = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/fallback/[slug].json`,
`/_next/data/${next.buildId}/fallback/[slug].json`,
undefined,
withInvocationId({
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',
},
})
......@@ -505,7 +502,7 @@ describe('Required Server Files', () => {
it('should return data correctly with x-matched-path for optional catch-all route', async () => {
const res = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/catch-all.json`,
`/_next/data/${next.buildId}/catch-all.json`,
undefined,
withInvocationId({
headers: {
......@@ -521,11 +518,11 @@ describe('Required Server Files', () => {
const res2 = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/catch-all/[[...rest]].json`,
`/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
undefined,
withInvocationId({
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',
},
})
......@@ -538,11 +535,11 @@ describe('Required Server Files', () => {
const res3 = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/catch-all/[[...rest]].json`,
`/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
undefined,
withInvocationId({
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',
},
})
......
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