Fix duplicate useServerInsertedHTML output in prerendered HTML with cacheComponents (#101147)
Adopts #99906 by @dgattey. This is a new PR so that we can rebase while
signing the commits. Thank you for the fix!
Closes #99906. Fixes #99905
Original description:
---
### What?
With `cacheComponents` on, `useServerInsertedHTML` output appears twice
in prerendered HTML. CSS-in-JS registries, like MUI's
AppRouterCacheProvider (Emotion), ship their whole stylesheet twice in .
### Why?
prerenderToStream passes one ServerInsertedHTMLProvider to both the
prospective client prerender and the final client prerender. The
prospective render is thrown away, but the callbacks it registered stay
on the shared provider and run again when the final HTML is built.
### How?
The prospective client prerender now gets its own provider, the same way
warmupClientModulesForStagedValidation and the validation renders
already do. The final render keeps the provider that
renderServerInsertedHTML reads from.
I added a static page to
test/production/app-dir/ppr-use-server-inserted-html and a check that
its inserted tag appears once. The check fails on canary (2 tags) and
passes with the fix, on Turbopack and webpack. The new page logs its
insertion at build time, so the existing "no insertion logged at build"
check now looks only for the dynamic page's log.
---------
Co-authored-by: Dylan Gattey <982182+dgattey@users.noreply.github.com>