Unverified Commit f2ddd134 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Fix non-RSC router prefetch handling (#94553)

### What?

Ignore router prefetch protocol headers when the request is not
classified as an RSC request. Add a dedicated force-dynamic App Router
fixture that verifies HTML and valid RSC prefetch behavior.

### Why?

A non-RSC HTML request with `Next-Router-Prefetch: 1` could enter the
partial prefetch tree path, produce a null active cache node, and
suspend the HTML render until timeout.

### How?

Determine `isRSCRequest` before parsing prefetch modes, then require it
for prefetch modes `1`, `2`, and `3`, including route-tree prefetch
classification. The regression fixture uses an abort timeout to fail
quickly if the HTML response hangs and separately confirms RSC
prefetches remain valid.

### Verification

- `pnpm build-all`
- `NEXT_TEST_PREFER_OFFLINE=1 pnpm test-start-turbo
test/e2e/app-dir/non-rsc-router-prefetch/non-rsc-router-prefetch.test.ts`
- `NEXT_TEST_PREFER_OFFLINE=1 pnpm test-start-webpack
test/e2e/app-dir/non-rsc-router-prefetch/non-rsc-router-prefetch.test.ts`
- `pnpm --filter=next types`

<!-- NEXT_JS_LLM_PR -->
parent 1cad9e98
...@@ -397,21 +397,24 @@ function parseRequestHeaders( ...@@ -397,21 +397,24 @@ function parseRequestHeaders(
headers: IncomingHttpHeaders, headers: IncomingHttpHeaders,
options: ParseRequestHeadersOptions options: ParseRequestHeadersOptions
): ParsedRequestHeaders { ): ParsedRequestHeaders {
const isRSCRequest = isRSCRequestHeader(headers[RSC_HEADER])
// runtime prefetch requests are *not* treated as prefetch requests // runtime prefetch requests are *not* treated as prefetch requests
// (TODO: this is confusing, we should refactor this to express this better) // (TODO: this is confusing, we should refactor this to express this better)
const isPrefetchRequest = headers[NEXT_ROUTER_PREFETCH_HEADER] === '1' const isPrefetchRequest =
isRSCRequest && headers[NEXT_ROUTER_PREFETCH_HEADER] === '1'
const isAppShellPrefetchRequest = headers[NEXT_ROUTER_PREFETCH_HEADER] === '3' const isAppShellPrefetchRequest =
isRSCRequest && headers[NEXT_ROUTER_PREFETCH_HEADER] === '3'
// App Shell prefetches are a subtype of runtime prefetch — same code path, // App Shell prefetches are a subtype of runtime prefetch — same code path,
// but with less resolved content (omitting link data) // but with less resolved content (omitting link data)
const isRuntimePrefetchRequest = const isRuntimePrefetchRequest =
headers[NEXT_ROUTER_PREFETCH_HEADER] === '2' || isAppShellPrefetchRequest isRSCRequest &&
(headers[NEXT_ROUTER_PREFETCH_HEADER] === '2' || isAppShellPrefetchRequest)
const isHmrRefresh = headers[NEXT_HMR_REFRESH_HEADER] !== undefined const isHmrRefresh = headers[NEXT_HMR_REFRESH_HEADER] !== undefined
const isRSCRequest = isRSCRequestHeader(headers[RSC_HEADER])
const shouldProvideFlightRouterState = const shouldProvideFlightRouterState =
isRSCRequest && (!isPrefetchRequest || !options.isRoutePPREnabled) isRSCRequest && (!isPrefetchRequest || !options.isRoutePPREnabled)
...@@ -421,7 +424,7 @@ function parseRequestHeaders( ...@@ -421,7 +424,7 @@ function parseRequestHeaders(
// Checks if this is a prefetch of the Route Tree by the Segment Cache // Checks if this is a prefetch of the Route Tree by the Segment Cache
const isRouteTreePrefetchRequest = const isRouteTreePrefetchRequest =
headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER] === '/_tree' isRSCRequest && headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER] === '/_tree'
const csp = const csp =
headers['content-security-policy'] || headers['content-security-policy'] ||
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export const dynamic = 'force-dynamic'
export default function Page() {
return <p>hello world</p>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: false,
experimental: {
cachedNavigations: false,
},
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import {
NEXT_ROUTER_PREFETCH_HEADER,
RSC_HEADER,
} from 'next/dist/client/components/app-router-headers'
describe('non-rsc-router-prefetch', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (skipped) {
return
}
beforeAll(async () => {
const res = await next.fetch('/')
await res.text()
})
it('ignores the router prefetch header for HTML requests', async () => {
const res = await next.fetch('/', {
headers: {
[NEXT_ROUTER_PREFETCH_HEADER]: '1',
},
signal: AbortSignal.timeout(5_000),
})
const html = await res.text()
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('text/html')
expect(html).toContain('hello world')
})
it('honors the router prefetch header for RSC requests', async () => {
const res = await next.fetch('/', {
headers: {
[RSC_HEADER]: '1',
[NEXT_ROUTER_PREFETCH_HEADER]: '1',
},
})
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('text/x-component')
})
})
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