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