fix: emit the global stylesheet preload only in production builds

Merged
FA
2mo ago
+70-15

Description

Stylesheet edits in assets/ don't apply in dev mode until a manual page reload, since v0.9.12.

Cause: #7078 (074a818de) added <link rel="preload" as="style" href=".../__reflex_global_styles.css"> to the document head, before the <link rel="stylesheet"> for the same file. In dev, the backend recompiles and copies the edited stylesheet, and Vite sends a css-update for /styles/__reflex_global_styles.css. Vite's client handles it by swapping the first <link> whose href contains that path (vite/dist/client/client.mjs), which is the preload. The browser fetches the new CSS as a preload and discards it ("preloaded using link preload but not used"). The stylesheet link keeps the old href, so the page keeps the old styles. On 0.8.23 the head had only the stylesheet link, and edits applied in under a second.

Fix (reflex/compiler/utils.py, create_document_root): emit the preload only when is_prod_mode(). #7078 added it as a production load-time optimization, so production output is byte-identical. Dev goes back to the single stylesheet link that Vite swaps. Reordering the links instead was rejected: it would change prod output and leave a stale preload after the swap.

Tests:

  • test_document_does_not_preload_the_global_stylesheet_in_dev fails before the fix.
  • test_document_preloads_the_global_stylesheet_in_prod keeps #7078's intent.
  • The dev head shape in test_create_document_root* goes back to its pre-#7078 values.

Verified with a real dev app: the hmr.css hot-reload benchmark from the Holistic Framework Benchmarks project (#7289) edits a stylesheet in assets/ and waits for the computed style in the browser.

  • Before: the update never applies (font-size stays 12px).
  • After: it applies in 0.99 s median over 3 runs, with no full reload. That matches 0.8.23 under the same machine load.

Checks

  • uv run pytest tests/units/compiler: 303 passed.
  • Pre-commit (ruff, codespell, pyi, pyright, ty) passes.