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