Skip to content
Source
Sign inSign up
TanStack/router

InlineCssStyle re-assigns innerHTML on every render β€” a full stylesheet re-parse per navigation, and a dead page under Trusted Types #8250

Closedhierotsu(GitHub profile, opens in a new tab) opened this issue 1 comment

Which project does this relate to?

React Router (@tanstack/react-router β€” HeadContent/Asset), reached through Start's server.build.inlineCss.

Describe the bug

HeadContent β†’ Asset β†’ InlineCssStyle renders the inlined stylesheet with dangerouslySetInnerHTML={{ __html: html }} β€” a new object literal on every render (packages/react-router/src/Asset.tsx, line 127 on main as of 2026-09-05).

HeadContent re-renders on every client-side navigation (title/meta change), and React 19's update path re-assigns innerHTML whenever the dangerouslySetInnerHTML prop is a new reference: updateProperties compares nextProp !== lastProp and setProp then does domElement.innerHTML = value.__html without looking at the string (react-dom 19.2.8, cjs/react-dom-client.production.js; React 18 used to compare the __html strings). Two consequences:

  1. Every client navigation re-writes the whole inlined stylesheet into the same <style>, string-equal (93 KB in our app) β€” a full CSS re-parse and style recalculation per navigation, for an element whose content never changes after hydration.
  2. Under a CSP with require-trusted-types-for 'script' the page dies. The assignment is a TrustedHTML sink; a default policy that does not mint arbitrary CSS refuses it, the TypeError is thrown inside React's commit phase, and the router's error component replaces the page β€” every client-side navigation ends on the error screen, while full loads work (hydration only reads innerHTML).

The same shape exists in the other branches of Asset β€” case 'style' (line 83) and the data-script branches of Script (lines 245, 263, 280): each hands React a fresh { __html } per render.

Complete minimal reproducer

No link yet β€” the count below reproduces in any Start app built with server.build.inlineCss: true and two routes with different head() titles. Happy to add a StackBlitz if that helps.

Steps to Reproduce the Bug

  1. Production build with inlineCss: true; serve it (the client-side placeholder path is the one that matters).
  2. Before navigating, wrap the innerHTML setter in the page:
const d = Object.getOwnPropertyDescriptor(Element.prototype, 'innerHTML')
window.__writes = []
Object.defineProperty(Element.prototype, 'innerHTML', {
  ...d,
  set(v) {
    window.__writes.push([this.tagName, this.hasAttribute('data-tsr-inline-css'), String(v).length])
    d.set.call(this, v)
  },
})
  1. Click an in-app <Link> to another route and read window.__writes. Observed: one ['STYLE', true, 93704] entry per navigation (our size) β€” the same bytes the element already holds. Nothing else about the element changed.
  2. Optional, the enforced variant: serve with Content-Security-Policy: require-trusted-types-for 'script'; trusted-types default and install a default policy whose createHTML returns null for anything not on its allowlist. The navigation throws TypeError: Failed to set the 'innerHTML' property on 'Element': This document requires 'TrustedHTML' assignment and the 'default' policy failed to execute from React's commit, and the route renders its error component.

Expected behavior

No write when the string did not change. Memoising the prop object fixes both symptoms β€” measured in our production build through a Vite transform of dist/esm/Asset.js:

const inline = React.useMemo(() => ({ __html: html }), [html])
// …
<style
  {...attrs}
  {...{ [INLINE_CSS_HYDRATION_ATTR]: '' }}
  dangerouslySetInnerHTML={inline}
  nonce={nonce}
  suppressHydrationWarning
/>

React then sees lastProp === nextProp for that key and never touches the sink. (Wrapping InlineCssStyle in React.memo would not be enough on its own: attrs is a fresh object each render.) The same one-line memo applies to the other { __html } literals in Asset.tsx.

Screenshots or Videos

No response

Platform

  • @tanstack/react-router 1.170.31 / @tanstack/react-start (server.build.inlineCss: true); the code is unchanged on main (1.170.32)
  • react / react-dom 19.2.8
  • Chromium (Playwright's bundled build) on Windows 11; Trusted Types enforced via CSP

Additional context

Found on a production build behind an enforced Trusted Types policy: 7 refused innerHTML writes per client navigation, three of them our own dangerouslySetInnerHTML head elements (fixed on our side the same way, one object per string) and this one from InlineCssStyle. Until this lands we ship the build-time transform above plus a test that reads the installed Asset.js and goes red on a bump that moves the line β€” or fixes it. Glad to open a PR if you'd take one.

  1. read the issue template. provide a complete minimal reproducer. don't paste a wall of text out of your LLM. be concise

Sign in to join this conversation.