Unverified Commit 00c4ba8b authored by Josh Story's avatar Josh Story Committed by GitHub

Hoist app page request URL into render context (#99269)

## Summary

Pass the final app-page request URL explicitly from the Node, Edge, and
build export entry points into rendering. This keeps URL parsing and the
deterministic prerender request ID tied to the exact URL observed on the
request after existing normalization.

Development ISR status bookkeeping uses the forwarded URL with its
existing URL parsing behavior.

A path-scoped lint guard prevents new direct `req.url` reads in
app-render.

## Verification

- `pnpm --filter=next types`
- `pnpm --filter=next build`
- Confirmed `rg -n '\breq\.url\b' packages/next/src/server/app-render`
has no matches.
- ESLint stdin probes reject `req.url` under app-render with the
`requestUrl` guidance and allow the same access outside app-render.
- Not run: integration tests (the worktree lacks per-package
dependencies, so the full bootstrap build could not complete).

<!-- NEXT_JS_LLM -->
parent edd8011d
......@@ -457,6 +457,19 @@ export default defineConfig([
'@next/internal/no-ambiguous-jsx': 'error',
},
},
{
files: ['packages/next/src/server/app-render/**/*.{ts,tsx}'],
rules: {
'no-restricted-properties': [
'error',
{
object: 'req',
property: 'url',
message: 'Use the explicit `requestUrl` input instead.',
},
],
},
},
{
files: ['examples/**/*'],
linterOptions: {
......
......@@ -895,6 +895,7 @@ export function createAppPageEntrypoint({
const routeMatch: RouteMatch = { resolvedPathname }
const dev: DevRenderContext | undefined = createDevRenderContext(req)
const context: AppPageRouteHandlerContext = {
requestUrl: req.url!,
query,
params,
page: normalizedSrcPage,
......
......@@ -123,6 +123,7 @@ async function requestHandler(
AppPageRouteHandlerContext,
'parsedRequestHeaders'
> = {
requestUrl: baseReq.url,
page: normalizedSrcPage,
routeMatch,
query,
......
......@@ -48,6 +48,7 @@ import {
export async function exportAppPage(
req: MockedRequest,
res: MockedResponse,
requestUrl: string,
page: string,
path: string,
pathname: string,
......@@ -96,6 +97,7 @@ export async function exportAppPage(
const result = await lazyPrerenderAppPage(
nextReq,
new NodeNextResponse(res),
requestUrl,
pathname,
query,
fallbackRouteParams,
......
......@@ -328,6 +328,7 @@ async function exportPageImpl(
return exportAppPage(
req,
res,
req.url,
page,
path,
pathname,
......
......@@ -2555,7 +2555,7 @@ type PreparedAppPageRender = {
loaderTree: LoaderTree
}
type GenerateRequestId = (req: BaseNextRequest) => string | Promise<string>
type GenerateRequestId = (requestUrl: string) => string | Promise<string>
const generateRenderRequestId: GenerateRequestId = () => {
if (process.env.NEXT_RUNTIME === 'edge') {
......@@ -2567,9 +2567,9 @@ const generateRenderRequestId: GenerateRequestId = () => {
}
}
const generatePrerenderRequestId: GenerateRequestId = async (req) => {
const generatePrerenderRequestId: GenerateRequestId = async (requestUrl) => {
return Buffer.from(
await crypto.subtle.digest('SHA-1', Buffer.from(req.url))
await crypto.subtle.digest('SHA-1', Buffer.from(requestUrl))
).toString('hex')
}
......@@ -2615,6 +2615,7 @@ function initializeClientComponentLoadTracking(
async function prepareAppPageRender(
req: BaseNextRequest,
res: BaseNextResponse,
requestUrl: string,
url: ReturnType<typeof parseRelativeUrl>,
pagePath: string,
query: NextParsedUrlQuery,
......@@ -2707,7 +2708,7 @@ async function prepareAppPageRender(
requestId = requestInsightsIdentity.requestId
} else {
// Otherwise we generate a new request ID.
requestId = await generateRequestId(req)
requestId = await generateRequestId(requestUrl)
}
// If the client has provided an HTML request ID, we use it to associate the
......@@ -3207,6 +3208,7 @@ async function renderAppPage(
async function renderToHTMLOrFlightImpl(
req: BaseNextRequest,
res: BaseNextResponse,
requestUrl: string,
url: ReturnType<typeof parseRelativeUrl>,
pagePath: string,
query: NextParsedUrlQuery,
......@@ -3229,6 +3231,7 @@ async function renderToHTMLOrFlightImpl(
const prepared = await prepareAppPageRender(
req,
res,
requestUrl,
url,
pagePath,
query,
......@@ -3256,6 +3259,7 @@ async function renderToHTMLOrFlightImpl(
async function prerenderToHTMLOrFlightImpl(
req: BaseNextRequest,
res: BaseNextResponse,
requestUrl: string,
url: ReturnType<typeof parseRelativeUrl>,
pagePath: string,
query: NextParsedUrlQuery,
......@@ -3277,6 +3281,7 @@ async function prerenderToHTMLOrFlightImpl(
const prepared = await prepareAppPageRender(
req,
res,
requestUrl,
url,
pagePath,
query,
......@@ -3304,6 +3309,7 @@ async function prerenderToHTMLOrFlightImpl(
export type AppPageRender = (
req: BaseNextRequest,
res: BaseNextResponse,
requestUrl: string,
pagePath: string,
query: NextParsedUrlQuery,
fallbackRouteParams: OpaqueFallbackRouteParams | null,
......@@ -3326,15 +3332,16 @@ type AppPagePreparation = {
function prepareAppPage(
req: BaseNextRequest,
requestUrl: string,
pagePath: string,
fallbackRouteParams: OpaqueFallbackRouteParams | null,
renderOpts: RenderOpts
): AppPagePreparation {
if (!req.url) {
if (!requestUrl) {
throw new Error('Invalid URL')
}
const url = parseRelativeUrl(req.url, undefined, false)
const url = parseRelativeUrl(requestUrl, undefined, false)
const interpolatedParams = interpolateParallelRouteParams(
renderOpts.ComponentMod.routeModule.userland.loaderTree,
......@@ -3393,6 +3400,7 @@ function prepareAppPage(
export const renderToHTMLOrFlight: AppPageRender = (
req,
res,
requestUrl,
pagePath,
query,
fallbackRouteParams,
......@@ -3404,6 +3412,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
) => {
const { url, interpolatedParams, postponedState } = prepareAppPage(
req,
requestUrl,
pagePath,
fallbackRouteParams,
renderOpts
......@@ -3426,6 +3435,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
renderToHTMLOrFlightImpl,
req,
res,
requestUrl,
url,
pagePath,
query,
......@@ -3444,6 +3454,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
export const prerenderToHTMLOrFlight: AppPagePrerender = (
req,
res,
requestUrl,
pagePath,
query,
fallbackRouteParams,
......@@ -3455,6 +3466,7 @@ export const prerenderToHTMLOrFlight: AppPagePrerender = (
) => {
const { url, interpolatedParams } = prepareAppPage(
req,
requestUrl,
pagePath,
fallbackRouteParams,
renderOpts
......@@ -3477,6 +3489,7 @@ export const prerenderToHTMLOrFlight: AppPagePrerender = (
prerenderToHTMLOrFlightImpl,
req,
res,
requestUrl,
url,
pagePath,
query,
......
......@@ -704,6 +704,7 @@ export default class NextNodeServer extends BaseServer<
const result = await renderAppPage(
req,
res,
req.url,
pathname,
query,
// This code path does not service revalidations for unknown param
......
......@@ -86,6 +86,9 @@ export type DevRenderContext = {
}
export interface AppPageRouteHandlerContext extends RouteModuleHandleContext {
// Exact path-relative URL (pathname plus search) after route preparation,
// preserving its encoded bytes for rendering and prerender request IDs.
requestUrl: string
page: string
routeMatch: RouteMatch
query: NextParsedUrlQuery
......@@ -179,6 +182,7 @@ export class AppPageRouteModule extends RouteModule<
return renderToHTMLOrFlight(
req,
res,
context.requestUrl,
context.page,
context.query,
context.fallbackRouteParams,
......@@ -202,6 +206,7 @@ export class AppPageRouteModule extends RouteModule<
return prerenderToHTMLOrFlight(
req,
res,
context.requestUrl,
context.page,
context.query,
context.fallbackRouteParams,
......
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