Repository navigation
fix(head): ignore next/head in the App Router like Next.js - #4610
Conversation
Next.js aliases next/head to a no-op in server components and gives client components a HeadManagerContext without mountedInstances, so App Router documents only carry Metadata API tags. vinext's client Head reconciled document.head and document.title on App Router pages, so a client layout shared with the Pages Router overrode the metadata title, and a server component rendering <Head> crashed on useRef. Head now renders nothing in an App Router tree, using the detection next/dynamic already relies on (moved to app-router-tree-context). Fixes #4589
|
/bigbonk review for issues |
@vinext/cloudflare
create-vinext-app
@vinext/types
vinext
@cloudflare/workers-response-store
commit: |
|
Performance benchmarksCompared 0 improved · 0 regressed · 7 within ±1.5%
View detailed results and traces 🟢 improvement · 🔴 regression · ⚫ change below 1.5% · paired base/head |
| await waitForAppRouterHydration(page); | ||
| await expect(page.locator("#next-head-layout-mounted")).toBeVisible(); | ||
|
|
||
| await page.click("#next-head-server-link"); |
There was a problem hiding this comment.
This test can still pass if the link unexpectedly falls back to a full document navigation: the destination SSR output has the same title and absent next/head tags. Since the case specifically claims to cover client navigation, set a window sentinel before the click and assert it survives afterward (as the other soft-navigation tests do), so a regression in the navigation path cannot mask a broken client-side metadata update.
There was a problem hiding this comment.
Fixed in 2c147c2: the test now sets a window marker before the click and asserts it survives the navigation.
|
Review: 1 finding. Posted 1 inline test-coverage finding. Production code had no actionable defects. |
|
/bigbonk review for issues |
|
LGTM! |
There was a problem hiding this comment.
✅ Approved — The fix matches Next.js: next/head does nothing in App Router trees and the Pages Router behaviour is unchanged. The detection hook was moved without changes, and the e2e tests cover both routers. I found no actionable issues in the changed code.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The change makes next/head render nothing in App Router trees. It adds no input handling, authorization or secret exposure.
📖 AGENTS.md: ✅ LGTM
This pull request does not touch the agent instructions.
📋 Code Quality: ✅ LGTM
Head calls the detection hook on every render and then picks between an empty fragment and PagesRouterHead, so it follows the rules of hooks. The shared useIsAppRouterTree() was moved without behaviour changes.
⚡ Performance: ✅ LGTM
Head now makes one cheap detection call per render, and App Router renders skip head collection and the DOM sync. Moving the hook adds about 0.1 kB gzip to the shared client chunk, which is negligible.
📝 Documentation: ✅ LGTM
The new JSDoc on Head and on useIsAppRouterTree explains the App Router behaviour and gives the Next.js rationale.
Reviewed commit: 2c147c22
Brings in the 16 upstream commits up to cloudflare#4626. Where upstream fixed an issue mriago/next already fixed, the official fix stays and ours goes: - cloudflare#4612 (/404 in dev, cloudflare#4591): drops our dev-server /404 status and its test. Ours also answered 404 for /_next/data/.../404.json, which Next answers with 200. - cloudflare#4611 (SIGTERM, cloudflare#4588): drops server/graceful-shutdown.ts, the gracefulShutdown option in cli.ts and standalone.ts, and its tests. closeServerOnTerminationSignals keeps NEXT_MANUAL_SIG_HANDLE and the 130/143 exit codes, and also drains waitUntil work. - cloudflare#4610 (next/head in the App Router, cloudflare#4589): drops head.react-server.ts, HeadManagerContext and its provider in app-route-tree.ts, the Pages-only resolveId exception and the fixture's legacy-head.tsx, with their tests. useIsAppRouterTree returns true in react-server, so a Server Component's <Head> no longer reaches useRef, and next/head keeps resolving to the full shim, so Pages-only Workers builds keep resetSSRHead and the rest of the exports. - cloudflare#4613 (pages/ dep scan, cloudflare#4592): drops our client-only crawl and tests/optimize-deps-hybrid-pages.test.ts; upstream crawls pages/ in both ssr and client. - cloudflare#4616 (invalid host / `//`, cloudflare#4600): drops our try in isReferencedAsset and its test. - cloudflare#4607 and cloudflare#4606 (tests cloudflare#4585, cloudflare#4586): drops our NODE_PATH reset in deploy.test.ts and our realpath change in the action-owner e2e. cloudflare#4617 moved the App Router navigation runtime to shims/app-router-navigation.ts. Our changes to navigation.ts (static shell prefetches, Partial Prefetching kinds, app-prefetch-setup cancellation) move with it; only the useSearchParams shell hole stays in navigation.ts. The non-reusable prefetch branch keeps cloudflare#4615's sharedBehavior and overrides prefetchKind/prepareSnapshot for static shells. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Builds on cloudflare#4610. Upstream makes next/head render nothing in the App Router, which matches Next.js, but Next.js also tells the developer: next-flight-loader warns once when a Server Component imports next/head, pointing at the Metadata API. Without it, a <Head> in a Server Component is silently dropped. Warn from a transform in the rsc environment, only when the App Router is enabled and the module has no "use client" directive. A Pages-only Workers build also names its server environment "rsc" and renders Pages there, so it is skipped, and next/head keeps resolving to the full shim everywhere. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fixes #4589
Summary
Next.js ignores
next/headin the App Router. On App Router routes vinext applied it, so a client layout shared with the Pages Router overwrote themetadatatitle, and an App server component that rendered<Head>returned a 500.Root cause
Headalways reconcileddocument.headanddocument.title. After hydration on an App Router route, the metadata title became thenext/headtitle, and a secondviewport, a seconddescription, and the layout's own meta tags appeared.HeadcallsuseRef, which does not exist in the react-server build. A server component that rendered<Head>failed withuseRef only works in Client Components(500).How Next.js handles it:
next/headtonoop-head(webpack:create-compiler-aliases.ts; Turbopack:next_import_map.rs).HeadManagerContextas{ appDir: true }(client/app-index.tsx,app-render.tsx). It has nomountedInstances, soside-effect.tsxnever updates the head.Fix
Headnow renders nothing in an App Router tree. It uses the detectionnext/dynamicalready relies on: the App Router SSR context,window.next.appDirin the browser, and the RSC environment, which only the App Router uses. That hook moved fromdynamic.tsintoapp-router-tree-context.tsasuseIsAppRouterTree(), so both shims share it. The Pages Router path (PagesRouterHead) is unchanged.Next.js probe (next@16.2.7,
next build --webpackand Turbopack)The probe used a root layout with
metadata.title, a client component that renders<Head><title>and metas, and a server component page that renders<Head>. The two bundlers gave identical results:Metadata Title, nonext/headtagsnext/headtitle, duplicate viewport/description, client meta<Head>useRef)<Head>Tests
New
tests/e2e/app-router/next-head-ignored.spec.tsuses newapp-basicfixture routes: a client layout shared by/next-head-ignored(App) and/next-head-shared(Pages), and an App server component that renders<Head>.Local validation:
CI=1 PLAYWRIGHT_PROJECT=app-router npx playwright test tests/e2e/app-router/next-head-ignored.spec.ts --project=app-routervp test run tests/head.test.ts tests/dynamic.test.ts tests/shims.test.ts tests/document.test.ts tests/routing.test.ts tests/route-sorting.test.tsvp checkon the changed filesBundle note
A production client build of
app-basicmovesapp-router-tree-contextfrom thenext/dynamicchunk into the shared chunk that holdshead. The shared chunk grows by 0.31 kB raw (0.11 kB gzip) and the dynamic chunk shrinks by 0.18 kB. This is a runtime-only shim change, so I didn't run build or dev cold-start benchmarks.