Unverified Commit 893121b8 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

Fix hybrid Pages/App Router not-found rendering with adapters (#96392)

## Summary

When Pages Router and App Router routes coexist, a Pages Router route
that returns `notFound` will internally hand rendering to the App Router
not-found page. Next.js [prefers the App Router `/_not-found` entry for
404
responses](https://github.com/vercel/next.js/blob/5f5e4773bbb54f19697063ea093e8d89ca7d081a/packages/next/src/server/base-server.ts#L2965-L2980)
before falling back to a Pages Router `/404` page.

When deployed via an adapter, this means it would have attempted to
resume the Pages entry, which wouldn't contain the relevant prerendered
shell or postponed state. If the not-found page suspends, its response
cannot be resumed using the metadata associated with the Pages Router
output.

For adapter requests, this marks the handoff with an empty postponed
state. This uses the existing resume protocol to perform a complete,
non-cacheable dynamic render. `next start` continues to use the normal
PPR path, where it can load and resume the App Router not-found output
directly.

This trades PPR shell reuse for correctness only during this
cross-router not-found handoff. Longer term, adapters should have an
explicit contract for cross-output handoffs so they can select and
resume the App Router not-found output normally, without necessarily
declaring the pages router output PPR-capable.

The change also documents the empty postponed-state contract and adds
deployment regression coverage.

## Verification

- Added an end-to-end test that verifies a Pages Router `notFound`
response includes the dynamic App Router not-found content when
deployed.
- `pnpm testheadless test/e2e/app-dir/pages-router-app-not-found`
- `pnpm --filter=next types`

<!-- NEXT_JS_LLM -->
parent a8dcd256
...@@ -155,10 +155,13 @@ export interface RequestMeta { ...@@ -155,10 +155,13 @@ export interface RequestMeta {
isNextDataReq?: true isNextDataReq?: true
/** /**
* Postponed state to use for resumption. If present it's assumed that the * Postponed state to use for resumption. When absent, the request is not a
* request is for a page that has postponed (there are no guarantees that the * resume request. A non-empty string contains the state to resume, while an
* page actually has postponed though as it would incur an additional cache * empty string represents a resume request without postponed state and
* lookup). * signals that the renderer should perform a full dynamic render.
*
* There are no guarantees that the page actually postponed, as verifying
* that would incur an additional cache lookup.
*/ */
postponed?: string postponed?: string
......
...@@ -116,6 +116,21 @@ export const getHandler = ({ ...@@ -116,6 +116,21 @@ export const getHandler = ({
const render404 = async () => { const render404 = async () => {
// TODO: should route-module itself handle rendering the 404 // TODO: should route-module itself handle rendering the 404
if (routerServerContext?.render404) { if (routerServerContext?.render404) {
// When the Pages and App Routers coexist, a Pages Router 404 renders
// the App Router not-found page. A direct route-module invocation
// cannot provide the postponed state needed to resume that App Router
// output, so an empty postponed state signals that the renderer must
// perform a complete dynamic render instead.
//
// TODO: Re-enter routing with the App Router not-found output so its
// prerender and postponed state can be selected and resumed.
if (
nextConfig.cacheComponents &&
!routerServerContext.isWrappedByNextServer &&
typeof getRequestMeta(req, 'postponed') !== 'string'
) {
addRequestMeta(req, 'postponed', '')
}
await routerServerContext.render404(req, res, parsedUrl, false) await routerServerContext.render404(req, res, parsedUrl, false)
} else { } else {
res.end('This page could not be found') res.end('This page could not be found')
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { Suspense } from 'react'
import { cookies } from 'next/headers'
async function DynamicNotFound() {
const cookieStore = await cookies()
const marker = cookieStore.get('not-found-marker')?.value ?? 'missing'
return <p id="dynamic-not-found">{marker}</p>
}
export default function NotFound() {
return (
<main>
<h1>App Router not found</h1>
<Suspense fallback={<p>Loading not-found content...</p>}>
<DynamicNotFound />
</Suspense>
</main>
)
}
export default function Page() {
return <p>App Router home</p>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
describe('pages-router-app-not-found', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('fully renders an app not-found selected by a pages route', async () => {
const marker = `not-found-${Date.now()}`
const res = await next.fetch(`/pages-route/${marker}`, {
headers: {
cookie: `not-found-marker=${marker}`,
},
})
const html = await res.text()
expect(res.status).toBe(404)
expect(html).toContain('App Router not found')
expect(html).toContain(marker)
})
})
export function getStaticPaths() {
return {
paths: [],
fallback: 'blocking',
}
}
export function getStaticProps() {
return {
notFound: true,
revalidate: 1,
}
}
export default function PagesRoute() {
return <p>This page should not render.</p>
}
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