JSON-LD script ends up twice in the page after a hydration error

未关闭
#193 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
react, typescript
领域
frontend, testing

调研方向

Start with clearHostContainer and compare its script handling with React's clearContainerSparingly. Reproduce the hydration mismatch using the issue's example, then inspect tests/hydration-recovery-structured-data.test.tsx. Done when recovery leaves one application/ld+json script while preserving other scripts.

由索引模型根据 Issue 内容生成。

描述

(react issue - redact also concerned. I'm not sure sure if react realted issues should be published here, if not feel free to close it )

Google Search Console started flagging pages of our site with "Review has multiple aggregate ratings". The HTML we send has a single JSON-LD block. The second one only appears once redact runs in the browser, which is the version Google indexes.

Our language switcher shows locale names with Intl.DisplayNames. The server runs on Bun (but same on Node), whose ICU data doesn't format them like Chrome does: "Русский" on the server, "русский" in the browser. That's enough for hydration to fail. The switcher lives in the root shell, outside any Suspense boundary, so redact recovers by client-rendering the whole document.

To do that, clearHostContainer empties <head> and <body> but keeps every <script>. The client render then inserts the JSON-LD script again, and the page ends up with two identical application/ld+json blocks. Google merges them (same @id) and sees two aggregateRatings.

Note that React does exactly the same here, since clearHostContainer mirrors React's clearContainerSparingly. I've reported it there with a fix, so this issue is about keeping redact in line with it:

Repro
const structuredData = '{"@type":"SoftwareApplication","@id":"https://example.com/#app","aggregateRating":{"ratingValue":"4.9","ratingCount":64}}'

const App = ({ label }: { label: string }) => (
  <html>
    <head>
      <title>app</title>
      <script type="application/ld+json" dangerouslySetInnerHTML={{ __html: structuredData }} />
    </head>
    <body>
      <a href="/ru">{label}</a>
    </body>
  </html>
)

// server HTML: same tree with label "Русский"
hydrateRoot(document, <App label="русский" />)

document.querySelectorAll('script[type="application/ld+json"]').length
// redact: 2 (React 19.3.1: 2 as well, until the PR above lands)
// expected: 1
Cause

Scripts are kept during the cleanup so a script that's still being streamed (opened but not closed yet) isn't lost before it runs. JSON-LD never runs, and the client render puts it back right after, so keeping it only creates a duplicate.

Fix: https://github.com/aymericzip/redact/commit/c6624f9

It removes application/ld+json scripts in clearHostContainer and keeps every other script, <style> and stylesheet <link> as before. It comes with a test in tests/hydration-recovery-structured-data.test.tsx, which fails on main and passes with the fix.

主要语言
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 摘要。