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

Đang mở
#85 6 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
57/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Lĩnh vực
frontend, testing-qa

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
TypeScript
Star
640
Fork
32
Merge trung bình
1 giờ 15 phút
Pull request đã merge (30 ngày)
7

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của TanStack/redact

Tất cả issue của TanStack/redact

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.