Repository navigation
fix(pages-router): render app/not-found for a hybrid Pages notFound - #3771
AhmedElBanna80 wants to merge 6 commits into
Conversation
In an app with both app/ and pages/, a Pages route that resolves to notFound (getStaticProps/getServerSideProps notFound, or a fallback: false miss) rendered pages/404. Next.js renders the App Router not-found in that case: with the app directory enabled, renderErrorToResponseImpl looks up /_not-found first and only falls back to /404 when it is missing. The hybrid bridge now asks the Pages renderer to mark a document 404 that came from a notFound result (x-vinext-pages-not-found). The App request stage consumes the marker and renders its not-found in place, keeping the source route's Cache-Control as pages-handler.ts does before render404. Middleware headers can't set the marker, and a page that renders itself with a 404 status is not marked. Production only; the dev server renders hybrid Pages through its own path. Closes cloudflare#3428
@vinext/cloudflare
create-vinext-app
@vinext/types
vinext
@cloudflare/workers-response-store
commit: |
Performance benchmarksCompared 1 improved · 0 regressed · 6 within ±1.5%
View detailed results and traces 🟢 improvement · 🔴 regression · ⚫ change below 1.5% · paired base/head |
|
CI: the one failing integration test (app-router-production-server, "lets a concrete Pages data route win a middleware rewrite over an App catch-all") also fails on main in runs 37763690664 and 37759531306. |
…arker, HEAD and the default not-found Cover the data-request exclusion, middleware being unable to set the x-vinext-pages-not-found marker, HEAD, and an app without app/not-found (Next.js adds its built-in /_not-found, so pages/404 is not reached). Two redundant pieces go: a header delete that ran on the App response, which never carries the marker, and the `!isDataReq` clause on the marking branch. A data request's notFound already resolves to a JSON response before that branch (buildPagesNotFoundResult), so the clause could not be reached; the unit test now pins that instead.
|
Pushed a follow-up commit that tightens the tests and drops two redundant bits of code. Tests added: /_next/data requests stay JSON 404s (no marker), a middleware that sets x-vinext-pages-not-found on a page that renders fine can't turn it into a 404, HEAD, and an app with no app/not-found. That last case matches Next.js: with the app directory enabled it adds the built-in /_not-found entry whenever the app has pages (build/route-discovery.ts), and renderErrorToResponseImpl looks it up before /404, so pages/404 isn't reached there either. Removed: a header delete in withPagesNotFoundCacheControl (it ran on the App response, which never carries the marker) and the |
# Conflicts: # packages/vinext/src/server/app-rsc-handler.ts
|
This pull request was not reviewed automatically because its author does not have write access to this repository. A maintainer with write access can start a review by commenting |
# Conflicts: # packages/vinext/src/server/pages-page-handler.ts
…p-not-found # Conflicts: # packages/vinext/src/server/app-rsc-handler.ts # packages/vinext/src/server/headers.ts
Closes #3428.
In a hybrid app, a Pages route that resolves to
notFound(gSP/gSSPnotFound, or afallback: falsemiss) renderspages/404. Next.js rendersapp/not-foundinstead: with the app dir enabled,renderErrorToResponseImpltries/_not-foundbefore/404, andpages-handler.tssendsnotFoundthroughrender404.Fixtures from
test/e2e/app-dir/not-found-with-pages-i18nandpages-router-app-not-found, built on 1.0.1:next start/ this PR/app-dir/foo(AppnotFound())/,/foo(PagesnotFound)pages/404/pages-route/x(nopages/404)_errorHow:
pages-page-handler.ts: newmarkNotFoundrender option. With it, a document 404 from anotFoundresult getsx-vinext-pages-not-found. Data requests and pages that set their own 404 status aren't marked.app-pages-bridge.ts: passesmarkNotFoundfor document renders, and drops the header if middleware sets it.app-rsc-handler.ts: the unmatched-route not-found render moves intorenderAppNotFound(). When the Pages response is marked,renderPagesForMatchKindrenders it instead; otherwise the marker is stripped. Doing it in the request handler covers both the single-stage path and the splithybrid-pages→app-not-foundpath, and later rewrites don't run once a Pages route has matched.s-maxage=1, ...forrevalidate: 1). Next sets it from thenotFoundrevalidate beforerender404, and the App render only sets Cache-Control when none is present.The marker follows the
x-vinext-special-error-statuspattern. If you'd prefer a different shape, I'm happy to change it.Dev isn't covered:
dev-server.tsrenders hybrid Pages separately and still showspages/404. I can do that in a follow-up.Tests:
tests/hybrid-pages-not-found.test.tsbuilds the fixture and checks gSP/gSSPnotFound,fallback: false, a non-default locale, Cache-Control, thecookies()content inside the not-found, and that found routes still render. Unit tests cover the handler (single and split stage), the bridge, and the Pages handler. Reverting any one of the three source files makes them fail.