Unverified Commit 6766a270 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Replace build id in Pages data routes with deployment id (#88959)

Closes PACK-6537

When a deployment id is available, don't put the data routes at `_next/data/<BUILDID>/page.json`, but at `_next/data/page.json`.
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.


If you use `output:export` and set `config.deploymentId` (i.e. enable skew protection) you are still expected to set these headers.

---

- [x] Find the right place where the header should be added for prerendered static RSC responses (route manifest)
parent a6f43994
...@@ -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"
} }
...@@ -2791,7 +2791,7 @@ export default async function build( ...@@ -2791,7 +2791,7 @@ export default async function build(
...serverPropsPages, ...serverPropsPages,
...ssgPages, ...ssgPages,
]).map((page) => { ]).map((page) => {
return buildDataRoute(page, buildId) return buildDataRoute(page, buildId, config.deploymentId)
}) })
} }
...@@ -3876,7 +3876,9 @@ export default async function build( ...@@ -3876,7 +3876,9 @@ export default async function build(
experimentalPPR: undefined, experimentalPPR: undefined,
renderingMode: undefined, renderingMode: undefined,
srcRoute: null, srcRoute: null,
dataRoute: path.posix.join( dataRoute: config.deploymentId
? path.posix.join('/_next/data', `${localePage}.json`)
: path.posix.join(
'/_next/data', '/_next/data',
buildId, buildId,
`${localePage}.json` `${localePage}.json`
...@@ -3894,11 +3896,9 @@ export default async function build( ...@@ -3894,11 +3896,9 @@ export default async function build(
experimentalPPR: undefined, experimentalPPR: undefined,
renderingMode: undefined, renderingMode: undefined,
srcRoute: null, srcRoute: null,
dataRoute: path.posix.join( dataRoute: config.deploymentId
'/_next/data', ? path.posix.join('/_next/data', `${file}.json`)
buildId, : path.posix.join('/_next/data', buildId, `${file}.json`),
`${file}.json`
),
// Pages does not have a prefetch data route. // Pages does not have a prefetch data route.
prefetchDataRoute: undefined, prefetchDataRoute: undefined,
allowHeader: ALLOWED_HEADERS, allowHeader: ALLOWED_HEADERS,
...@@ -3939,7 +3939,12 @@ export default async function build( ...@@ -3939,7 +3939,12 @@ export default async function build(
experimentalPPR: undefined, experimentalPPR: undefined,
renderingMode: undefined, renderingMode: undefined,
srcRoute: page, srcRoute: page,
dataRoute: path.posix.join( dataRoute: config.deploymentId
? path.posix.join(
'/_next/data',
`${normalizePagePath(route.pathname)}.json`
)
: path.posix.join(
'/_next/data', '/_next/data',
buildId, buildId,
`${normalizePagePath(route.pathname)}.json` `${normalizePagePath(route.pathname)}.json`
...@@ -4052,11 +4057,9 @@ export default async function build( ...@@ -4052,11 +4057,9 @@ export default async function build(
if (ssgPages.size > 0 || appDir) { if (ssgPages.size > 0 || appDir) {
tbdPrerenderRoutes.forEach((tbdRoute) => { tbdPrerenderRoutes.forEach((tbdRoute) => {
const normalizedRoute = normalizePagePath(tbdRoute) const normalizedRoute = normalizePagePath(tbdRoute)
const dataRoute = path.posix.join( const dataRoute = config.deploymentId
'/_next/data', ? path.posix.join('/_next/data', `${normalizedRoute}.json`)
buildId, : path.posix.join('/_next/data', buildId, `${normalizedRoute}.json`)
`${normalizedRoute}.json`
)
prerenderManifest.dynamicRoutes[tbdRoute] = { prerenderManifest.dynamicRoutes[tbdRoute] = {
routeRegex: normalizeRouteRegex( routeRegex: normalizeRouteRegex(
......
...@@ -18,6 +18,7 @@ import { ...@@ -18,6 +18,7 @@ import {
DEV_CLIENT_MIDDLEWARE_MANIFEST, DEV_CLIENT_MIDDLEWARE_MANIFEST,
} from '../shared/lib/constants' } from '../shared/lib/constants'
import { resolvePromiseWithTimeout } from './lib/promise' import { resolvePromiseWithTimeout } from './lib/promise'
import { getDeploymentId } from '../shared/lib/deployment-id'
declare global { declare global {
interface Window { interface Window {
...@@ -170,8 +171,12 @@ export default class PageLoader { ...@@ -170,8 +171,12 @@ export default class PageLoader {
removeTrailingSlash(addLocale(path, locale)), removeTrailingSlash(addLocale(path, locale)),
'.json' '.json'
) )
// We check the response header instead
return addBasePath( return addBasePath(
`/_next/data/${this.buildId}${dataRoute}${search}`, getDeploymentId()
? `/_next/data${dataRoute}${search}`
: `/_next/data/${this.buildId}${dataRoute}${search}`,
true true
) )
} }
......
...@@ -19,6 +19,8 @@ export const NEXT_DATA_SUFFIX = '.json' ...@@ -19,6 +19,8 @@ export const NEXT_DATA_SUFFIX = '.json'
export const NEXT_META_SUFFIX = '.meta' export const NEXT_META_SUFFIX = '.meta'
export const NEXT_BODY_SUFFIX = '.body' export const NEXT_BODY_SUFFIX = '.body'
export const NEXT_NAV_DEPLOYMENT_ID_HEADER = 'x-nextjs-deployment-id'
export const NEXT_CACHE_TAGS_HEADER = 'x-next-cache-tags' export const NEXT_CACHE_TAGS_HEADER = 'x-next-cache-tags'
export const NEXT_CACHE_REVALIDATED_TAGS_HEADER = 'x-next-revalidated-tags' export const NEXT_CACHE_REVALIDATED_TAGS_HEADER = 'x-next-revalidated-tags'
export const NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER = export const NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER =
......
...@@ -6,6 +6,7 @@ import { escapeStringRegexp } from '../shared/lib/escape-regexp' ...@@ -6,6 +6,7 @@ import { escapeStringRegexp } from '../shared/lib/escape-regexp'
import { tryToParsePath } from './try-to-parse-path' import { tryToParsePath } from './try-to-parse-path'
import { allowedStatusCodes } from './redirect-status' import { allowedStatusCodes } from './redirect-status'
import { isFullStringUrl } from './url' import { isFullStringUrl } from './url'
import { NEXT_NAV_DEPLOYMENT_ID_HEADER } from './constants'
export type RouteHas = export type RouteHas =
| { | {
...@@ -746,7 +747,8 @@ export default async function loadCustomRoutes( ...@@ -746,7 +747,8 @@ export default async function loadCustomRoutes(
) )
} }
if (config.experimental?.useSkewCookie && config.deploymentId) { if (config.deploymentId) {
if (config.experimental?.useSkewCookie) {
headers.unshift({ headers.unshift({
source: '/:path*', source: '/:path*',
headers: [ headers: [
...@@ -758,6 +760,17 @@ export default async function loadCustomRoutes( ...@@ -758,6 +760,17 @@ export default async function loadCustomRoutes(
}) })
} }
headers.unshift({
source: '/_next/data/(.*)',
headers: [
{
key: NEXT_NAV_DEPLOYMENT_ID_HEADER,
value: config.deploymentId,
},
],
})
}
if (!config.skipTrailingSlashRedirect) { if (!config.skipTrailingSlashRedirect) {
if (config.trailingSlash) { if (config.trailingSlash) {
redirects.unshift( redirects.unshift(
......
...@@ -524,7 +524,7 @@ export default abstract class Server< ...@@ -524,7 +524,7 @@ export default abstract class Server<
? new SegmentPrefixRSCPathnameNormalizer() ? new SegmentPrefixRSCPathnameNormalizer()
: undefined, : undefined,
data: this.enabledDirectories.pages data: this.enabledDirectories.pages
? new NextDataPathnameNormalizer(this.buildId) ? new NextDataPathnameNormalizer(this.buildId, this.deploymentId)
: undefined, : undefined,
} }
...@@ -680,6 +680,7 @@ export default abstract class Server< ...@@ -680,6 +680,7 @@ export default abstract class Server<
return false return false
} }
if (!this.nextConfig.deploymentId) {
if (params.path[0] !== this.buildId) { if (params.path[0] !== this.buildId) {
// Ignore if its a middleware request when we aren't on edge. // Ignore if its a middleware request when we aren't on edge.
if (getRequestMeta(req, 'middlewareInvoke')) { if (getRequestMeta(req, 'middlewareInvoke')) {
...@@ -693,6 +694,7 @@ export default abstract class Server< ...@@ -693,6 +694,7 @@ export default abstract class Server<
// remove buildId from URL // remove buildId from URL
params.path.shift() params.path.shift()
}
const lastParam = params.path[params.path.length - 1] const lastParam = params.path[params.path.length - 1]
...@@ -2440,13 +2442,12 @@ export default abstract class Server< ...@@ -2440,13 +2442,12 @@ export default abstract class Server<
} }
private stripNextDataPath(filePath: string, stripLocale = true) { private stripNextDataPath(filePath: string, stripLocale = true) {
if (filePath.includes(this.buildId)) { if (this.deploymentId) {
const splitPath = filePath.substring( filePath = filePath.replace(/\/_next\/data/, '')
filePath.indexOf(this.buildId) + this.buildId.length } else {
) filePath = filePath.replace(/\/_next\/data\/[^/]+/, '')
filePath = denormalizePagePath(splitPath.replace(/\.json$/, ''))
} }
filePath = filePath.replace(/\.json$/, '')
if (this.localeNormalizer && stripLocale) { if (this.localeNormalizer && stripLocale) {
return this.localeNormalizer.normalize(filePath) return this.localeNormalizer.normalize(filePath)
......
import { buildDataRoute } from './build-data-route' import { buildDataRoute } from './build-data-route'
describe('buildDataRoute', () => { describe('buildDataRoute', () => {
describe('with build id', () => {
it('should build a dynamic data route', () => { it('should build a dynamic data route', () => {
const dataRoute = buildDataRoute('/[...slug]', '123') const dataRoute = buildDataRoute('/[...slug]', '123', '')
expect(dataRoute).toMatchInlineSnapshot(` expect(dataRoute).toMatchInlineSnapshot(`
{ {
"dataRouteRegex": "^/_next/data/123/(.+?)\\.json$", "dataRouteRegex": "^/_next/data/123/(.+?)\\.json$",
...@@ -16,7 +17,7 @@ describe('buildDataRoute', () => { ...@@ -16,7 +17,7 @@ describe('buildDataRoute', () => {
}) })
it('should build a static data route', () => { it('should build a static data route', () => {
const dataRoute = buildDataRoute('/about', '123') const dataRoute = buildDataRoute('/about', '123', '')
expect(dataRoute).toMatchInlineSnapshot(` expect(dataRoute).toMatchInlineSnapshot(`
{ {
"dataRouteRegex": "^/_next/data/123/about\\.json$", "dataRouteRegex": "^/_next/data/123/about\\.json$",
...@@ -26,4 +27,33 @@ describe('buildDataRoute', () => { ...@@ -26,4 +27,33 @@ describe('buildDataRoute', () => {
} }
`) `)
}) })
})
describe('with deployment id', () => {
it('should build a dynamic data route', () => {
const dataRoute = buildDataRoute('/[...slug]', '123', 'ABCDEF')
expect(dataRoute).toMatchInlineSnapshot(`
{
"dataRouteRegex": "^/_next/data/(.+?)\\.json$",
"namedDataRouteRegex": "^/_next/data/(?<nxtPslug>.+?)\\.json$",
"page": "/[...slug]",
"routeKeys": {
"nxtPslug": "nxtPslug",
},
}
`)
})
it('should build a static data route', () => {
const dataRoute = buildDataRoute('/about', '123', 'ABCDEF')
expect(dataRoute).toMatchInlineSnapshot(`
{
"dataRouteRegex": "^/_next/data/about\\.json$",
"namedDataRouteRegex": undefined,
"page": "/about",
"routeKeys": undefined,
}
`)
})
})
}) })
...@@ -5,9 +5,15 @@ import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex ...@@ -5,9 +5,15 @@ import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex
import { normalizeRouteRegex } from '../../../lib/load-custom-routes' import { normalizeRouteRegex } from '../../../lib/load-custom-routes'
import { escapeStringRegexp } from '../../../shared/lib/escape-regexp' import { escapeStringRegexp } from '../../../shared/lib/escape-regexp'
export function buildDataRoute(page: string, buildId: string) { export function buildDataRoute(
page: string,
buildId: string,
deploymentId: string
) {
const pagePath = normalizePagePath(page) const pagePath = normalizePagePath(page)
const dataRoute = path.posix.join('/_next/data', buildId, `${pagePath}.json`) const dataRoute = deploymentId
? path.posix.join('/_next/data', `${pagePath}.json`)
: path.posix.join('/_next/data', buildId, `${pagePath}.json`)
let dataRouteRegex: string let dataRouteRegex: string
let namedDataRouteRegex: string | undefined let namedDataRouteRegex: string | undefined
...@@ -26,11 +32,15 @@ export function buildDataRoute(page: string, buildId: string) { ...@@ -26,11 +32,15 @@ export function buildDataRoute(page: string, buildId: string) {
} else { } else {
dataRouteRegex = normalizeRouteRegex( dataRouteRegex = normalizeRouteRegex(
new RegExp( new RegExp(
`^${path.posix.join( `^${
deploymentId
? path.posix.join('/_next/data', `${pagePath}\\.json`)
: path.posix.join(
'/_next/data', '/_next/data',
escapeStringRegexp(buildId), escapeStringRegexp(buildId),
`${pagePath}\\.json` `${pagePath}\\.json`
)}$` )
}$`
).source ).source
) )
} }
......
...@@ -283,9 +283,15 @@ export async function setupFsCheck(opts: { ...@@ -283,9 +283,15 @@ export async function setupFsCheck(opts: {
// upstream builder that relies on this // upstream builder that relies on this
re: opts.config.i18n re: opts.config.i18n
? new RegExp( ? new RegExp(
route.dataRouteRegex.replace( opts.config.deploymentId ||
`/${escapedBuildId}/`, opts.config.experimental.runtimeServerDeploymentId
`/${escapedBuildId}/(?<nextLocale>[^/]+?)/` ? route.dataRouteRegex.replace(
`/_next/data/`,
`/_next/data/(?<nextLocale>[^/]+?)/`
)
: route.dataRouteRegex.replace(
`/_next/data/${escapedBuildId}/`,
`/_next/data/${escapedBuildId}/(?<nextLocale>[^/]+?)/`
) )
) )
: new RegExp(route.dataRouteRegex), : new RegExp(route.dataRouteRegex),
...@@ -574,7 +580,12 @@ export async function setupFsCheck(opts: { ...@@ -574,7 +580,12 @@ export async function setupFsCheck(opts: {
continue continue
} }
const nextDataPrefix = `/_next/data/${buildId}/` // When deploymentId is set, data URLs don't include the build ID
const nextDataPrefix =
opts.config.deploymentId ||
opts.config.experimental.runtimeServerDeploymentId
? '/_next/data/'
: `/_next/data/${buildId}/`
if ( if (
type === 'pageFile' && type === 'pageFile' &&
...@@ -582,7 +593,7 @@ export async function setupFsCheck(opts: { ...@@ -582,7 +593,7 @@ export async function setupFsCheck(opts: {
curItemPath.endsWith('.json') curItemPath.endsWith('.json')
) { ) {
items = nextDataRoutes items = nextDataRoutes
// remove _next/data/<build-id> prefix // remove _next/data/ or _next/data/<build-id>/ prefix
curItemPath = curItemPath.substring(nextDataPrefix.length - 1) curItemPath = curItemPath.substring(nextDataPrefix.length - 1)
// remove .json postfix // remove .json postfix
......
...@@ -331,7 +331,10 @@ export function getResolveRoutes( ...@@ -331,7 +331,10 @@ export function getResolveRoutes(
config.basePath && config.basePath !== '/' config.basePath && config.basePath !== '/'
? new BasePathPathnameNormalizer(config.basePath) ? new BasePathPathnameNormalizer(config.basePath)
: undefined, : undefined,
data: new NextDataPathnameNormalizer(fsChecker.buildId), data: new NextDataPathnameNormalizer(
fsChecker.buildId,
config.deploymentId || ''
),
} }
async function handleRoute( async function handleRoute(
......
...@@ -1088,7 +1088,11 @@ async function startWatcher( ...@@ -1088,7 +1088,11 @@ async function startWatcher(
const dataRoutes: typeof opts.fsChecker.dynamicRoutes = [] const dataRoutes: typeof opts.fsChecker.dynamicRoutes = []
for (const page of sortedRoutes) { for (const page of sortedRoutes) {
const route = buildDataRoute(page, 'development') const route = buildDataRoute(
page,
'development',
nextConfig.deploymentId
)
const routeRegex = getNamedRouteRegex(route.page, { const routeRegex = getNamedRouteRegex(route.page, {
prefixRouteKeys: true, prefixRouteKeys: true,
}) })
......
...@@ -4,14 +4,18 @@ describe('NextDataPathnameNormalizer', () => { ...@@ -4,14 +4,18 @@ describe('NextDataPathnameNormalizer', () => {
describe('constructor', () => { describe('constructor', () => {
it('should error when no buildID is provided', () => { it('should error when no buildID is provided', () => {
expect(() => { expect(() => {
new NextDataPathnameNormalizer('') new NextDataPathnameNormalizer('', '')
}).toThrowErrorMatchingInlineSnapshot(`"Invariant: buildID is required"`) }).toThrowErrorMatchingInlineSnapshot(`"Invariant: buildID is required"`)
}) })
}) })
describe.each([
{ name: 'with build id', buildId: 'build-id', deploymentId: '' },
{ name: 'with deployment id', buildId: 'build-id', deploymentId: 'dpl-id' },
])('$name', ({ buildId, deploymentId }) => {
describe('match', () => { describe('match', () => {
it('should return false if the pathname does not start with the prefix', () => { it('should return false if the pathname does not start with the prefix', () => {
const normalizer = new NextDataPathnameNormalizer('build-id') const normalizer = new NextDataPathnameNormalizer(buildId, deploymentId)
const pathnames = ['/foo', '/foo/bar', '/fooo/bar'] const pathnames = ['/foo', '/foo/bar', '/fooo/bar']
for (const pathname of pathnames) { for (const pathname of pathnames) {
expect(normalizer.match(pathname)).toBe(false) expect(normalizer.match(pathname)).toBe(false)
...@@ -19,15 +23,16 @@ describe('NextDataPathnameNormalizer', () => { ...@@ -19,15 +23,16 @@ describe('NextDataPathnameNormalizer', () => {
}) })
it('should return false if the pathname only ends with `.json`', () => { it('should return false if the pathname only ends with `.json`', () => {
const normalizer = new NextDataPathnameNormalizer('build-id') const normalizer = new NextDataPathnameNormalizer(buildId, deploymentId)
const pathnames = ['/foo.json', '/foo/bar.json', '/fooo/bar.json'] const pathnames = ['/foo.json', '/foo/bar.json', '/fooo/bar.json']
for (const pathname of pathnames) { for (const pathname of pathnames) {
expect(normalizer.match(pathname)).toBe(false) expect(normalizer.match(pathname)).toBe(false)
} }
}) })
})
it('should return true if it matches', () => { it('should return true if it matches', () => {
const normalizer = new NextDataPathnameNormalizer('build-id') const normalizer = new NextDataPathnameNormalizer(buildId, deploymentId)
const pathnames = [ const pathnames = [
'/_next/data/build-id/index.json', '/_next/data/build-id/index.json',
'/_next/data/build-id/foo.json', '/_next/data/build-id/foo.json',
...@@ -38,11 +43,10 @@ describe('NextDataPathnameNormalizer', () => { ...@@ -38,11 +43,10 @@ describe('NextDataPathnameNormalizer', () => {
expect(normalizer.match(pathname)).toBe(true) expect(normalizer.match(pathname)).toBe(true)
} }
}) })
})
describe('normalize', () => { describe('normalize', () => {
it('should return the same pathname if we are not matched and the pathname does not start with the prefix', () => { it('should return the same pathname if we are not matched and the pathname does not start with the prefix', () => {
const normalizer = new NextDataPathnameNormalizer('build-id') const normalizer = new NextDataPathnameNormalizer(buildId, deploymentId)
const pathnames = ['/foo', '/foo/bar', '/fooo/bar'] const pathnames = ['/foo', '/foo/bar', '/fooo/bar']
for (const pathname of pathnames) { for (const pathname of pathnames) {
expect(normalizer.normalize(pathname)).toBe(pathname) expect(normalizer.normalize(pathname)).toBe(pathname)
...@@ -50,8 +54,14 @@ describe('NextDataPathnameNormalizer', () => { ...@@ -50,8 +54,14 @@ describe('NextDataPathnameNormalizer', () => {
}) })
it('should strip the prefix and the `.json` extension from the pathname when it matches', () => { it('should strip the prefix and the `.json` extension from the pathname when it matches', () => {
const normalizer = new NextDataPathnameNormalizer('build-id') const normalizer = new NextDataPathnameNormalizer(buildId, deploymentId)
const pathnames = [ const pathnames = deploymentId
? [
'/_next/data/foo.json',
'/_next/data/foo/bar.json',
'/_next/data/fooo/bar.json',
]
: [
'/_next/data/build-id/foo.json', '/_next/data/build-id/foo.json',
'/_next/data/build-id/foo/bar.json', '/_next/data/build-id/foo/bar.json',
'/_next/data/build-id/fooo/bar.json', '/_next/data/build-id/fooo/bar.json',
...@@ -59,7 +69,9 @@ describe('NextDataPathnameNormalizer', () => { ...@@ -59,7 +69,9 @@ describe('NextDataPathnameNormalizer', () => {
for (const pathname of pathnames) { for (const pathname of pathnames) {
expect(normalizer.normalize(pathname)).toBe( expect(normalizer.normalize(pathname)).toBe(
pathname.substring( pathname.substring(
'/_next/data/build-id'.length, deploymentId
? '/_next/data'.length
: '/_next/data/build-id'.length,
pathname.length - '.json'.length pathname.length - '.json'.length
) )
) )
...@@ -67,8 +79,15 @@ describe('NextDataPathnameNormalizer', () => { ...@@ -67,8 +79,15 @@ describe('NextDataPathnameNormalizer', () => {
}) })
it('should normalize `/index` to `/`', () => { it('should normalize `/index` to `/`', () => {
const normalizer = new NextDataPathnameNormalizer('build-id') const normalizer = new NextDataPathnameNormalizer(buildId, deploymentId)
expect(normalizer.normalize('/_next/data/build-id/index.json')).toBe('/') expect(
normalizer.normalize(
deploymentId
? `/_next/data/index.json`
: `/_next/data/build-id/index.json`
)
).toBe('/')
})
}) })
}) })
}) })
...@@ -7,12 +7,15 @@ import { SuffixPathnameNormalizer } from './suffix' ...@@ -7,12 +7,15 @@ import { SuffixPathnameNormalizer } from './suffix'
export class NextDataPathnameNormalizer implements PathnameNormalizer { export class NextDataPathnameNormalizer implements PathnameNormalizer {
private readonly prefix: PrefixPathnameNormalizer private readonly prefix: PrefixPathnameNormalizer
private readonly suffix = new SuffixPathnameNormalizer('.json') private readonly suffix = new SuffixPathnameNormalizer('.json')
constructor(buildID: string) { constructor(buildID: string, deploymentId: string) {
if (!buildID) { if (!buildID) {
throw new Error('Invariant: buildID is required') throw new Error('Invariant: buildID is required')
} }
this.prefix = new PrefixPathnameNormalizer(`/_next/data/${buildID}`) // When deploymentId is set, data URLs don't include the build ID
this.prefix = new PrefixPathnameNormalizer(
deploymentId ? '/_next/data' : `/_next/data/${buildID}`
)
} }
public match(pathname: string) { public match(pathname: string) {
......
...@@ -31,6 +31,7 @@ import { ...@@ -31,6 +31,7 @@ import {
CACHE_ONE_YEAR, CACHE_ONE_YEAR,
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,
......
import { pathHasPrefix } from '../router/utils/path-has-prefix' import { pathHasPrefix } from '../router/utils/path-has-prefix'
import { getDeploymentId } from '../deployment-id'
/** /**
* strip _next/data/<build-id>/ prefix and .json suffix * strip _next/data/(<build-id>/) prefix and .json suffix
* When deploymentId is set, the path does NOT contain build-id.
*/ */
export function normalizeDataPath(pathname: string) { export function normalizeDataPath(pathname: string) {
if (!pathHasPrefix(pathname || '/', '/_next/data')) { if (!pathHasPrefix(pathname || '/', '/_next/data')) {
return pathname return pathname
} }
pathname = pathname
.replace(/\/_next\/data\/[^/]{1,}/, '') if (getDeploymentId()) {
.replace(/\.json$/, '') pathname = pathname.replace(/\/_next\/data/, '')
} else {
pathname = pathname.replace(/\/_next\/data\/[^/]+/, '')
}
pathname = pathname.replace(/\.json$/, '')
if (pathname === '/index') { if (pathname === '/index') {
return '/' return '/'
......
...@@ -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${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