In the Next.js App Router, a server component that renders HTML with dangerouslySetInnerHTML sends that HTML twice: once in the page's HTML, and again, escaped as a string, in the inline React Server Components payload that hydration reads. For a long markdown article the payload copy can be bigger than the article. One of my case-study pages was 512 KB of HTML against a 520 KB budget: 181 KB of article and 280 KB of RSC payload. Sending each body only once, in the static HTML, brought that page to 262 KB without removing a word or an image.
This is Next.js 16.4 with React 19.3, statically exported. The mechanism applies to any App Router page that renders a big HTML string from a server component.
Why does the RSC payload contain my HTML?
The HTML the server sends is not all React needs. To hydrate, and to render later client-side navigations without a full load, React also needs the component tree as data: the RSC payload. In a static or server-rendered App Router page it's inlined at the bottom of the HTML as a series of self.__next_f.push([...]) script calls.
Props are part of that tree. dangerouslySetInnerHTML={{ __html: html }} is a prop whose value is your whole rendered article, so the article goes into the payload as one long JSON string, with every quote and angle bracket escaped. Escaping makes the payload copy bigger than the HTML copy.
How to see it on your own page
Weigh the parts of the built HTML instead of guessing. Sum the self.__next_f.push script bodies, then measure the article element on its own. On my page:
| Part | Size |
|---|---|
| RSC payload | 280 KB |
| Article (59 KB of text, 122 KB of markup) | 181 KB |
| Everything else | ~50 KB |
One trap: if the article itself quotes self.__next_f.push(...), as this one does, a regex over the page matches the prose as well. Count only inside <script> tags.
My first idea was trimming the image markup (long CDN URLs, alt text repeated in aria-label). That's about 20% of the article and does nothing about the copy, which grows with every section the article gains.
The fix: render an empty marker, fill it after the build
The body only has to be in the HTML. Nothing on the client reads it from the payload. So in production the component writes the HTML to disk at build time and renders an empty element with an id:
export function StaticHtml({ id, html, className }: Props) {
if (process.env.NODE_ENV !== 'production') {
return (
<div className={className} dangerouslySetInnerHTML={{ __html: html }} />
)
}
recordStaticHtml(id, html)
return (
<div
className={className}
data-static-html={id}
suppressHydrationWarning
dangerouslySetInnerHTML={{ __html: '' }}
/>
)
}
The payload now carries an empty string. recordStaticHtml writes the body to .next/static-html/<id>.html. next dev has no post-build step, so in development the component renders the HTML directly.
A script runs right after next build and writes each body into its marker in the exported HTML files:
const MARKER = /(<(div|script)\b[^>]*\bdata-static-html="([^"]+)"[^>]*>)<\/\2>/g
export function injectStaticHtml(page, lookup) {
const filled = []
const missing = []
const html = page.replace(MARKER, (whole, open, tag, id) => {
const body = lookup(id)
if (body == null) {
missing.push(id)
return whole
}
filled.push(id)
return `${open}${body}</${tag}>`
})
return { html, filled, missing }
}
It's wired into the build script (next build && node scripts/inline-static-html.mjs), and it fails the build in two cases. A marker with no recorded body would ship an empty article. Zero markers across the whole site means the component stopped recording, and the pages would quietly go back to the doubled size.
Does hydration wipe the body?
No. When React hydrates an element with dangerouslySetInnerHTML, it doesn't rewrite the element's children to match the prop, so the static body stays in place. suppressHydrationWarning silences the dev warning about the difference. I checked this on a real production build served locally, not by reasoning: after hydration the page still had all 13 h2s and 83 images, and the console showed no hydration warnings.
Client-side navigation gets the empty marker
A full page load is covered. A <Link> click is not. The App Router renders the next page from its RSC payload (the .txt file next to the HTML), and that payload now holds an empty body.
The first version handled this by reloading the page once. It worked, but a full load throws away the JS heap, the scroll restoration and the instant feel that client navigation exists for. The second version fetches the page's own static HTML and copies each body in:
export async function fillStaticHtml(): Promise<boolean> {
const markers = [...document.querySelectorAll<HTMLElement>(EMPTY)]
if (markers.length === 0) return true
const path = window.location.pathname.replace(/\/$/, '') || '/'
const html = await warmPage(path)
pages.delete(path)
const source = new DOMParser().parseFromString(html, 'text/html')
const bodies = new Map(
[...source.querySelectorAll<HTMLElement>(MARKER)].map((el) => [
el.dataset.staticHtml ?? '',
el.innerHTML,
])
)
const filled = fillMarkers(markers, (id) => bodies.get(id) ?? null)
if (filled.length) window.dispatchEvent(new Event(FILLED_EVENT))
return !hasEmptyMarker()
}
That HTML file is cached at the CDN (about 49 KB brotli for the biggest page). A small client component starts the fetch on pointerover, touchstart or focusin of an article link, so it usually lands before the click does. The reload stayed as a fallback, guarded by a sessionStorage key so a broken build can't loop. If it fires, it reports an error to analytics.
Anything that reads the article DOM has to re-run
When the body is filled after mount, anything that read the article on mount has read an empty one:
-
The section rail and the outline observers would find no headings. They re-run when the fill event fires, through a hook that bumps a counter on
static-html:filled, and that counter sits in their effect deps. - The read beacon, an "article read" event that fires when a sentinel at the end of the article scrolls into view. With an empty body the sentinel was on screen at once, so the event could fire for an article nobody read.
JSON-LD goes through the same marker
JSON-LD <script> blocks had the same problem: a server-rendered string that the client never reads, shipped in both places. They now use the same marker, keyed by a hash of the JSON. One thing to watch: the client selector had to become div[data-static-html]. With a bare [data-static-html]:empty, the never-filled JSON-LD script matched as "still empty", so the beacon would never fire and the fallback would reload.
Results
| Page | Before | After |
|---|---|---|
| Longest case study | 512 KB | 262 KB (payload 42 KB) |
| Project list | 410 KB | 398 KB |
That's 270 bodies across 87 pages, and all 3,047 JSON-LD blocks still parse.
When this is worth doing
Only when a server component renders a large string that no client code reads: markdown bodies, CMS HTML, JSON-LD. If a client component needs the data as props, it has to be in the payload. For a blog or a docs site where the body is static HTML, the payload copy is pure overhead, and it grows with every article.
If you'd rather not run a post-build step, check the payload first: in an App Router page, any big string you pass to dangerouslySetInnerHTML ships twice. For the other things that made my list page heavy (repeated SVG paths, copied JSON-LD, class strings), see Cutting a Next.js page from 854 KB to 459 KB.
Top comments (0)