Keep implicit libraries off window.__reflex in apps without dynamic components
Type of change
- Performance change, with one compatibility note (see Compatibility)
Description
Problem. The app root imports every bundled library as a namespace and puts it on window.__reflex, where dynamic components look it up. A namespace import keeps every export. RadixThemesPlugin registers @radix-ui/themes as soon as one Radix Themes component is compiled, so an app that renders a heading and a button ships all of @radix-ui/themes and the 48 Radix primitive packages behind it. In a blank app that is 31% of the raw client JS, and the page uses about 27 KB raw of the 243 KB shipped.
frontend_lazy_bundled_libraries=True does not help here: the lazy import("@radix-ui/themes") resolves to the same module the pages import statically, so the bundler keeps the whole package in the eagerly loaded chunk (measured: 240.2 KB gzipped with the flag, 240.3 KB without).
Change. Only a module that imports evalReactComponent can read an optional library from window.__reflex. compile_app now passes compile_app_root whether any compiled module (page, memo or app wrap) has that import:
- With it, nothing changes.
- Without it, the root exposes the default libraries and the ones the app registered with
bundle_library, and leaves out the ones the compiler registered on its own (Radix Themes, plugin frontend dependencies). This applies to the eager imports and to the lazy loaders.
Measured on reflex init --template blank, reflex export --frontend-only, all client JS:
main (6f5c39cac) | this PR | |
|---|---|---|
| JS raw | 784.8 KB | 530.3 KB |
| JS gzip | 240.3 KB | 171.0 KB |
| Lighthouse mobile, simulated throttling | 90, LCP 2.87 s | 93, LCP 2.56 s |
| Lighthouse mobile, DevTools throttling | 88, LCP 2.99 s | 92, LCP 2.67 s |
Lighthouse 12, median of five runs at /, served gzipped from a local static server with no backend running.
Compatibility
A script that reads a compiler-registered library from window.__reflex directly, in an app that renders no component held in state, will now find it missing. The remedy is bundle_library("<library>"), which always exposes the library. The default libraries (react, @emotion/react, $/utils/context, $/utils/context-registry, $/utils/state) and explicit registrations are untouched. I filed the news fragment as performance; say if you would rather have it under breaking.
Related
- #8035 does the same for apps without state and edits the same two functions (
_compile_app,compile_app_root), so the second to merge needs a small rebase. The two compose:_get_window_libraries(implicit=dynamic_components) if stateful else []. - #8033 moves
evalReactComponentto$/utils/transport. After it, the import this PR looks for comes fromDirs.TRANSPORT_PATH; the check is one line.
Tests
test_compiler.py:_get_window_libraries(implicit=False)returns exactly the default and app-registered libraries.compile_appon an app withRadixThemesPluginand an explicitbundle_libraryemits exactly the expected namespace imports and lazy loaders, with and without a component held in state, with and withoutfrontend_lazy_bundled_libraries. The no-dynamic-component cases fail on main.- Playwright
test_radix_components.py, dev and prod: a Radix app with no dynamic component has exactly the five default keys onwindow.__reflex. - Playwright
test_dynamic_components.py(eager and lazy) still passes. - Run locally: the two Playwright files above (6 passed),
ruff check,ruff format, andpyrighton the changed files. Full unit suite: 16,580 passed. The 264 that did not pass are all intests/units/reflex_cli/v2(my checkout has no tags, so the hosting CLI rejects its dev version) and thereflex_benchbrowser driver tests (no matching Chromium build locally); none are in the compiler.
š¤ Generated with Claude Code