Skip to content

fix(head): ignore next/head in the App Router like Next.js - #4610

Merged
james-elicx merged 2 commits into
mainfrom
claude/fix-4589-next-head-app-router
Nov 9, 2026
Merged

james-elicx merged 2 commits into
mainfrom
claude/fix-4589-next-head-app-router

Conversation

@james-elicx

Copy link
Copy Markdown
Member

Fixes #4589

Summary

Next.js ignores next/head in the App Router. On App Router routes vinext applied it, so a client layout shared with the Pages Router overwrote the metadata title, and an App server component that rendered <Head> returned a 500.

Root cause

  • Browser: the client Head always reconciled document.head and document.title. After hydration on an App Router route, the metadata title became the next/head title, and a second viewport, a second description, and the layout's own meta tags appeared.
  • RSC: Head calls useRef, which does not exist in the react-server build. A server component that rendered <Head> failed with useRef only works in Client Components (500).

How Next.js handles it:

  • In server components, Next aliases next/head to noop-head (webpack: create-compiler-aliases.ts; Turbopack: next_import_map.rs).
  • In client components, the App Router provides HeadManagerContext as { appDir: true } (client/app-index.tsx, app-render.tsx). It has no mountedInstances, so side-effect.tsx never updates the head.
  • Turbopack prints no warning for this.

Fix

Head now renders nothing in an App Router tree. It uses the detection next/dynamic already relies on: the App Router SSR context, window.next.appDir in the browser, and the RSC environment, which only the App Router uses. That hook moved from dynamic.ts into app-router-tree-context.ts as useIsAppRouterTree(), so both shims share it. The Pages Router path (PagesRouterHead) is unchanged.

Next.js probe (next@16.2.7, next build --webpack and 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:

Next.js vinext before vinext after
App route SSR head metadata only metadata only metadata only
App route after hydration Metadata Title, no next/head tags next/head title, duplicate viewport/description, client meta metadata only
App client navigation metadata title — metadata title
App server component <Head> 200, renders nothing 500 (useRef) 200, renders nothing
Pages route <Head> applied applied applied

Tests

New tests/e2e/app-router/next-head-ignored.spec.ts uses new app-basic fixture routes: a client layout shared by /next-head-ignored (App) and /next-head-shared (Pages), and an App server component that renders <Head>.

  • Before the fix: the 3 App Router tests failed. The Pages Router test passed.
  • After the fix: all 4 pass.

Local validation:

  • CI=1 PLAYWRIGHT_PROJECT=app-router npx playwright test tests/e2e/app-router/next-head-ignored.spec.ts --project=app-router
  • vp 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.ts
  • vp check on the changed files

Bundle note

A production client build of app-basic moves app-router-tree-context from the next/dynamic chunk into the shared chunk that holds head. 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.

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
@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head 3685a6c094ee8c98ac2817159fa1692001652e61 without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@pkg-pr-new

pkg-pr-new Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@vinext/cloudflare

npm i https://pkg.pr.new/@vinext/cloudflare@4610

create-vinext-app

npm i https://pkg.pr.new/create-vinext-app@4610

@vinext/types

npm i https://pkg.pr.new/@vinext/types@4610

vinext

npm i https://pkg.pr.new/vinext@4610

@cloudflare/workers-response-store

npm i https://pkg.pr.new/@cloudflare/workers-response-store@4610

commit: 2c147c2

@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor
Example Preview Production Original
app-router-cloudflare preview production
pages-router-cloudflare preview production
app-router-playground preview production original
realworld-api-rest preview production
nextra-docs-template preview production
benchmarks preview production
hackernews preview production original
response-store-demo preview production
workers-cache preview production
kv preview production
static-assets-cache preview production
static-assets-pages preview production
static-assets-pages-i18n preview production
static-export preview production
web preview production

@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Performance benchmarks

Compared 2c147c2 against base e7a12de using alternating same-runner rounds. Next.js was unchanged and skipped.

0 improved · 0 regressed · 7 within ±1.5%

Scenario Framework Baseline Current Change
Client bundle size (gzip) vinext 153.5 KB 153.5 KB ⚫ -0.0%
Client entry size (gzip) vinext 141.7 KB 141.7 KB ⚫ -0.0%
Dev server cold start vinext 2.17 s 2.15 s ⚫ -0.7%
Dev server cold start (large Pages Router app) vinext 17.32 s 17.52 s ⚫ +1.2%
Production build time vinext 2.72 s 2.73 s ⚫ +0.3%
RSC entry closure size (gzip) vinext 141.5 KB 141.5 KB ⚫ -0.0%
Server bundle size (gzip) vinext 250.6 KB 250.6 KB ⚫ -0.0%

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");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2c147c2: the test now sets a window marker before the click and asserts it survives the navigation.

@ask-bonk

ask-bonk Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Review: 1 finding.

Posted 1 inline test-coverage finding. Production code had no actionable defects.

github run

@james-elicx

Copy link
Copy Markdown
Member Author

/bigbonk review for issues
Please review exact head 2c147c2266b51d54663fbd7094cd1ba88159318b without modifying or pushing the branch. Report all actionable findings within your 8-minute time limit.

@ask-bonk

ask-bonk Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

LGTM!

github run

@james-elicx
james-elicx marked this pull request as ready for review November 9, 2026 00:11

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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

@james-elicx
james-elicx merged commit 6f0abf1 into main Nov 9, 2026
182 of 184 checks passed
@james-elicx
james-elicx deleted the claude/fix-4589-next-head-app-router branch November 9, 2026 10:27
@github-actions github-actions Bot mentioned this pull request Nov 9, 2026
MrIago added a commit to MrIago/vinext that referenced this pull request Nov 14, 2026
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>
MrIago added a commit to MrIago/vinext that referenced this pull request Nov 14, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

next/head is applied in App Router routes

1 participant