Fix css_module stylesheets missing from SSR head after the first request - #5707
Fix css_module stylesheets missing from SSR head after the first request#5707nicoburns wants to merge 1 commit into
Conversation
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
Tested this end-to-end with a minimal fullstack repro of DioxusLabs/dioxus-components#287 (one SSR regression (the reported bug): 3/3 consecutive requests to a freshly booted server each contained exactly one Browser (dx serve, fullstack + wasm hydration): page renders styled on first load and stays styled across reloads (2nd+ SSR requests = the bug scenario), with exactly one stylesheet link after hydration (verified |
3dc7e1b to
ee9a5b8
Compare
|
Related #5644 |
Summary
Fixes DioxusLabs/dioxus-components#287 ("component rendering takes a while after reloading but is instantaneous after building").
The
#[css_module]-generated__CssIdent::derefinjected its stylesheet viadocument().create_link(...)guarded by a process-globalstatic OnceLock. On a fullstack server theServerDocumentcollecting head elements is per-request, so only the first request after the server boots got the<link rel="stylesheet">in the SSR<head>. Every subsequent request (i.e. any page reload) was served with no stylesheet link at all, and the page rendered as unstyled text until WASM hydration re-inserted the link client-side.Reproduced with a minimal fullstack app: the first
curlof/contained the stylesheet link in the head; the second did not.The fix deduplicates per document (per
VirtualDom) instead of per process:dioxus_document::insert_stylesheet(href)helper that reuses the same root-scopeLinkContextdedup (keyed"{href}|stylesheet") as thedocument::Linkcomponent, then gates oncreate_head_component()so hydration doesn't insert a duplicate link when the server already rendered it:css_modulemacro's generatedDerefnow just callsdioxus::document::insert_stylesheet(&ASSET.to_string())instead of theOnceLockblock.With this, every SSR request emits the component stylesheet in the head, and hydration skips re-inserting it (previously the client always appended a duplicate link).
Verified:
curling a fullstack repro repeatedly now returns the<link rel="stylesheet">in the head on every request;cargo clippy --workspace --all-features --all-targets -D warningsandcargo fmt --checkpass.Link to Devin session: https://dioxus.staging.devinenterprise.com/sessions/05fa859d90474349a5e9f0880dddbda0
Requested by: @nicoburns
Devin Review