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

Aperta
#193 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
react, typescript
Ambito
frontend, testing

Direzione di ricerca

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.

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

Descrizione

(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.

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.