Unverified Commit 303e38ec authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

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

`test/e2e/middleware-redirects` has 404s on the data route
parent 994406ea
...@@ -986,6 +986,5 @@ ...@@ -986,6 +986,5 @@
"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, config.deploymentId) return buildDataRoute(page, buildId)
}) })
} }
...@@ -3876,13 +3876,11 @@ export default async function build( ...@@ -3876,13 +3876,11 @@ export default async function build(
experimentalPPR: undefined, experimentalPPR: undefined,
renderingMode: undefined, renderingMode: undefined,
srcRoute: null, srcRoute: null,
dataRoute: config.deploymentId dataRoute: path.posix.join(
? path.posix.join('/_next/data', `${localePage}.json`) '/_next/data',
: path.posix.join( buildId,
'/_next/data', `${localePage}.json`
buildId, ),
`${localePage}.json`
),
prefetchDataRoute: undefined, prefetchDataRoute: undefined,
allowHeader: ALLOWED_HEADERS, allowHeader: ALLOWED_HEADERS,
} }
...@@ -3896,9 +3894,11 @@ export default async function build( ...@@ -3896,9 +3894,11 @@ export default async function build(
experimentalPPR: undefined, experimentalPPR: undefined,
renderingMode: undefined, renderingMode: undefined,
srcRoute: null, srcRoute: null,
dataRoute: config.deploymentId dataRoute: path.posix.join(
? path.posix.join('/_next/data', `${file}.json`) '/_next/data',
: path.posix.join('/_next/data', buildId, `${file}.json`), buildId,
`${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,16 +3939,11 @@ export default async function build( ...@@ -3939,16 +3939,11 @@ export default async function build(
experimentalPPR: undefined, experimentalPPR: undefined,
renderingMode: undefined, renderingMode: undefined,
srcRoute: page, srcRoute: page,
dataRoute: config.deploymentId dataRoute: path.posix.join(
? path.posix.join( '/_next/data',
'/_next/data', buildId,
`${normalizePagePath(route.pathname)}.json` `${normalizePagePath(route.pathname)}.json`
) ),
: path.posix.join(
'/_next/data',
buildId,
`${normalizePagePath(route.pathname)}.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,
...@@ -4057,9 +4052,11 @@ export default async function build( ...@@ -4057,9 +4052,11 @@ 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 = config.deploymentId const dataRoute = path.posix.join(
? path.posix.join('/_next/data', `${normalizedRoute}.json`) '/_next/data',
: path.posix.join('/_next/data', buildId, `${normalizedRoute}.json`) buildId,
`${normalizedRoute}.json`
)
prerenderManifest.dynamicRoutes[tbdRoute] = { prerenderManifest.dynamicRoutes[tbdRoute] = {
routeRegex: normalizeRouteRegex( routeRegex: normalizeRouteRegex(
......
...@@ -18,7 +18,6 @@ import { ...@@ -18,7 +18,6 @@ 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 {
...@@ -171,12 +170,8 @@ export default class PageLoader { ...@@ -171,12 +170,8 @@ export default class PageLoader {
removeTrailingSlash(addLocale(path, locale)), removeTrailingSlash(addLocale(path, locale)),
'.json' '.json'
) )
// We check the response header instead
return addBasePath( return addBasePath(
getDeploymentId() `/_next/data/${this.buildId}${dataRoute}${search}`,
? `/_next/data${dataRoute}${search}`
: `/_next/data/${this.buildId}${dataRoute}${search}`,
true true
) )
} }
......
...@@ -19,8 +19,6 @@ export const NEXT_DATA_SUFFIX = '.json' ...@@ -19,8 +19,6 @@ 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,7 +6,6 @@ import { escapeStringRegexp } from '../shared/lib/escape-regexp' ...@@ -6,7 +6,6 @@ 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 =
| { | {
...@@ -747,25 +746,13 @@ export default async function loadCustomRoutes( ...@@ -747,25 +746,13 @@ export default async function loadCustomRoutes(
) )
} }
if (config.deploymentId) { if (config.experimental?.useSkewCookie && config.deploymentId) {
if (config.experimental?.useSkewCookie) {
headers.unshift({
source: '/:path*',
headers: [
{
key: 'Set-Cookie',
value: `__vdpl=${config.deploymentId}; Path=/; HttpOnly`,
},
],
})
}
headers.unshift({ headers.unshift({
source: '/_next/data/(.*)', source: '/:path*',
headers: [ headers: [
{ {
key: NEXT_NAV_DEPLOYMENT_ID_HEADER, key: 'Set-Cookie',
value: config.deploymentId, value: `__vdpl=${config.deploymentId}; Path=/; HttpOnly`,
}, },
], ],
}) })
......
...@@ -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, this.deploymentId) ? new NextDataPathnameNormalizer(this.buildId)
: undefined, : undefined,
} }
...@@ -680,22 +680,20 @@ export default abstract class Server< ...@@ -680,22 +680,20 @@ 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')) { return false
return false
}
// Make sure to 404 if the buildId isn't correct
await this.render404(req, res, parsedUrl)
return true
} }
// remove buildId from URL // Make sure to 404 if the buildId isn't correct
params.path.shift() await this.render404(req, res, parsedUrl)
return true
} }
// remove buildId from URL
params.path.shift()
const lastParam = params.path[params.path.length - 1] const lastParam = params.path[params.path.length - 1]
// show 404 if it doesn't end with .json // show 404 if it doesn't end with .json
...@@ -2442,12 +2440,13 @@ export default abstract class Server< ...@@ -2442,12 +2440,13 @@ export default abstract class Server<
} }
private stripNextDataPath(filePath: string, stripLocale = true) { private stripNextDataPath(filePath: string, stripLocale = true) {
if (this.deploymentId) { if (filePath.includes(this.buildId)) {
filePath = filePath.replace(/\/_next\/data/, '') const splitPath = filePath.substring(
} else { filePath.indexOf(this.buildId) + this.buildId.length
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$", "namedDataRouteRegex": "^/_next/data/123/(?<nxtPslug>.+?)\\.json$",
"namedDataRouteRegex": "^/_next/data/123/(?<nxtPslug>.+?)\\.json$", "page": "/[...slug]",
"page": "/[...slug]", "routeKeys": {
"routeKeys": { "nxtPslug": "nxtPslug",
"nxtPslug": "nxtPslug", },
}, }
} `)
`)
})
it('should build a static data route', () => {
const dataRoute = buildDataRoute('/about', '123', '')
expect(dataRoute).toMatchInlineSnapshot(`
{
"dataRouteRegex": "^/_next/data/123/about\\.json$",
"namedDataRouteRegex": undefined,
"page": "/about",
"routeKeys": undefined,
}
`)
})
}) })
describe('with deployment id', () => { it('should build a static data route', () => {
it('should build a dynamic data route', () => { const dataRoute = buildDataRoute('/about', '123')
const dataRoute = buildDataRoute('/[...slug]', '123', 'ABCDEF') expect(dataRoute).toMatchInlineSnapshot(`
expect(dataRoute).toMatchInlineSnapshot(` {
{ "dataRouteRegex": "^/_next/data/123/about\\.json$",
"dataRouteRegex": "^/_next/data/(.+?)\\.json$", "namedDataRouteRegex": undefined,
"namedDataRouteRegex": "^/_next/data/(?<nxtPslug>.+?)\\.json$", "page": "/about",
"page": "/[...slug]", "routeKeys": undefined,
"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,15 +5,9 @@ import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex ...@@ -5,15 +5,9 @@ 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( export function buildDataRoute(page: string, buildId: string) {
page: string,
buildId: string,
deploymentId: string
) {
const pagePath = normalizePagePath(page) const pagePath = normalizePagePath(page)
const dataRoute = deploymentId const dataRoute = path.posix.join('/_next/data', buildId, `${pagePath}.json`)
? 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
...@@ -32,15 +26,11 @@ export function buildDataRoute( ...@@ -32,15 +26,11 @@ export function buildDataRoute(
} else { } else {
dataRouteRegex = normalizeRouteRegex( dataRouteRegex = normalizeRouteRegex(
new RegExp( new RegExp(
`^${ `^${path.posix.join(
deploymentId '/_next/data',
? path.posix.join('/_next/data', `${pagePath}\\.json`) escapeStringRegexp(buildId),
: path.posix.join( `${pagePath}\\.json`
'/_next/data', )}$`
escapeStringRegexp(buildId),
`${pagePath}\\.json`
)
}$`
).source ).source
) )
} }
......
...@@ -283,16 +283,10 @@ export async function setupFsCheck(opts: { ...@@ -283,16 +283,10 @@ 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(
opts.config.deploymentId || route.dataRouteRegex.replace(
opts.config.experimental.runtimeServerDeploymentId `/${escapedBuildId}/`,
? route.dataRouteRegex.replace( `/${escapedBuildId}/(?<nextLocale>[^/]+?)/`
`/_next/data/`, )
`/_next/data/(?<nextLocale>[^/]+?)/`
)
: route.dataRouteRegex.replace(
`/_next/data/${escapedBuildId}/`,
`/_next/data/${escapedBuildId}/(?<nextLocale>[^/]+?)/`
)
) )
: new RegExp(route.dataRouteRegex), : new RegExp(route.dataRouteRegex),
groups: routeRegex.groups, groups: routeRegex.groups,
...@@ -580,12 +574,7 @@ export async function setupFsCheck(opts: { ...@@ -580,12 +574,7 @@ export async function setupFsCheck(opts: {
continue continue
} }
// When deploymentId is set, data URLs don't include the build ID const nextDataPrefix = `/_next/data/${buildId}/`
const nextDataPrefix =
opts.config.deploymentId ||
opts.config.experimental.runtimeServerDeploymentId
? '/_next/data/'
: `/_next/data/${buildId}/`
if ( if (
type === 'pageFile' && type === 'pageFile' &&
...@@ -593,7 +582,7 @@ export async function setupFsCheck(opts: { ...@@ -593,7 +582,7 @@ export async function setupFsCheck(opts: {
curItemPath.endsWith('.json') curItemPath.endsWith('.json')
) { ) {
items = nextDataRoutes items = nextDataRoutes
// remove _next/data/ or _next/data/<build-id>/ prefix // remove _next/data/<build-id> prefix
curItemPath = curItemPath.substring(nextDataPrefix.length - 1) curItemPath = curItemPath.substring(nextDataPrefix.length - 1)
// remove .json postfix // remove .json postfix
......
...@@ -331,10 +331,7 @@ export function getResolveRoutes( ...@@ -331,10 +331,7 @@ export function getResolveRoutes(
config.basePath && config.basePath !== '/' config.basePath && config.basePath !== '/'
? new BasePathPathnameNormalizer(config.basePath) ? new BasePathPathnameNormalizer(config.basePath)
: undefined, : undefined,
data: new NextDataPathnameNormalizer( data: new NextDataPathnameNormalizer(fsChecker.buildId),
fsChecker.buildId,
config.deploymentId || ''
),
} }
async function handleRoute( async function handleRoute(
......
...@@ -1088,11 +1088,7 @@ async function startWatcher( ...@@ -1088,11 +1088,7 @@ 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( const route = buildDataRoute(page, 'development')
page,
'development',
nextConfig.deploymentId
)
const routeRegex = getNamedRouteRegex(route.page, { const routeRegex = getNamedRouteRegex(route.page, {
prefixRouteKeys: true, prefixRouteKeys: true,
}) })
......
...@@ -4,35 +4,30 @@ describe('NextDataPathnameNormalizer', () => { ...@@ -4,35 +4,30 @@ 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([ describe('match', () => {
{ name: 'with build id', buildId: 'build-id', deploymentId: '' }, it('should return false if the pathname does not start with the prefix', () => {
{ name: 'with deployment id', buildId: 'build-id', deploymentId: 'dpl-id' }, const normalizer = new NextDataPathnameNormalizer('build-id')
])('$name', ({ buildId, deploymentId }) => { const pathnames = ['/foo', '/foo/bar', '/fooo/bar']
describe('match', () => { for (const pathname of pathnames) {
it('should return false if the pathname does not start with the prefix', () => { expect(normalizer.match(pathname)).toBe(false)
const normalizer = new NextDataPathnameNormalizer(buildId, deploymentId) }
const pathnames = ['/foo', '/foo/bar', '/fooo/bar'] })
for (const pathname of pathnames) {
expect(normalizer.match(pathname)).toBe(false)
}
})
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(buildId, deploymentId) const normalizer = new NextDataPathnameNormalizer('build-id')
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(buildId, deploymentId) const normalizer = new NextDataPathnameNormalizer('build-id')
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',
...@@ -43,51 +38,37 @@ describe('NextDataPathnameNormalizer', () => { ...@@ -43,51 +38,37 @@ 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(buildId, deploymentId) const normalizer = new NextDataPathnameNormalizer('build-id')
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)
} }
}) })
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(buildId, deploymentId) const normalizer = new NextDataPathnameNormalizer('build-id')
const pathnames = deploymentId const pathnames = [
? [ '/_next/data/build-id/foo.json',
'/_next/data/foo.json', '/_next/data/build-id/foo/bar.json',
'/_next/data/foo/bar.json', '/_next/data/build-id/fooo/bar.json',
'/_next/data/fooo/bar.json', ]
] for (const pathname of pathnames) {
: [ expect(normalizer.normalize(pathname)).toBe(
'/_next/data/build-id/foo.json', pathname.substring(
'/_next/data/build-id/foo/bar.json', '/_next/data/build-id'.length,
'/_next/data/build-id/fooo/bar.json', pathname.length - '.json'.length
]
for (const pathname of pathnames) {
expect(normalizer.normalize(pathname)).toBe(
pathname.substring(
deploymentId
? '/_next/data'.length
: '/_next/data/build-id'.length,
pathname.length - '.json'.length
)
) )
} )
}) }
})
it('should normalize `/index` to `/`', () => { it('should normalize `/index` to `/`', () => {
const normalizer = new NextDataPathnameNormalizer(buildId, deploymentId) const normalizer = new NextDataPathnameNormalizer('build-id')
expect( expect(normalizer.normalize('/_next/data/build-id/index.json')).toBe('/')
normalizer.normalize(
deploymentId
? `/_next/data/index.json`
: `/_next/data/build-id/index.json`
)
).toBe('/')
})
}) })
}) })
}) })
...@@ -7,15 +7,12 @@ import { SuffixPathnameNormalizer } from './suffix' ...@@ -7,15 +7,12 @@ 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, deploymentId: string) { constructor(buildID: string) {
if (!buildID) { if (!buildID) {
throw new Error('Invariant: buildID is required') throw new Error('Invariant: buildID is required')
} }
// When deploymentId is set, data URLs don't include the build ID this.prefix = new PrefixPathnameNormalizer(`/_next/data/${buildID}`)
this.prefix = new PrefixPathnameNormalizer(
deploymentId ? '/_next/data' : `/_next/data/${buildID}`
)
} }
public match(pathname: string) { public match(pathname: string) {
......
...@@ -31,7 +31,6 @@ import { ...@@ -31,7 +31,6 @@ 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'
...@@ -635,10 +634,6 @@ export const getHandler = ({ ...@@ -635,10 +634,6 @@ 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
} }
...@@ -647,10 +642,6 @@ export const getHandler = ({ ...@@ -647,10 +642,6 @@ 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
...@@ -723,14 +714,6 @@ export const getHandler = ({ ...@@ -723,14 +714,6 @@ 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
if (getDeploymentId()) { .replace(/\/_next\/data\/[^/]{1,}/, '')
pathname = pathname.replace(/\/_next\/data/, '') .replace(/\.json$/, '')
} else {
pathname = pathname.replace(/\/_next\/data\/[^/]+/, '')
}
pathname = pathname.replace(/\.json$/, '')
if (pathname === '/index') { if (pathname === '/index') {
return '/' return '/'
......
...@@ -42,10 +42,7 @@ import { omit } from './utils/omit' ...@@ -42,10 +42,7 @@ 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 { import { MATCHED_PATH_HEADER } from '../../../lib/constants'
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'
...@@ -559,25 +556,6 @@ function fetchNextData({ ...@@ -559,25 +556,6 @@ 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'
...@@ -8,9 +11,8 @@ import { ...@@ -8,9 +11,8 @@ import {
nextBuild, nextBuild,
nextStart, nextStart,
} from 'next-test-utils' } from 'next-test-utils'
import { isNextDeploy, isNextDev, isNextStart, nextTestSetup } from 'e2e-utils'
const appDir = __dirname const appDir = join(__dirname, '..')
let proxyPort let proxyPort
...@@ -50,174 +52,145 @@ const runTests = (switchDeployment: (bool) => void) => { ...@@ -50,174 +52,145 @@ const runTests = (switchDeployment: (bool) => void) => {
}) })
} }
describe('pages ssg data deployment skew - hard navigate when a new deployment occurs', () => { describe('SSG data 404 - 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 = [] let proxyServer
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) => {
await new Promise<void>((resolve) => { require('console').error(e)
proxyServer.listen(proxyPort, () => resolve())
}) })
}) if (should404Data && req.url.match(/\/_next\/data/)) {
afterAll(async () => { res.statusCode = 404
for (const app of apps) { return res.end('not found')
await killApp(app)
} }
proxyServer.close() proxy.web(req, res)
}) })
runTests((v) => { await new Promise<void>((resolve) => {
should404Data = v proxyServer.listen(proxyPort, () => resolve())
}) })
}) })
} afterAll(async () => {
for (const app of apps) {
await killApp(app)
}
proxyServer.close()
})
runTests((v) => {
should404Data = v
})
})
if (isNextStart) { describe.each([
describe.each([ { name: 'with build id' },
{ name: 'with build id' }, {
{ name: 'with deployment id',
name: 'with deployment id', 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',
])( ({ NEXT_DEPLOYMENT_ID1, NEXT_DEPLOYMENT_ID2 }) => {
'production mode $name', let shouldSwitchDeployment = false
({ NEXT_DEPLOYMENT_ID1, NEXT_DEPLOYMENT_ID2, OUTPUT_MODE }) => { let apps = []
let shouldSwitchDeployment = false let proxyServer
let apps = []
let proxyServer beforeAll(async () => {
await nextBuild(appDir, [], {
beforeAll(async () => { env: {
await nextBuild(appDir, [], { DIST_DIR: '1',
NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID1,
},
})
let appPort1 = await findPort()
apps.push(
await nextStart(appDir, appPort1, {
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(
await nextStart(appDir, appPort2, {
env: {
DIST_DIR: '2',
NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID2,
},
})
)
const proxy1 = httpProxy.createProxyServer({ const proxy1 = httpProxy.createProxyServer({
target: `http://localhost:${appPort1}`, target: `http://localhost:${appPort1}`,
})
const proxy2 = httpProxy.createProxyServer({
target: `http://localhost:${appPort2}`,
})
proxyPort = await findPort()
proxyServer = http.createServer((req, res) => {
req.on('error', (e) => {
require('console').error(e)
}) })
const proxy2 = httpProxy.createProxyServer({ res.on('error', (e) => {
target: `http://localhost:${appPort2}`, require('console').error(e)
}) })
proxyPort = await findPort()
proxyServer = http.createServer((req, res) => { if (shouldSwitchDeployment) {
req.on('error', (e) => { proxy2.web(req, res, undefined, (e) => {
require('console').error(e)
})
res.on('error', (e) => {
require('console').error(e) require('console').error(e)
}) })
return
}
if (shouldSwitchDeployment) { proxy1.web(req, res, undefined, (e) => {
proxy2.web(req, res, undefined, (e) => { require('console').error(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()
})
runTests((v) => { await new Promise<void>((resolve) => {
shouldSwitchDeployment = v proxyServer.listen(proxyPort, () => resolve())
}) })
} })
) afterAll(async () => {
} for (const app of apps) {
await killApp(app)
describe('header with deployment id', () => { }
const { next } = nextTestSetup({ proxyServer.close()
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) runTests((v) => {
expect(res.headers.get('content-type')).toStartWith('application/json') shouldSwitchDeployment = v
expect(res.headers.get('x-nextjs-deployment-id')).toBeTruthy() })
} }
}) )
})
}) })
...@@ -28,6 +28,7 @@ describe('Required Server Files', () => { ...@@ -28,6 +28,7 @@ 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
...@@ -36,6 +37,8 @@ describe('Required Server Files', () => { ...@@ -36,6 +37,8 @@ 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')
) )
...@@ -193,7 +196,7 @@ describe('Required Server Files', () => { ...@@ -193,7 +196,7 @@ describe('Required Server Files', () => {
const { pageProps: data4 } = JSON.parse( const { pageProps: data4 } = JSON.parse(
await renderViaHTTP( await renderViaHTTP(
appPort, appPort,
`/_next/data/${next.buildId}/fallback/third.json`, `/_next/data/${buildId}/fallback/third.json`,
undefined, undefined,
withInvocationId() withInvocationId()
) )
...@@ -316,7 +319,7 @@ describe('Required Server Files', () => { ...@@ -316,7 +319,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/${next.buildId}/dynamic/first.json?nxtPslug=first`, `/_next/data/${buildId}/dynamic/first.json?nxtPslug=first`,
undefined, undefined,
withInvocationId({ withInvocationId({
headers: { headers: {
...@@ -332,11 +335,11 @@ describe('Required Server Files', () => { ...@@ -332,11 +335,11 @@ describe('Required Server Files', () => {
const res2 = await fetchViaHTTP( const res2 = await fetchViaHTTP(
appPort, appPort,
`/_next/data/${next.buildId}/fallback/[slug].json`, `/_next/data/${buildId}/fallback/[slug].json`,
undefined, undefined,
withInvocationId({ withInvocationId({
headers: { headers: {
'x-matched-path': `/_next/data/${next.buildId}/fallback/[slug].json`, 'x-matched-path': `/_next/data/${buildId}/fallback/[slug].json`,
'x-now-route-matches': 'nxtPslug=second', 'x-now-route-matches': 'nxtPslug=second',
}, },
}) })
...@@ -502,7 +505,7 @@ describe('Required Server Files', () => { ...@@ -502,7 +505,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/${next.buildId}/catch-all.json`, `/_next/data/${buildId}/catch-all.json`,
undefined, undefined,
withInvocationId({ withInvocationId({
headers: { headers: {
...@@ -518,11 +521,11 @@ describe('Required Server Files', () => { ...@@ -518,11 +521,11 @@ describe('Required Server Files', () => {
const res2 = await fetchViaHTTP( const res2 = await fetchViaHTTP(
appPort, appPort,
`/_next/data/${next.buildId}/catch-all/[[...rest]].json`, `/_next/data/${buildId}/catch-all/[[...rest]].json`,
undefined, undefined,
withInvocationId({ withInvocationId({
headers: { headers: {
'x-matched-path': `/_next/data/${next.buildId}/catch-all/[[...rest]].json`, 'x-matched-path': `/_next/data/${buildId}/catch-all/[[...rest]].json`,
'x-now-route-matches': 'nxtPrest=hello&rest=hello', 'x-now-route-matches': 'nxtPrest=hello&rest=hello',
}, },
}) })
...@@ -535,11 +538,11 @@ describe('Required Server Files', () => { ...@@ -535,11 +538,11 @@ describe('Required Server Files', () => {
const res3 = await fetchViaHTTP( const res3 = await fetchViaHTTP(
appPort, appPort,
`/_next/data/${next.buildId}/catch-all/[[...rest]].json`, `/_next/data/${buildId}/catch-all/[[...rest]].json`,
undefined, undefined,
withInvocationId({ withInvocationId({
headers: { headers: {
'x-matched-path': `/_next/data/${next.buildId}/catch-all/[[...rest]].json`, 'x-matched-path': `/_next/data/${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