router icon indicating copy to clipboard operation
router copied to clipboard

Assets: async script triggers hydration mismatch

Open hiendaovinh opened this issue 10 months ago • 2 comments

Which project does this relate to?

Router

Describe the bug

I believe there is a regression with this https://github.com/TanStack/router/pull/5980/files or maybe the one before that https://github.com/TanStack/router/pull/5895/files#diff-5319ec26839a90fd840b11ecc7f23fa4b8ff5c311338056cd032204f8448ee45R146-R154. By specifying "async: true", it will trigger the hydration warning.

Image

Your Example Website or App

https://stackblitz.com/edit/github-52nzeepx

Steps to Reproduce the Bug or Issue

I tried to reproduce with vite/react (no tanstack router) and simple renderToPipeableStream & hydrateRoot. It gives the same result

function AppFailedToSuppressHydrationWarning() {
  if (typeof window === 'undefined') {
    return <script src="https://accounts.google.com/gsi/client" async></script>
  }

  return <script dangerouslySetInnerHTML={{ __html: "" }} suppressHydrationWarning={true} async></script>
}

function AppSucceededToSuppressHydrationWarning() {
  if (typeof window === 'undefined') {
    return <script src="https://accounts.google.com/gsi/client"></script>
  }

  return <script dangerouslySetInnerHTML={{ __html: "" }} suppressHydrationWarning={true}></script>
}

function AppHoisted() {
  return <div>
    This should belong to the body

    {/* this will go to the head with renderToPipeableStream */}
    <script src="https://accounts.google.com/gsi/client" async={true}></script>
  </div>
}

export default AppFailedToSuppressHydrationWarning

The suppressHydrationWarning doesn't seem to work with async script.

Expected behavior

No hydration mismatch warning

Screenshots or Videos

No response

Platform

  • Router / Start Version: 1.141.1
  • OS: Ubuntu 24.04.3 LTS
  • Browser: Chrome
  • Browser Version: Version 143.0.7499.192 (Official Build) (64-bit)
  • Bundler: vite
  • Bundler Version: 7.2.7

Additional context

Already reported here: https://discord.com/channels/719702312431386674/1457054816831541289

hiendaovinh avatar Jan 21 '26 17:01 hiendaovinh

can confirm with version 1.145.2

Dubzer avatar Jan 23 '26 01:01 Dubzer

I've opened a PR (#6479) to fix this. 😸

Thanks for reporting this! I took a look into it and noticed that React's hydration mismatch with async scripts persists even with suppressHydrationWarning. PR attempts to fix this by stripping the async/defer attributes on the client-side render only. This should prevent the mismatch while keeping the server output correct. Hopefully, this helps!

kyu-rong avatar Jan 24 '26 04:01 kyu-rong