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 @@
"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",
"989": "Loaded static props were from an outdated deployment, forcing a hard reload"
"988": "Invalid \\`deploymentId\\` configuration: contains invalid characters. Only alphanumeric characters, hyphens, and underscores are allowed. See https://nextjs.org/docs/messages/deploymentid-invalid-characters"
}
......@@ -2791,7 +2791,7 @@ export default async function build(
...serverPropsPages,
...ssgPages,
]).map((page) => {
return buildDataRoute(page, buildId, config.deploymentId)
return buildDataRoute(page, buildId)
})
}
......@@ -3876,13 +3876,11 @@ export default async function build(
experimentalPPR: undefined,
renderingMode: undefined,
srcRoute: null,
dataRoute: config.deploymentId
? path.posix.join('/_next/data', `${localePage}.json`)
: path.posix.join(
'/_next/data',
buildId,
`${localePage}.json`
),
dataRoute: path.posix.join(
'/_next/data',
buildId,
`${localePage}.json`
),
prefetchDataRoute: undefined,
allowHeader: ALLOWED_HEADERS,
}
......@@ -3896,9 +3894,11 @@ export default async function build(
experimentalPPR: undefined,
renderingMode: undefined,
srcRoute: null,
dataRoute: config.deploymentId
? path.posix.join('/_next/data', `${file}.json`)
: path.posix.join('/_next/data', buildId, `${file}.json`),
dataRoute: path.posix.join(
'/_next/data',
buildId,
`${file}.json`
),
// Pages does not have a prefetch data route.
prefetchDataRoute: undefined,
allowHeader: ALLOWED_HEADERS,
......@@ -3939,16 +3939,11 @@ export default async function build(
experimentalPPR: undefined,
renderingMode: undefined,
srcRoute: page,
dataRoute: config.deploymentId
? path.posix.join(
'/_next/data',
`${normalizePagePath(route.pathname)}.json`
)
: path.posix.join(
'/_next/data',
buildId,
`${normalizePagePath(route.pathname)}.json`
),
dataRoute: path.posix.join(
'/_next/data',
buildId,
`${normalizePagePath(route.pathname)}.json`
),
// Pages does not have a prefetch data route.
prefetchDataRoute: undefined,
allowHeader: ALLOWED_HEADERS,
......@@ -4057,9 +4052,11 @@ export default async function build(
if (ssgPages.size > 0 || appDir) {
tbdPrerenderRoutes.forEach((tbdRoute) => {
const normalizedRoute = normalizePagePath(tbdRoute)
const dataRoute = config.deploymentId
? path.posix.join('/_next/data', `${normalizedRoute}.json`)
: path.posix.join('/_next/data', buildId, `${normalizedRoute}.json`)
const dataRoute = path.posix.join(
'/_next/data',
buildId,
`${normalizedRoute}.json`
)
prerenderManifest.dynamicRoutes[tbdRoute] = {
routeRegex: normalizeRouteRegex(
......
......@@ -18,7 +18,6 @@ import {
DEV_CLIENT_MIDDLEWARE_MANIFEST,
} from '../shared/lib/constants'
import { resolvePromiseWithTimeout } from './lib/promise'
import { getDeploymentId } from '../shared/lib/deployment-id'
declare global {
interface Window {
......@@ -171,12 +170,8 @@ export default class PageLoader {
removeTrailingSlash(addLocale(path, locale)),
'.json'
)
// We check the response header instead
return addBasePath(
getDeploymentId()
? `/_next/data${dataRoute}${search}`
: `/_next/data/${this.buildId}${dataRoute}${search}`,
`/_next/data/${this.buildId}${dataRoute}${search}`,
true
)
}
......
......@@ -19,8 +19,6 @@ export const NEXT_DATA_SUFFIX = '.json'
export const NEXT_META_SUFFIX = '.meta'
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_REVALIDATED_TAGS_HEADER = 'x-next-revalidated-tags'
export const NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER =
......
......@@ -6,7 +6,6 @@ import { escapeStringRegexp } from '../shared/lib/escape-regexp'
import { tryToParsePath } from './try-to-parse-path'
import { allowedStatusCodes } from './redirect-status'
import { isFullStringUrl } from './url'
import { NEXT_NAV_DEPLOYMENT_ID_HEADER } from './constants'
export type RouteHas =
| {
......@@ -747,25 +746,13 @@ export default async function loadCustomRoutes(
)
}
if (config.deploymentId) {
if (config.experimental?.useSkewCookie) {
headers.unshift({
source: '/:path*',
headers: [
{
key: 'Set-Cookie',
value: `__vdpl=${config.deploymentId}; Path=/; HttpOnly`,
},
],
})
}
if (config.experimental?.useSkewCookie && config.deploymentId) {
headers.unshift({
source: '/_next/data/(.*)',
source: '/:path*',
headers: [
{
key: NEXT_NAV_DEPLOYMENT_ID_HEADER,
value: config.deploymentId,
key: 'Set-Cookie',
value: `__vdpl=${config.deploymentId}; Path=/; HttpOnly`,
},
],
})
......
......@@ -524,7 +524,7 @@ export default abstract class Server<
? new SegmentPrefixRSCPathnameNormalizer()
: undefined,
data: this.enabledDirectories.pages
? new NextDataPathnameNormalizer(this.buildId, this.deploymentId)
? new NextDataPathnameNormalizer(this.buildId)
: undefined,
}
......@@ -680,22 +680,20 @@ export default abstract class Server<
return false
}
if (!this.nextConfig.deploymentId) {
if (params.path[0] !== this.buildId) {
// Ignore if its a middleware request when we aren't on edge.
if (getRequestMeta(req, 'middlewareInvoke')) {
return false
}
// Make sure to 404 if the buildId isn't correct
await this.render404(req, res, parsedUrl)
return true
if (params.path[0] !== this.buildId) {
// Ignore if its a middleware request when we aren't on edge.
if (getRequestMeta(req, 'middlewareInvoke')) {
return false
}
// remove buildId from URL
params.path.shift()
// Make sure to 404 if the buildId isn't correct
await this.render404(req, res, parsedUrl)
return true
}
// remove buildId from URL
params.path.shift()
const lastParam = params.path[params.path.length - 1]
// show 404 if it doesn't end with .json
......@@ -2442,12 +2440,13 @@ export default abstract class Server<
}
private stripNextDataPath(filePath: string, stripLocale = true) {
if (this.deploymentId) {
filePath = filePath.replace(/\/_next\/data/, '')
} else {
filePath = filePath.replace(/\/_next\/data\/[^/]+/, '')
if (filePath.includes(this.buildId)) {
const splitPath = filePath.substring(
filePath.indexOf(this.buildId) + this.buildId.length
)
filePath = denormalizePagePath(splitPath.replace(/\.json$/, ''))
}
filePath = filePath.replace(/\.json$/, '')
if (this.localeNormalizer && stripLocale) {
return this.localeNormalizer.normalize(filePath)
......
import { buildDataRoute } from './build-data-route'
describe('buildDataRoute', () => {
describe('with build id', () => {
it('should build a dynamic data route', () => {
const dataRoute = buildDataRoute('/[...slug]', '123', '')
expect(dataRoute).toMatchInlineSnapshot(`
{
"dataRouteRegex": "^/_next/data/123/(.+?)\\.json$",
"namedDataRouteRegex": "^/_next/data/123/(?<nxtPslug>.+?)\\.json$",
"page": "/[...slug]",
"routeKeys": {
"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,
}
`)
})
it('should build a dynamic data route', () => {
const dataRoute = buildDataRoute('/[...slug]', '123')
expect(dataRoute).toMatchInlineSnapshot(`
{
"dataRouteRegex": "^/_next/data/123/(.+?)\\.json$",
"namedDataRouteRegex": "^/_next/data/123/(?<nxtPslug>.+?)\\.json$",
"page": "/[...slug]",
"routeKeys": {
"nxtPslug": "nxtPslug",
},
}
`)
})
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,
}
`)
})
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,
}
`)
})
})
......@@ -5,15 +5,9 @@ import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex
import { normalizeRouteRegex } from '../../../lib/load-custom-routes'
import { escapeStringRegexp } from '../../../shared/lib/escape-regexp'
export function buildDataRoute(
page: string,
buildId: string,
deploymentId: string
) {
export function buildDataRoute(page: string, buildId: string) {
const pagePath = normalizePagePath(page)
const dataRoute = deploymentId
? path.posix.join('/_next/data', `${pagePath}.json`)
: path.posix.join('/_next/data', buildId, `${pagePath}.json`)
const dataRoute = path.posix.join('/_next/data', buildId, `${pagePath}.json`)
let dataRouteRegex: string
let namedDataRouteRegex: string | undefined
......@@ -32,15 +26,11 @@ export function buildDataRoute(
} else {
dataRouteRegex = normalizeRouteRegex(
new RegExp(
`^${
deploymentId
? path.posix.join('/_next/data', `${pagePath}\\.json`)
: path.posix.join(
'/_next/data',
escapeStringRegexp(buildId),
`${pagePath}\\.json`
)
}$`
`^${path.posix.join(
'/_next/data',
escapeStringRegexp(buildId),
`${pagePath}\\.json`
)}$`
).source
)
}
......
......@@ -283,16 +283,10 @@ export async function setupFsCheck(opts: {
// upstream builder that relies on this
re: opts.config.i18n
? new RegExp(
opts.config.deploymentId ||
opts.config.experimental.runtimeServerDeploymentId
? route.dataRouteRegex.replace(
`/_next/data/`,
`/_next/data/(?<nextLocale>[^/]+?)/`
)
: route.dataRouteRegex.replace(
`/_next/data/${escapedBuildId}/`,
`/_next/data/${escapedBuildId}/(?<nextLocale>[^/]+?)/`
)
route.dataRouteRegex.replace(
`/${escapedBuildId}/`,
`/${escapedBuildId}/(?<nextLocale>[^/]+?)/`
)
)
: new RegExp(route.dataRouteRegex),
groups: routeRegex.groups,
......@@ -580,12 +574,7 @@ export async function setupFsCheck(opts: {
continue
}
// 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}/`
const nextDataPrefix = `/_next/data/${buildId}/`
if (
type === 'pageFile' &&
......@@ -593,7 +582,7 @@ export async function setupFsCheck(opts: {
curItemPath.endsWith('.json')
) {
items = nextDataRoutes
// remove _next/data/ or _next/data/<build-id>/ prefix
// remove _next/data/<build-id> prefix
curItemPath = curItemPath.substring(nextDataPrefix.length - 1)
// remove .json postfix
......
......@@ -331,10 +331,7 @@ export function getResolveRoutes(
config.basePath && config.basePath !== '/'
? new BasePathPathnameNormalizer(config.basePath)
: undefined,
data: new NextDataPathnameNormalizer(
fsChecker.buildId,
config.deploymentId || ''
),
data: new NextDataPathnameNormalizer(fsChecker.buildId),
}
async function handleRoute(
......
......@@ -1088,11 +1088,7 @@ async function startWatcher(
const dataRoutes: typeof opts.fsChecker.dynamicRoutes = []
for (const page of sortedRoutes) {
const route = buildDataRoute(
page,
'development',
nextConfig.deploymentId
)
const route = buildDataRoute(page, 'development')
const routeRegex = getNamedRouteRegex(route.page, {
prefixRouteKeys: true,
})
......
......@@ -4,35 +4,30 @@ describe('NextDataPathnameNormalizer', () => {
describe('constructor', () => {
it('should error when no buildID is provided', () => {
expect(() => {
new NextDataPathnameNormalizer('', '')
new NextDataPathnameNormalizer('')
}).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', () => {
it('should return false if the pathname does not start with the prefix', () => {
const normalizer = new NextDataPathnameNormalizer(buildId, deploymentId)
const pathnames = ['/foo', '/foo/bar', '/fooo/bar']
for (const pathname of pathnames) {
expect(normalizer.match(pathname)).toBe(false)
}
})
describe('match', () => {
it('should return false if the pathname does not start with the prefix', () => {
const normalizer = new NextDataPathnameNormalizer('build-id')
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`', () => {
const normalizer = new NextDataPathnameNormalizer(buildId, deploymentId)
const pathnames = ['/foo.json', '/foo/bar.json', '/fooo/bar.json']
for (const pathname of pathnames) {
expect(normalizer.match(pathname)).toBe(false)
}
})
it('should return false if the pathname only ends with `.json`', () => {
const normalizer = new NextDataPathnameNormalizer('build-id')
const pathnames = ['/foo.json', '/foo/bar.json', '/fooo/bar.json']
for (const pathname of pathnames) {
expect(normalizer.match(pathname)).toBe(false)
}
})
it('should return true if it matches', () => {
const normalizer = new NextDataPathnameNormalizer(buildId, deploymentId)
const normalizer = new NextDataPathnameNormalizer('build-id')
const pathnames = [
'/_next/data/build-id/index.json',
'/_next/data/build-id/foo.json',
......@@ -43,51 +38,37 @@ describe('NextDataPathnameNormalizer', () => {
expect(normalizer.match(pathname)).toBe(true)
}
})
})
describe('normalize', () => {
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 pathnames = ['/foo', '/foo/bar', '/fooo/bar']
for (const pathname of pathnames) {
expect(normalizer.normalize(pathname)).toBe(pathname)
}
})
describe('normalize', () => {
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 pathnames = ['/foo', '/foo/bar', '/fooo/bar']
for (const pathname of pathnames) {
expect(normalizer.normalize(pathname)).toBe(pathname)
}
})
it('should strip the prefix and the `.json` extension from the pathname when it matches', () => {
const normalizer = new NextDataPathnameNormalizer(buildId, deploymentId)
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/bar.json',
'/_next/data/build-id/fooo/bar.json',
]
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 strip the prefix and the `.json` extension from the pathname when it matches', () => {
const normalizer = new NextDataPathnameNormalizer('build-id')
const pathnames = [
'/_next/data/build-id/foo.json',
'/_next/data/build-id/foo/bar.json',
'/_next/data/build-id/fooo/bar.json',
]
for (const pathname of pathnames) {
expect(normalizer.normalize(pathname)).toBe(
pathname.substring(
'/_next/data/build-id'.length,
pathname.length - '.json'.length
)
}
})
)
}
})
it('should normalize `/index` to `/`', () => {
const normalizer = new NextDataPathnameNormalizer(buildId, deploymentId)
expect(
normalizer.normalize(
deploymentId
? `/_next/data/index.json`
: `/_next/data/build-id/index.json`
)
).toBe('/')
})
it('should normalize `/index` to `/`', () => {
const normalizer = new NextDataPathnameNormalizer('build-id')
expect(normalizer.normalize('/_next/data/build-id/index.json')).toBe('/')
})
})
})
......@@ -7,15 +7,12 @@ import { SuffixPathnameNormalizer } from './suffix'
export class NextDataPathnameNormalizer implements PathnameNormalizer {
private readonly prefix: PrefixPathnameNormalizer
private readonly suffix = new SuffixPathnameNormalizer('.json')
constructor(buildID: string, deploymentId: string) {
constructor(buildID: string) {
if (!buildID) {
throw new Error('Invariant: buildID is required')
}
// When deploymentId is set, data URLs don't include the build ID
this.prefix = new PrefixPathnameNormalizer(
deploymentId ? '/_next/data' : `/_next/data/${buildID}`
)
this.prefix = new PrefixPathnameNormalizer(`/_next/data/${buildID}`)
}
public match(pathname: string) {
......
......@@ -31,7 +31,6 @@ import {
CACHE_ONE_YEAR,
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'
......@@ -635,10 +634,6 @@ 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
}
......@@ -647,10 +642,6 @@ 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
......@@ -723,14 +714,6 @@ 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,
......
import { pathHasPrefix } from '../router/utils/path-has-prefix'
import { getDeploymentId } from '../deployment-id'
/**
* strip _next/data/(<build-id>/) prefix and .json suffix
* When deploymentId is set, the path does NOT contain build-id.
* strip _next/data/<build-id>/ prefix and .json suffix
*/
export function normalizeDataPath(pathname: string) {
if (!pathHasPrefix(pathname || '/', '/_next/data')) {
return pathname
}
if (getDeploymentId()) {
pathname = pathname.replace(/\/_next\/data/, '')
} else {
pathname = pathname.replace(/\/_next\/data\/[^/]+/, '')
}
pathname = pathname.replace(/\.json$/, '')
pathname = pathname
.replace(/\/_next\/data\/[^/]{1,}/, '')
.replace(/\.json$/, '')
if (pathname === '/index') {
return '/'
......
......@@ -42,10 +42,7 @@ 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,
NEXT_NAV_DEPLOYMENT_ID_HEADER,
} from '../../../lib/constants'
import { MATCHED_PATH_HEADER } from '../../../lib/constants'
import { getDeploymentId } from '../deployment-id'
import { isJavaScriptURLString } from '../../../client/lib/javascript-url'
......@@ -559,25 +556,6 @@ 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'
......@@ -8,9 +11,8 @@ import {
nextBuild,
nextStart,
} from 'next-test-utils'
import { isNextDeploy, isNextDev, isNextStart, nextTestSetup } from 'e2e-utils'
const appDir = __dirname
const appDir = join(__dirname, '..')
let proxyPort
......@@ -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') {
it('should skip this suite on Windows', () => {})
return
}
if (isNextDev) {
describe('development mode', () => {
let should404Data = false
let apps = []
let proxyServer
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)
})
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)
proxyServer = http.createServer((req, res) => {
req.on('error', (e) => {
require('console').error(e)
})
await new Promise<void>((resolve) => {
proxyServer.listen(proxyPort, () => resolve())
res.on('error', (e) => {
require('console').error(e)
})
})
afterAll(async () => {
for (const app of apps) {
await killApp(app)
if (should404Data && req.url.match(/\/_next\/data/)) {
res.statusCode = 404
return res.end('not found')
}
proxyServer.close()
proxy.web(req, res)
})
runTests((v) => {
should404Data = v
await new Promise<void>((resolve) => {
proxyServer.listen(proxyPort, () => resolve())
})
})
}
afterAll(async () => {
for (const app of apps) {
await killApp(app)
}
proxyServer.close()
})
runTests((v) => {
should404Data = v
})
})
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, [], {
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, {
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, [], {
await nextBuild(appDir, [], {
env: {
DIST_DIR: '2',
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,
},
})
let appPort2 = await findPort()
apps.push(
await nextStart(appDir, appPort2, {
env: {
DIST_DIR: '2',
NEXT_DEPLOYMENT_ID: NEXT_DEPLOYMENT_ID2,
},
})
)
)
const proxy1 = httpProxy.createProxyServer({
target: `http://localhost:${appPort1}`,
const proxy1 = httpProxy.createProxyServer({
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({
target: `http://localhost:${appPort2}`,
res.on('error', (e) => {
require('console').error(e)
})
proxyPort = await findPort()
proxyServer = http.createServer((req, res) => {
req.on('error', (e) => {
require('console').error(e)
})
res.on('error', (e) => {
if (shouldSwitchDeployment) {
proxy2.web(req, res, undefined, (e) => {
require('console').error(e)
})
return
}
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())
proxy1.web(req, res, undefined, (e) => {
require('console').error(e)
})
})
afterAll(async () => {
for (const app of apps) {
await killApp(app)
}
proxyServer.close()
})
runTests((v) => {
shouldSwitchDeployment = v
await new Promise<void>((resolve) => {
proxyServer.listen(proxyPort, () => resolve())
})
}
)
}
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`)
})
afterAll(async () => {
for (const app of apps) {
await killApp(app)
}
proxyServer.close()
})
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toStartWith('application/json')
expect(res.headers.get('x-nextjs-deployment-id')).toBeTruthy()
}
})
})
runTests((v) => {
shouldSwitchDeployment = v
})
}
)
})
......@@ -28,6 +28,7 @@ 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
......@@ -36,6 +37,8 @@ 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')
)
......@@ -193,7 +196,7 @@ describe('Required Server Files', () => {
const { pageProps: data4 } = JSON.parse(
await renderViaHTTP(
appPort,
`/_next/data/${next.buildId}/fallback/third.json`,
`/_next/data/${buildId}/fallback/third.json`,
undefined,
withInvocationId()
)
......@@ -316,7 +319,7 @@ describe('Required Server Files', () => {
it('should return data correctly with x-matched-path', async () => {
const res = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}/dynamic/first.json?nxtPslug=first`,
`/_next/data/${buildId}/dynamic/first.json?nxtPslug=first`,
undefined,
withInvocationId({
headers: {
......@@ -332,11 +335,11 @@ describe('Required Server Files', () => {
const res2 = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}/fallback/[slug].json`,
`/_next/data/${buildId}/fallback/[slug].json`,
undefined,
withInvocationId({
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',
},
})
......@@ -502,7 +505,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/${next.buildId}/catch-all.json`,
`/_next/data/${buildId}/catch-all.json`,
undefined,
withInvocationId({
headers: {
......@@ -518,11 +521,11 @@ describe('Required Server Files', () => {
const res2 = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
`/_next/data/${buildId}/catch-all/[[...rest]].json`,
undefined,
withInvocationId({
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',
},
})
......@@ -535,11 +538,11 @@ describe('Required Server Files', () => {
const res3 = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}/catch-all/[[...rest]].json`,
`/_next/data/${buildId}/catch-all/[[...rest]].json`,
undefined,
withInvocationId({
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',
},
})
......
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