Repository navigation
fix(pages): prevent duplicate viewport meta tags in next/head - #4595
Open
itsrayeedscodespace wants to merge 1 commit into
Open
itsrayeedscodespace wants to merge 1 commit into
itsrayeedscodespace wants to merge 1 commit into
Conversation
reduceHeadChildren flattened defaultHead() through Children.toArray along with the user's <Head> children, turning the default viewport's key into ".$viewport". The dedupe filter then treated it as keyed, skipped the meta[name] check, and rendered it next to a user <meta name="viewport">. Like Next.js's reduceComponents(), only the user children are flattened now and defaultHead() is concatenated afterwards with its raw keys, so a user viewport replaces the default on the server and on the client. Fixes cloudflare#4593 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4593
Summary
What was broken: In a Pages Router app,
<Head><meta name="viewport" content="width=device-width, initial-scale=1" /></Head>rendered two viewport tags: vinext's default (width=device-width) and the app's. Next.js renders only the app's.Root cause:
reduceHeadChildren(packages/vinext/src/shims/head.ts) got[...defaultHead(), ...userChildren]and passed each one throughChildren.toArray. That changed the default viewport's key fromviewportto.$viewport. The dedupe filter treats any key with a$as user-keyed, and for keyedmeta[name]tags it skips the name-category check. So the default survived next to an unkeyed user viewport. The same thing happened on the client in_syncClientHead.Why the fix matches Next.js: Next's
reduceComponents()flattens only the user elements (inside-effect.tsx, viaChildren.toArray), then runs.reverse().concat(defaultHead().reverse()).filter(unique()).reverse(). The defaults keep their raw keys, sounique()never registers them as keyed and dedupes them byname/charSet.Changes
reduceHeadChildren(headChildren, defaults = [])takes the defaults as a separate argument. They are placed ahead of the user tags but are not passed throughChildren.toArray. The order ([defaults, ...user]→ reverse → filter → reverse) is the same as Next's.getSSRHeadHTML()and_syncClientHead()passdefaultHead()through that argument. Nothing else changes: user children (including_document.getInitialPropshead) are still flattened per entry exactly as before.Tests
tests/head.test.ts, newdescribe("Head user viewport replaces the default (issue #4593)"):head-priorityfixture (user charset/viewport/title, exact order)head-duplicate-default-keysfixture (several<Head>s, keyed charset/viewport)<Head>instanceskey="viewport"on an unrelated tag no longer removes the default viewport (Next.js behavior: the default's raw key is never added to the key set)_document.getInitialPropsreplaces the defaultIntegration:
tests/fixtures/pages-basic/pages/head-custom-viewport.tsx(new small fixture page)tests/pages-router.test.ts: SSR HTML has exactly one viewport, the user's (ported from Next'srendering-head.test.ts"header helper dedupes tags")tests/e2e/pages-router/head-and-dynamic.spec.ts: in a real browser, one viewport after hydration, the default after client navigation to/about, and the custom one again after back navigationNext.js references:
test/development/pages-dir/client-navigation/rendering-head.test.tsand its fixtureshead-priority.jsandhead-duplicate-default-keys.js.Validation
Red/green (run locally):
vp test run tests/head.test.ts→ 7 failed / 60 passed. Example:Received: "<meta charset…/><meta name="viewport" content="width=device-width" …/><meta name="viewport" content="width=device-width, initial-scale=1" …/>"pages-router.test.tscase →expected [ Array(2) ] to have a length of 1 but got 2toHaveCount: Expected: 1, Received: 2Commands with the fix applied:
vp test run tests/head.test.ts tests/shims.test.ts tests/document.test.ts→ 1563 passedvp test run tests/head.test.ts tests/shims.test.ts tests/document.test.ts tests/pages-router.test.ts tests/pages-router-concurrency.test.ts tests/pages-preview.test.ts tests/pages-page-response.test.ts tests/pages-page-handler.test.ts tests/unified-request-context.test.ts tests/client-page-root.test.ts→ 2278 passed, 1 failed. The failure ispages-router.test.ts > skips data cache reads while res.revalidate() regenerates a page in dev. It fails the same way on unmodifiedmainin my environment, passes when run alone, and is unrelated to head handling.PLAYWRIGHT_PROJECT=pages-router playwright test tests/e2e/pages-router/head-and-dynamic.spec.ts --project pages-router→ 10 passedvp fmt --checkclean;node scripts/sync-next-types.mjs --checkandnode scripts/check-shim-types.mjspassvp check: no findings in changed files. Locally it reports type errors only inapps/webandpackages/cloudflare(missing generated Workers types such asEnvandcloudflare:workers), because of my local install.git diff --checkcleanEnvironment limitations: Node 26 locally, where
better-sqlite3(used only by theecosystem/better-authfixture) doesn't compile. So I installed withpnpm install --frozen-lockfile --ignore-scripts. I relied on CI for the full Vitest suite and the other Playwright projects.Compatibility
charSetdedupe doesn't depend on keys, so a user charset still replaces the default, and the default is still emitted otherwise.<Head>instance withChildren.toArray, so keyed dedupe across instances is unchanged (existing tests pass)._syncClientHeadstill seedsdefaultHead(), so hydration keeps the server's charset/viewport (existing tests pass).key="charset"/key="viewport"on an unrelated tag (e.g.<meta name="description" key="viewport">) no longer drops the default.<meta name="viewport" key="viewport">still replaces it.🤖 Generated with Claude Code