Unverified Commit 522de8c3 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Fix deployment id header when proxying (#89593)

When proxying through multiple projects, this header would get overwritten by the proxy project:
- the header would be the proxy's deployment id
- `process.env.NEXT_DEPLOYMENT_ID` would be the origin's deployment id

Needs https://github.com/vercel/vercel/pull/14903
parent 5ea38767
......@@ -30,6 +30,7 @@ import type {
ManifestRewriteRoute,
FunctionsConfigManifest,
DynamicPrerenderManifestRoute,
ManifestHeaderRoute,
} from '..'
import {
......@@ -1977,6 +1978,19 @@ export async function handleBuildComplete({
} satisfies Route
}
const buildRouteFromHeader = (route: ManifestHeaderRoute): Route => {
const converted = convertHeaders([route])[0]
const regex = converted.src || route.regex
return {
source: route.source,
sourceRegex: route.internal ? regex : modifyRouteRegex(regex),
headers: 'headers' in converted ? converted.headers || {} : {},
has: route.has,
missing: route.missing,
priority: route.internal || undefined,
} satisfies Route
}
try {
Log.info(`Running onBuildComplete from ${adapterMod.name}`)
......@@ -2007,19 +2021,12 @@ export async function handleBuildComplete({
} satisfies Route
})
const headers = routesManifest.headers.map((route) => {
const converted = convertHeaders([route])[0]
const regex = converted.src || route.regex
return {
source: route.source,
sourceRegex: route.internal ? regex : modifyRouteRegex(regex),
headers: 'headers' in converted ? converted.headers || {} : {},
has: route.has,
missing: route.missing,
priority: route.internal || undefined,
} satisfies Route
})
const headers = routesManifest.headers.map((route) =>
buildRouteFromHeader(route)
)
const onMatchHeaders = routesManifest.onMatchHeaders.map((route) =>
buildRouteFromHeader(route)
)
await adapterMod.onBuildComplete({
routing: {
......@@ -2038,6 +2045,7 @@ export async function handleBuildComplete({
'cache-control': `public,max-age=${CACHE_ONE_YEAR_SECONDS},immutable`,
},
},
...onMatchHeaders,
],
fallback: rewrites.fallback,
shouldNormalizeNextData: !!needsMiddlewareResolveRoutes,
......@@ -2050,6 +2058,7 @@ export async function handleBuildComplete({
config,
redirects: [],
headers: [],
onMatchHeaders: [],
rewrites,
restrictedRedirectPaths: [],
isAppPPREnabled: config.cacheComponents,
......
......@@ -157,7 +157,8 @@ async function collectRoutesForAnalyze(
}
// Load custom routes
const { redirects, headers, rewrites } = await loadCustomRoutes(config)
const { redirects, headers, onMatchHeaders, rewrites } =
await loadCustomRoutes(config)
// Compute restricted redirect paths
const restrictedRedirectPaths = ['/_next'].map((pathPrefix) =>
......@@ -173,6 +174,7 @@ async function collectRoutesForAnalyze(
config,
redirects,
headers,
onMatchHeaders,
rewrites,
restrictedRedirectPaths,
isAppPPREnabled,
......
......@@ -31,6 +31,7 @@ export interface GenerateRoutesManifestOptions {
config: NextConfigComplete
redirects: CustomRoutes['redirects']
headers: CustomRoutes['headers']
onMatchHeaders: CustomRoutes['headers']
rewrites: CustomRoutes['rewrites']
restrictedRedirectPaths: string[]
isAppPPREnabled: boolean
......@@ -57,6 +58,7 @@ export function generateRoutesManifest(
config,
redirects,
headers,
onMatchHeaders,
rewrites,
restrictedRedirectPaths,
isAppPPREnabled,
......@@ -102,6 +104,7 @@ export function generateRoutesManifest(
buildCustomRoute('redirect', r, restrictedRedirectPaths)
),
headers: headers.map((r) => buildCustomRoute('header', r)),
onMatchHeaders: onMatchHeaders.map((r) => buildCustomRoute('header', r)),
rewrites: {
beforeFiles: rewrites.beforeFiles.map((r) =>
buildCustomRoute('rewrite', r)
......
......@@ -431,6 +431,7 @@ export type RoutesManifest = {
fallback: Array<ManifestRewriteRoute>
}
headers: Array<ManifestHeaderRoute>
onMatchHeaders: Array<ManifestHeaderRoute>
staticRoutes: Array<ManifestRoute>
dynamicRoutes: ReadonlyArray<DynamicManifestRoute>
dataRoutes: Array<ManifestDataRoute>
......@@ -1040,7 +1041,7 @@ export default async function build(
.traceChild('load-custom-routes')
.traceAsyncFn(() => loadCustomRoutes(config))
const { headers, rewrites, redirects } = customRoutes
const { headers, onMatchHeaders, rewrites, redirects } = customRoutes
const combinedRewrites: Rewrite[] = [
...rewrites.beforeFiles,
...rewrites.afterFiles,
......@@ -1507,6 +1508,7 @@ export default async function build(
config,
redirects,
headers,
onMatchHeaders,
rewrites,
restrictedRedirectPaths,
isAppPPREnabled,
......@@ -4143,7 +4145,9 @@ export default async function build(
if (debugOutput) {
nextBuildSpan
.traceChild('print-custom-routes')
.traceFn(() => printCustomRoutes({ redirects, rewrites, headers }))
.traceFn(() =>
printCustomRoutes({ redirects, onMatchHeaders, rewrites, headers })
)
}
await nextBuildSpan.traceChild('print-tree-view').traceAsyncFn(() =>
......
......@@ -646,13 +646,14 @@ export function printCustomRoutes({
redirects,
rewrites,
headers,
onMatchHeaders,
}: CustomRoutes) {
const printRoutes = (
routes: Redirect[] | Rewrite[] | Header[],
type: 'Redirects' | 'Rewrites' | 'Headers'
type: 'Redirects' | 'Rewrites' | 'Headers' | 'On Match Headers'
) => {
const isRedirects = type === 'Redirects'
const isHeaders = type === 'Headers'
const isHeaders = type === 'Headers' || type === 'On Match Headers'
print(underline(type))
/*
......@@ -705,6 +706,9 @@ export function printCustomRoutes({
if (headers.length) {
printRoutes(headers, 'Headers')
}
if (onMatchHeaders.length) {
printRoutes(onMatchHeaders, 'On Match Headers')
}
const combinedRewrites = [
...rewrites.beforeFiles,
......
......@@ -489,6 +489,7 @@ export function checkCustomRoutes(
export interface CustomRoutes {
headers: Header[]
onMatchHeaders: Header[]
rewrites: {
fallback: Rewrite[]
afterFiles: Rewrite[]
......@@ -708,6 +709,8 @@ export default async function loadCustomRoutes(
loadRedirects(config),
])
const onMatchHeaders: Header[] = []
const totalRewrites =
rewrites.beforeFiles.length +
rewrites.afterFiles.length +
......@@ -760,7 +763,7 @@ export default async function loadCustomRoutes(
})
}
headers.unshift(
onMatchHeaders.push(
{
source: '/:path*',
has: [
......@@ -852,6 +855,7 @@ export default async function loadCustomRoutes(
return {
headers,
onMatchHeaders,
rewrites,
redirects,
}
......
......@@ -159,6 +159,7 @@ export async function setupFsCheck(opts: {
afterFiles: [],
fallback: [],
},
onMatchHeaders: [],
headers: [],
}
let buildId = 'development'
......@@ -337,6 +338,7 @@ export async function setupFsCheck(opts: {
fallback: [],
},
headers: routesManifest.headers,
onMatchHeaders: routesManifest.onMatchHeaders,
}
} else {
// dev handling
......@@ -369,6 +371,14 @@ export async function setupFsCheck(opts: {
opts.config.experimental.caseSensitiveRoutes
)
)
const onMatchHeaders = customRoutes.onMatchHeaders.map((item) =>
buildCustomRoute(
'header',
item,
opts.config.basePath,
opts.config.experimental.caseSensitiveRoutes
)
)
const redirects = customRoutes.redirects.map((item) =>
buildCustomRoute(
'redirect',
......@@ -431,6 +441,7 @@ export async function setupFsCheck(opts: {
return {
headers,
onMatchHeaders,
rewrites,
redirects,
......
......@@ -867,6 +867,12 @@ export function getResolveRoutes(
for (const route of routes) {
const result = await handleRoute(route)
if (result) {
if (result.matchedOutput) {
// handle onMatchHeaders
for (const onMatchHeaders of fsChecker.onMatchHeaders) {
await handleRoute(onMatchHeaders)
}
}
return result
}
}
......
......@@ -124,6 +124,7 @@ export interface DevRoutesManifest {
rewrites: RoutesManifest['rewrites']
redirects: RoutesManifest['redirects']
headers: RoutesManifest['headers']
onMatchHeaders: RoutesManifest['headers']
i18n: RoutesManifest['i18n']
skipProxyUrlNormalize: RoutesManifest['skipProxyUrlNormalize']
}
......@@ -309,6 +310,7 @@ async function startWatcher(
rewrites: opts.fsChecker.rewrites,
redirects: opts.fsChecker.redirects,
headers: opts.fsChecker.headers,
onMatchHeaders: opts.fsChecker.onMatchHeaders,
i18n: nextConfig.i18n || undefined,
skipProxyUrlNormalize: nextConfig.skipProxyUrlNormalize,
}
......
......@@ -228,6 +228,7 @@ export abstract class RouteModule<
},
redirects: [],
headers: [],
onMatchHeaders: [],
i18n:
(process.env.__NEXT_I18N_CONFIG as any as I18NConfig) || undefined,
skipProxyUrlNormalize: Boolean(
......
......@@ -67,7 +67,7 @@ describe('segment cache (deployment skew)', () => {
},
})
// Deployment skew is hard to properly e2e deploy test, so this just checks for the header
// Deployment skew is hard to properly e2e deploy test, so this just checks for the header.
it('header is set on RSC responses', async () => {
for (const route of ['/dynamic-page', '/static-page']) {
await next.fetch(route)
......
......@@ -208,7 +208,7 @@ describe('pages ssg data deployment skew - hard navigate when a new deployment o
},
})
// Deployment skew is hard to properly e2e deploy test, so this just checks for the header
// 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)
......
......@@ -1864,6 +1864,7 @@ const runTests = (isDev = false) => {
"source": "/has-header-4",
},
],
"onMatchHeaders": [],
"pages404": true,
"redirects": [
{
......
......@@ -1463,6 +1463,7 @@ function runTests({ dev }) {
},
],
"headers": [],
"onMatchHeaders": [],
"pages404": true,
"redirects": [
{
......
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