Skip to content

Pages Router next/head: charset/viewport ordering and _document.getInitialProps head-tag merge order not matched #1569

Description

@james-elicx

Follow-up to #1475 / #1559. The attribute rename (data-vinext-headdata-next-head) was split into #1559 as a small isolated patch. Two other parts of #1475 remain and need their own work:

Problem (remaining parts)

1. charset/viewport ordering

Pages Router <Head> emission does not match Next.js's canonical order for charset and viewport tags. The Next.js e2e suite (test/e2e/next-head/index.test.ts) asserts that charset appears first, followed by viewport, then user tags — in that specific order. vinext currently emits them out of order.

2. _document.getInitialProps() head-tag merge

When a custom _document defines getInitialProps() and returns head tags via Head.rewind() / head: [...], those tags must be merged into the SSR HTML before the user's next/head tags. vinext currently does not call _document.getInitialProps() for head purposes at all, so downstream tag lookups in _document fail.

Reference

  • .nextjs-ref/test/e2e/next-head/index.test.ts — assertions on ordering and on _document.getInitialProps() merge
  • .nextjs-ref/packages/next/src/pages/_document.tsx — canonical merge logic

Estimated impact

~2 e2e test failures remaining after #1559.

Recommendation

  1. Reproduce in vinext's own test suite with a Pages Router fixture that imports next/head plus a custom _document with getInitialProps(). Assert (a) charset/viewport ordering, (b) that _document.getInitialProps() head tags appear ahead of user next/head tags.
  2. Update packages/vinext/src/shims/head.ts (or wherever head tags are aggregated for SSR) to:
    • emit charset and viewport in Next.js's canonical position
    • call the user's _document.getInitialProps() (when defined) and merge its head output ahead of user next/head tags
  3. Mirror .nextjs-ref/packages/next/src/pages/_document.tsx for the exact merge semantics.

Part of #1328.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions