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

未關閉
#85 6 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
57/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍

研究方向

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.

由索引模型根據 Issue 內容生成。

描述

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.

主要語言
TypeScript
星號
640
分支
32
平均合併
1 小時 15 分鐘
30 天內合併 PR
7

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

TanStack/redact 的其他 Issue

查看 TanStack/redact 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。