Unverified Commit c75cc67f authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Clean up flight headers (#67525)

- The array nesting was most likely done accidentally in #42482.
- The wording 'parameters' is vestigial from when those were actually
query params.
- Some header consts were missing the `_HEADER` suffix.
parent b70f7c89
...@@ -139,11 +139,11 @@ import { getFilesInDir } from '../lib/get-files-in-dir' ...@@ -139,11 +139,11 @@ import { getFilesInDir } from '../lib/get-files-in-dir'
import { eventSwcPlugins } from '../telemetry/events/swc-plugins' import { eventSwcPlugins } from '../telemetry/events/swc-plugins'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths' import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
import { import {
ACTION, ACTION_HEADER,
NEXT_ROUTER_PREFETCH_HEADER, NEXT_ROUTER_PREFETCH_HEADER,
RSC_HEADER, RSC_HEADER,
RSC_CONTENT_TYPE_HEADER, RSC_CONTENT_TYPE_HEADER,
NEXT_ROUTER_STATE_TREE, NEXT_ROUTER_STATE_TREE_HEADER,
NEXT_DID_POSTPONE_HEADER, NEXT_DID_POSTPONE_HEADER,
} from '../client/components/app-router-headers' } from '../client/components/app-router-headers'
import { webpackBuild } from './webpack-build' import { webpackBuild } from './webpack-build'
...@@ -1136,7 +1136,7 @@ export default async function build( ...@@ -1136,7 +1136,7 @@ export default async function build(
header: RSC_HEADER, header: RSC_HEADER,
// This vary header is used as a default. It is technically re-assigned in `base-server`, // This vary header is used as a default. It is technically re-assigned in `base-server`,
// and may include an additional Vary option for `Next-URL`. // and may include an additional Vary option for `Next-URL`.
varyHeader: `${RSC_HEADER}, ${NEXT_ROUTER_STATE_TREE}, ${NEXT_ROUTER_PREFETCH_HEADER}`, varyHeader: `${RSC_HEADER}, ${NEXT_ROUTER_STATE_TREE_HEADER}, ${NEXT_ROUTER_PREFETCH_HEADER}`,
prefetchHeader: NEXT_ROUTER_PREFETCH_HEADER, prefetchHeader: NEXT_ROUTER_PREFETCH_HEADER,
didPostponeHeader: NEXT_DID_POSTPONE_HEADER, didPostponeHeader: NEXT_DID_POSTPONE_HEADER,
contentTypeHeader: RSC_CONTENT_TYPE_HEADER, contentTypeHeader: RSC_CONTENT_TYPE_HEADER,
...@@ -2699,7 +2699,7 @@ export default async function build( ...@@ -2699,7 +2699,7 @@ export default async function build(
// this flag is used to selectively bypass the static cache and invoke the lambda directly // this flag is used to selectively bypass the static cache and invoke the lambda directly
// to enable server actions on static routes // to enable server actions on static routes
const bypassFor: RouteHas[] = [ const bypassFor: RouteHas[] = [
{ type: 'header', key: ACTION }, { type: 'header', key: ACTION_HEADER },
{ {
type: 'header', type: 'header',
key: 'content-type', key: 'content-type',
......
export const RSC_HEADER = 'RSC' as const export const RSC_HEADER = 'RSC' as const
export const ACTION = 'Next-Action' as const export const ACTION_HEADER = 'Next-Action' as const
export const NEXT_ROUTER_STATE_TREE_HEADER = 'Next-Router-State-Tree' as const
export const NEXT_ROUTER_STATE_TREE = 'Next-Router-State-Tree' as const
export const NEXT_ROUTER_PREFETCH_HEADER = 'Next-Router-Prefetch' as const export const NEXT_ROUTER_PREFETCH_HEADER = 'Next-Router-Prefetch' as const
export const NEXT_URL = 'Next-Url' as const export const NEXT_URL = 'Next-Url' as const
export const RSC_CONTENT_TYPE_HEADER = 'text/x-component' as const export const RSC_CONTENT_TYPE_HEADER = 'text/x-component' as const
export const FLIGHT_PARAMETERS = [ export const FLIGHT_HEADERS = [
[RSC_HEADER], RSC_HEADER,
[NEXT_ROUTER_STATE_TREE], NEXT_ROUTER_STATE_TREE_HEADER,
[NEXT_ROUTER_PREFETCH_HEADER], NEXT_ROUTER_PREFETCH_HEADER,
] as const ] as const
export const NEXT_RSC_UNION_QUERY = '_rsc' as const export const NEXT_RSC_UNION_QUERY = '_rsc' as const
......
...@@ -18,7 +18,7 @@ import type { ...@@ -18,7 +18,7 @@ import type {
} from '../../../server/app-render/types' } from '../../../server/app-render/types'
import { import {
NEXT_ROUTER_PREFETCH_HEADER, NEXT_ROUTER_PREFETCH_HEADER,
NEXT_ROUTER_STATE_TREE, NEXT_ROUTER_STATE_TREE_HEADER,
NEXT_RSC_UNION_QUERY, NEXT_RSC_UNION_QUERY,
NEXT_URL, NEXT_URL,
RSC_HEADER, RSC_HEADER,
...@@ -53,7 +53,7 @@ export async function fetchServerResponse( ...@@ -53,7 +53,7 @@ export async function fetchServerResponse(
): Promise<FetchServerResponseResult> { ): Promise<FetchServerResponseResult> {
const headers: { const headers: {
[RSC_HEADER]: '1' [RSC_HEADER]: '1'
[NEXT_ROUTER_STATE_TREE]: string [NEXT_ROUTER_STATE_TREE_HEADER]: string
[NEXT_URL]?: string [NEXT_URL]?: string
[NEXT_ROUTER_PREFETCH_HEADER]?: '1' [NEXT_ROUTER_PREFETCH_HEADER]?: '1'
// A header that is only added in test mode to assert on fetch priority // A header that is only added in test mode to assert on fetch priority
...@@ -62,7 +62,7 @@ export async function fetchServerResponse( ...@@ -62,7 +62,7 @@ export async function fetchServerResponse(
// Enable flight response // Enable flight response
[RSC_HEADER]: '1', [RSC_HEADER]: '1',
// Provide the current router state // Provide the current router state
[NEXT_ROUTER_STATE_TREE]: encodeURIComponent( [NEXT_ROUTER_STATE_TREE_HEADER]: encodeURIComponent(
JSON.stringify(flightRouterState) JSON.stringify(flightRouterState)
), ),
} }
...@@ -84,7 +84,7 @@ export async function fetchServerResponse( ...@@ -84,7 +84,7 @@ export async function fetchServerResponse(
const uniqueCacheQuery = hexHash( const uniqueCacheQuery = hexHash(
[ [
headers[NEXT_ROUTER_PREFETCH_HEADER] || '0', headers[NEXT_ROUTER_PREFETCH_HEADER] || '0',
headers[NEXT_ROUTER_STATE_TREE], headers[NEXT_ROUTER_STATE_TREE_HEADER],
headers[NEXT_URL], headers[NEXT_URL],
].join(',') ].join(',')
) )
......
...@@ -5,8 +5,8 @@ import type { ...@@ -5,8 +5,8 @@ import type {
} from '../../../../server/app-render/types' } from '../../../../server/app-render/types'
import { callServer } from '../../../app-call-server' import { callServer } from '../../../app-call-server'
import { import {
ACTION, ACTION_HEADER,
NEXT_ROUTER_STATE_TREE, NEXT_ROUTER_STATE_TREE_HEADER,
NEXT_URL, NEXT_URL,
RSC_CONTENT_TYPE_HEADER, RSC_CONTENT_TYPE_HEADER,
} from '../../app-router-headers' } from '../../app-router-headers'
...@@ -63,8 +63,10 @@ async function fetchServerAction( ...@@ -63,8 +63,10 @@ async function fetchServerAction(
method: 'POST', method: 'POST',
headers: { headers: {
Accept: RSC_CONTENT_TYPE_HEADER, Accept: RSC_CONTENT_TYPE_HEADER,
[ACTION]: actionId, [ACTION_HEADER]: actionId,
[NEXT_ROUTER_STATE_TREE]: encodeURIComponent(JSON.stringify(state.tree)), [NEXT_ROUTER_STATE_TREE_HEADER]: encodeURIComponent(
JSON.stringify(state.tree)
),
...(process.env.NEXT_DEPLOYMENT_ID ...(process.env.NEXT_DEPLOYMENT_ID
? { ? {
'x-deployment-id': process.env.NEXT_DEPLOYMENT_ID, 'x-deployment-id': process.env.NEXT_DEPLOYMENT_ID,
......
...@@ -8,8 +8,8 @@ import type { BaseNextRequest, BaseNextResponse } from '../base-http' ...@@ -8,8 +8,8 @@ import type { BaseNextRequest, BaseNextResponse } from '../base-http'
import { import {
RSC_HEADER, RSC_HEADER,
RSC_CONTENT_TYPE_HEADER, RSC_CONTENT_TYPE_HEADER,
NEXT_ROUTER_STATE_TREE, NEXT_ROUTER_STATE_TREE_HEADER,
ACTION, ACTION_HEADER,
} from '../../client/components/app-router-headers' } from '../../client/components/app-router-headers'
import { isNotFoundError } from '../../client/components/not-found' import { isNotFoundError } from '../../client/components/not-found'
import { import {
...@@ -325,10 +325,10 @@ async function createRedirectRenderResult( ...@@ -325,10 +325,10 @@ async function createRedirectRenderResult(
} }
// Ensures that when the path was revalidated we don't return a partial response on redirects // Ensures that when the path was revalidated we don't return a partial response on redirects
forwardedHeaders.delete(NEXT_ROUTER_STATE_TREE) forwardedHeaders.delete(NEXT_ROUTER_STATE_TREE_HEADER)
// When an action follows a redirect, it's no longer handling an action: it's just a normal RSC request // When an action follows a redirect, it's no longer handling an action: it's just a normal RSC request
// to the requested URL. We should remove the `next-action` header so that it's not treated as an action // to the requested URL. We should remove the `next-action` header so that it's not treated as an action
forwardedHeaders.delete(ACTION) forwardedHeaders.delete(ACTION_HEADER)
try { try {
const response = await fetch(fetchUrl, { const response = await fetch(fetchUrl, {
......
...@@ -37,7 +37,7 @@ import { ...@@ -37,7 +37,7 @@ import {
import { stripInternalQueries } from '../internal-utils' import { stripInternalQueries } from '../internal-utils'
import { import {
NEXT_ROUTER_PREFETCH_HEADER, NEXT_ROUTER_PREFETCH_HEADER,
NEXT_ROUTER_STATE_TREE, NEXT_ROUTER_STATE_TREE_HEADER,
NEXT_URL, NEXT_URL,
RSC_HEADER, RSC_HEADER,
} from '../../client/components/app-router-headers' } from '../../client/components/app-router-headers'
...@@ -768,7 +768,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -768,7 +768,7 @@ async function renderToHTMLOrFlightImpl(
isRSCRequest && (!isPrefetchRSCRequest || !isRoutePPREnabled) isRSCRequest && (!isPrefetchRSCRequest || !isRoutePPREnabled)
const parsedFlightRouterState = parseAndValidateFlightRouterState( const parsedFlightRouterState = parseAndValidateFlightRouterState(
req.headers[NEXT_ROUTER_STATE_TREE.toLowerCase()] req.headers[NEXT_ROUTER_STATE_TREE_HEADER.toLowerCase()]
) )
/** /**
......
import type { IncomingHttpHeaders } from 'node:http' import type { IncomingHttpHeaders } from 'node:http'
import { FLIGHT_PARAMETERS } from '../../client/components/app-router-headers' import { FLIGHT_HEADERS } from '../../client/components/app-router-headers'
/** /**
* Removes the flight headers from the request. * Removes the flight headers from the request.
...@@ -8,7 +8,7 @@ import { FLIGHT_PARAMETERS } from '../../client/components/app-router-headers' ...@@ -8,7 +8,7 @@ import { FLIGHT_PARAMETERS } from '../../client/components/app-router-headers'
* @param req the request to strip the headers from * @param req the request to strip the headers from
*/ */
export function stripFlightHeaders(headers: IncomingHttpHeaders) { export function stripFlightHeaders(headers: IncomingHttpHeaders) {
for (const [header] of FLIGHT_PARAMETERS) { for (const header of FLIGHT_HEADERS) {
delete headers[header.toLowerCase()] delete headers[header.toLowerCase()]
} }
} }
...@@ -7,7 +7,7 @@ import type { WithStore } from './with-store' ...@@ -7,7 +7,7 @@ import type { WithStore } from './with-store'
import type { NextRequest } from '../web/spec-extension/request' import type { NextRequest } from '../web/spec-extension/request'
import type { __ApiPreviewProps } from '../api-utils' import type { __ApiPreviewProps } from '../api-utils'
import { FLIGHT_PARAMETERS } from '../../client/components/app-router-headers' import { FLIGHT_HEADERS } from '../../client/components/app-router-headers'
import { import {
HeadersAdapter, HeadersAdapter,
type ReadonlyHeaders, type ReadonlyHeaders,
...@@ -25,8 +25,8 @@ import type { RequestLifecycleOpts } from '../base-server' ...@@ -25,8 +25,8 @@ import type { RequestLifecycleOpts } from '../base-server'
function getHeaders(headers: Headers | IncomingHttpHeaders): ReadonlyHeaders { function getHeaders(headers: Headers | IncomingHttpHeaders): ReadonlyHeaders {
const cleaned = HeadersAdapter.from(headers) const cleaned = HeadersAdapter.from(headers)
for (const param of FLIGHT_PARAMETERS) { for (const header of FLIGHT_HEADERS) {
cleaned.delete(param.toString().toLowerCase()) cleaned.delete(header.toLowerCase())
} }
return HeadersAdapter.seal(cleaned) return HeadersAdapter.seal(cleaned)
......
...@@ -95,7 +95,7 @@ import { ...@@ -95,7 +95,7 @@ import {
NEXT_ROUTER_PREFETCH_HEADER, NEXT_ROUTER_PREFETCH_HEADER,
NEXT_DID_POSTPONE_HEADER, NEXT_DID_POSTPONE_HEADER,
NEXT_URL, NEXT_URL,
NEXT_ROUTER_STATE_TREE, NEXT_ROUTER_STATE_TREE_HEADER,
} from '../client/components/app-router-headers' } from '../client/components/app-router-headers'
import type { import type {
MatchOptions, MatchOptions,
...@@ -1817,7 +1817,7 @@ export default abstract class Server< ...@@ -1817,7 +1817,7 @@ export default abstract class Server<
isAppPath: boolean, isAppPath: boolean,
resolvedPathname: string resolvedPathname: string
): void { ): void {
const baseVaryHeader = `${RSC_HEADER}, ${NEXT_ROUTER_STATE_TREE}, ${NEXT_ROUTER_PREFETCH_HEADER}` const baseVaryHeader = `${RSC_HEADER}, ${NEXT_ROUTER_STATE_TREE_HEADER}, ${NEXT_ROUTER_PREFETCH_HEADER}`
const isRSCRequest = getRequestMeta(req, 'isRSCRequest') ?? false const isRSCRequest = getRequestMeta(req, 'isRSCRequest') ?? false
let addedNextUrlToVary = false let addedNextUrlToVary = false
......
...@@ -37,7 +37,7 @@ import { ...@@ -37,7 +37,7 @@ import {
prepareDestination, prepareDestination,
} from '../../../shared/lib/router/utils/prepare-destination' } from '../../../shared/lib/router/utils/prepare-destination'
import type { TLSSocket } from 'tls' import type { TLSSocket } from 'tls'
import { NEXT_ROUTER_STATE_TREE } from '../../../client/components/app-router-headers' import { NEXT_ROUTER_STATE_TREE_HEADER } from '../../../client/components/app-router-headers'
import { getSelectedParams } from '../../../client/components/router-reducer/compute-changed-path' import { getSelectedParams } from '../../../client/components/router-reducer/compute-changed-path'
import { isInterceptionRouteRewrite } from '../../../lib/generate-interception-routes-rewrites' import { isInterceptionRouteRewrite } from '../../../lib/generate-interception-routes-rewrites'
import { parseAndValidateFlightRouterState } from '../../app-render/parse-and-validate-flight-router-state' import { parseAndValidateFlightRouterState } from '../../app-render/parse-and-validate-flight-router-state'
...@@ -701,7 +701,7 @@ export function getResolveRoutes( ...@@ -701,7 +701,7 @@ export function getResolveRoutes(
// This attempts to extract the dynamic params from the provided router state. // This attempts to extract the dynamic params from the provided router state.
if (isInterceptionRouteRewrite(route as Rewrite)) { if (isInterceptionRouteRewrite(route as Rewrite)) {
const stateHeader = const stateHeader =
req.headers[NEXT_ROUTER_STATE_TREE.toLowerCase()] req.headers[NEXT_ROUTER_STATE_TREE_HEADER.toLowerCase()]
if (stateHeader) { if (stateHeader) {
rewriteParams = { rewriteParams = {
......
import type { IncomingMessage } from 'http' import type { IncomingMessage } from 'http'
import type { BaseNextRequest } from '../base-http' import type { BaseNextRequest } from '../base-http'
import type { NextRequest } from '../web/exports' import type { NextRequest } from '../web/exports'
import { ACTION } from '../../client/components/app-router-headers' import { ACTION_HEADER } from '../../client/components/app-router-headers'
export function getServerActionRequestMetadata( export function getServerActionRequestMetadata(
req: IncomingMessage | BaseNextRequest | NextRequest req: IncomingMessage | BaseNextRequest | NextRequest
...@@ -16,10 +16,10 @@ export function getServerActionRequestMetadata( ...@@ -16,10 +16,10 @@ export function getServerActionRequestMetadata(
let contentType: string | null let contentType: string | null
if (req.headers instanceof Headers) { if (req.headers instanceof Headers) {
actionId = req.headers.get(ACTION.toLowerCase()) ?? null actionId = req.headers.get(ACTION_HEADER.toLowerCase()) ?? null
contentType = req.headers.get('content-type') contentType = req.headers.get('content-type')
} else { } else {
actionId = (req.headers[ACTION.toLowerCase()] as string) ?? null actionId = (req.headers[ACTION_HEADER.toLowerCase()] as string) ?? null
contentType = req.headers['content-type'] ?? null contentType = req.headers['content-type'] ?? null
} }
......
...@@ -11,7 +11,7 @@ import { waitUntilSymbol } from './spec-extension/fetch-event' ...@@ -11,7 +11,7 @@ import { waitUntilSymbol } from './spec-extension/fetch-event'
import { NextURL } from './next-url' import { NextURL } from './next-url'
import { stripInternalSearchParams } from '../internal-utils' import { stripInternalSearchParams } from '../internal-utils'
import { normalizeRscURL } from '../../shared/lib/router/utils/app-paths' import { normalizeRscURL } from '../../shared/lib/router/utils/app-paths'
import { FLIGHT_PARAMETERS } from '../../client/components/app-router-headers' import { FLIGHT_HEADERS } from '../../client/components/app-router-headers'
import { ensureInstrumentationRegistered } from './globals' import { ensureInstrumentationRegistered } from './globals'
import { import {
withRequestStore, withRequestStore,
...@@ -133,13 +133,13 @@ export async function adapter( ...@@ -133,13 +133,13 @@ export async function adapter(
const requestHeaders = fromNodeOutgoingHttpHeaders(params.request.headers) const requestHeaders = fromNodeOutgoingHttpHeaders(params.request.headers)
const flightHeaders = new Map() const flightHeaders = new Map()
// Parameters should only be stripped for middleware // Headers should only be stripped for middleware
if (!isEdgeRendering) { if (!isEdgeRendering) {
for (const param of FLIGHT_PARAMETERS) { for (const header of FLIGHT_HEADERS) {
const key = param.toString().toLowerCase() const key = header.toLowerCase()
const value = requestHeaders.get(key) const value = requestHeaders.get(key)
if (value) { if (value) {
flightHeaders.set(key, requestHeaders.get(key)) flightHeaders.set(key, value)
requestHeaders.delete(key) requestHeaders.delete(key)
} }
} }
......
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