-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move request header parsing for
app-render
into a dedicated function
Previously, parsing options from the request headers was a bit scattered accross the `renderToHTMLOrFlightImpl` function.
- Loading branch information
1 parent
deab0f6
commit ccb7dc7
Showing
2 changed files
with
62 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/next/src/server/app-render/parse-request-headers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { IncomingHttpHeaders } from 'http' | ||
import { | ||
NEXT_ROUTER_PREFETCH_HEADER, | ||
NEXT_ROUTER_STATE_TREE_HEADER, | ||
RSC_HEADER, | ||
} from '../../client/components/app-router-headers' | ||
import { getScriptNonceFromHeader } from './get-script-nonce-from-header' | ||
import { parseAndValidateFlightRouterState } from './parse-and-validate-flight-router-state' | ||
import type { FlightRouterState } from './types' | ||
|
||
export interface ParseRequestHeadersOptions { | ||
readonly isRoutePPREnabled: boolean | ||
} | ||
|
||
export interface ParsedRequestHeaders { | ||
/** | ||
* Router state provided from the client-side router. Used to handle rendering | ||
* from the common layout down. This value will be undefined if the request is | ||
* not a client-side navigation request, or if the request is a prefetch | ||
* request. | ||
*/ | ||
readonly flightRouterState: FlightRouterState | undefined | ||
readonly isPrefetchRequest: boolean | ||
readonly isRSCRequest: boolean | ||
readonly nonce: string | undefined | ||
} | ||
|
||
// We read these values from the request object as, in certain cases, | ||
// base-server will strip them to opt into different rendering behavior. | ||
export function parseRequestHeaders( | ||
headers: IncomingHttpHeaders, | ||
options: ParseRequestHeadersOptions | ||
): ParsedRequestHeaders { | ||
const isPrefetchRequest = | ||
headers[NEXT_ROUTER_PREFETCH_HEADER.toLowerCase()] !== undefined | ||
|
||
const isRSCRequest = headers[RSC_HEADER.toLowerCase()] !== undefined | ||
|
||
const shouldProvideFlightRouterState = | ||
isRSCRequest && (!isPrefetchRequest || !options.isRoutePPREnabled) | ||
|
||
const flightRouterState = shouldProvideFlightRouterState | ||
? parseAndValidateFlightRouterState( | ||
headers[NEXT_ROUTER_STATE_TREE_HEADER.toLowerCase()] | ||
) | ||
: undefined | ||
|
||
const csp = | ||
headers['content-security-policy'] || | ||
headers['content-security-policy-report-only'] | ||
|
||
const nonce = | ||
typeof csp === 'string' ? getScriptNonceFromHeader(csp) : undefined | ||
|
||
return { flightRouterState, isPrefetchRequest, isRSCRequest, nonce } | ||
} |