[Fizz] Fix duplicate preload links in SSR#36249
[Fizz] Fix duplicate preload links in SSR#36249Shramkoweb wants to merge 1 commit intofacebook:mainfrom
Conversation
… rendered resource (facebook#35889) When Flight encounters a `<link rel="preload">` JSX element during server rendering, it emits a preload hint while still serializing the element in the payload. During SSR, Fizz processes both the hint (via `preload()`) and the element (via `pushLink()`), resulting in duplicate `<link>` tags in the HTML output. Add deduplication in `pushLink()` for `rel="preload"` links by checking and registering in `resumableState` before emitting, matching the same resource tracking used by the imperative `preload()` API.
|
Hi @Shramkoweb! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Summary
Fixes #35889
<link rel="preload" as="font" />(and other preload types) rendered inside an RSC component appears twice in SSR HTML when used with Fizz SSR. Regression since 19.2.0, introduced in #34604.Root cause:
When the RSC Flight server processes a
<link rel="preload">element, it does two things:'L'row) viaprocessLink()inReactFlightServerConfigDOM.js<link>element in the serialized component outputDuring Fizz SSR, the Flight hint is received and dispatched to the Fizz
preload()function, which registers the resource inresumableStateand adds it torenderState.fontPreloads. Later, the same<link>element from the component output is processed bypushLink(), which fell through topushLinkImpl(renderState.hoistableChunks, props)with no deduplication check — producing a second identical link tag.Fix:
In
pushLink(), before emitting a<link rel="preload">tohoistableChunks, check whether the resource is already registered inresumableState(using the same per-as-type tracking thatpreload()uses:imageResources,styleResources,scriptResources, andunknownResourcesfor fonts and others). If already registered, returnnull. If not yet registered, register it so futurepreload()calls will also deduplicate correctly.How did you test this change?
Added regression test
does not emit duplicate preload links in SSR when Flight hints preload a resource that is also rendered as JSXinReactFlightDOM-test.jsthat renders a<link rel="preload" as="font">inside an RSC component through Flight → Fizz SSR and asserts the link appears exactly once in the HTML output.