InlineCssStyle re-assigns innerHTML on every render β a full stylesheet re-parse per navigation, and a dead page under Trusted Types #8250
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:
- 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. - Under a CSP with
require-trusted-types-for 'script'the page dies. The assignment is aTrustedHTMLsink; adefaultpolicy that does not mint arbitrary CSS refuses it, theTypeErroris 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 readsinnerHTML).
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#
- Production build with
inlineCss: true; serve it (the client-side placeholder path is the one that matters). - Before navigating, wrap the
innerHTMLsetter 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)
},
})
- Click an in-app
<Link>to another route and readwindow.__writes. Observed: one['STYLE', true, 93704]entry per navigation (our size) β the same bytes the element already holds. Nothing else about the element changed. - Optional, the enforced variant: serve with
Content-Security-Policy: require-trusted-types-for 'script'; trusted-types defaultand install adefaultpolicy whosecreateHTMLreturnsnullfor anything not on its allowlist. The navigation throwsTypeError: Failed to set the 'innerHTML' property on 'Element': This document requires 'TrustedHTML' assignment and the 'default' policy failed to executefrom 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-router1.170.31 /@tanstack/react-start(server.build.inlineCss: true); the code is unchanged onmain(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.
Sign in to join this conversation.
read the issue template. provide a complete minimal reproducer. don't paste a wall of text out of your LLM. be concise