Head hydration matches <meta> by name only: an inserted theme-color meta breaks Next.js viewport.themeColor hydration on every page

Aperta
#85 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
57/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva

Direzione di ricerca

Start with dist/dom/features/hydration/full.js, especially HEAD_KEY_ATTRS.meta and HydrationCursor.head(); the report also points to related issue #17. Reproduce with the supplied Next.js app and compare REDACT=1 against REDACT=0 in production, checking recoverable errors and the three theme color tags after hydration. Done means inserted head nodes are skipped, the reported errors disappear, and server rendered attributes remain unchanged.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Version: @tanstack/redact 0.2.1 through withRedact, Next.js 16.4.3 (latest stable; App Router, Turbopack), react/react-dom 19.3.1, Chromium, production build (next build && next start).

What happens

Next.js renders a viewport.themeColor with light and dark entries as two <meta name="theme-color"> tags that differ only in media. A common no-flash theme pattern adds an inline <head> script that runs before hydration and inserts one more <meta name="theme-color"> (the active colour, no media) in front of them.

HEAD_KEY_ATTRS.meta in dist/dom/features/hydration/full.js is ["name", "property", "charSet", "httpEquiv"]. Because media isn't part of the key, HydrationCursor.head() claims the inserted tag for the first server-rendered meta, and the next theme-color meta is matched one tag too early. This happens on every page load:

  • 6 recoverable errors. In production each one reads Hydration mismatch.. In development they read Hydration mismatch on <meta> for "media". (×4) and Hydration mismatch on <meta> for "content". (×2): the same three mismatches, reported twice. Next.js passes them to reportError, so every page view fires 6 window error events.
  • The tags are rewritten. The inserted meta gets media="(prefers-color-scheme: light)", and the light meta becomes a second dark one.

Native React hydrates the same HTML with no errors and leaves the tags unchanged.

meta[name="theme-color"] after hydration React 19.3.1 Redact 0.2.1
1st (inserted by the script) no media, #ffffff (prefers-color-scheme: light), #ffffff
2nd (prefers-color-scheme: light), #ffffff (prefers-color-scheme: dark), #000000
3rd (prefers-color-scheme: dark), #000000 (prefers-color-scheme: dark), #000000
Errors per page load 0 6

The app stays interactive: a client counter on the page still works.

Reproduction

package.json dependencies: [email protected], [email protected], [email protected], @tanstack/[email protected].

next.config.mjs:

import { withRedact } from '@tanstack/redact/next'

const config = {}

export default process.env.REDACT === '1' ? withRedact(config) : config

app/layout.js:

const themeScript = `(function(){var m=document.createElement('meta');m.setAttribute('name','theme-color');m.setAttribute('data-active','');m.setAttribute('content','#ffffff');var first=document.querySelector('meta[name="theme-color"]');document.head.insertBefore(m,first||document.head.firstChild)})()`

export const viewport = {
  themeColor: [
    { media: '(prefers-color-scheme: light)', color: '#ffffff' },
    { media: '(prefers-color-scheme: dark)', color: '#000000' },
  ],
}

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <head>
        <script dangerouslySetInnerHTML={{ __html: themeScript }} />
      </head>
      <body>{children}</body>
    </html>
  )
}

app/page.js:

import Counter from './counter'

export default function Page() {
  return (
    <main>
      <h1>Redact repro</h1>
      <Counter />
    </main>
  )
}

app/counter.js:

'use client'
import { useState } from 'react'

export default function Counter() {
  const [count, setCount] = useState(0)
  return <button id="counter" onClick={() => setCount(count + 1)}>count {count}</button>
}

Run REDACT=1 next build && next start, open /, and watch the console or Playwright pageerror. Then run the same steps with REDACT=0 to compare. REDACT=1 next dev shows the detailed messages.

Expected

The same result as React: head nodes the server didn't render are skipped, nothing is reported, and no attributes are rewritten.

Suggested fix

Add media to HEAD_KEY_ATTRS.meta. When a head candidate fails attribute validation, keep scanning instead of claiming it, because scripts and browser extensions often insert nodes into <head>. #17 (head claiming, foreign nodes) looks related.

Why it matters for us

We evaluated 0.2.1 on a production Next.js 16.4.3 app. The bundle saving matched your numbers: every page loads 34.6 KB less JS (zstd). 0.2.1 also fixed everything we hit on 0.2.0 (react-modal dialogs, focus after sending a form), so thank you for that! But our e2e suite (87 tests) still fails on every page, and these errors are the only cause, so we can't ship Redact to production yet. Our other blocker is #81: QR codes from qrcode.react and SVG chart labels render wrong after client rendering. I've added the details in a comment there.

Lingua principale
TypeScript
Stelle
640
Fork
32
Merge medio
1h 15m
PR unite (30g)
7

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di TanStack/redact

Tutte le issue di TanStack/redact

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.