Repository navigation
Keep implicit libraries off window.__reflex in apps without dynamic components - #8036
FarhanAliRaza wants to merge 1 commit into
Conversation
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, so an app that uses one Radix Themes component ships all of @radix-ui/themes and the 45 primitives behind it, although only a component held in state ever reads the namespace. compile_app now tells compile_app_root whether any compiled module imports evalReactComponent, the only reader of an optional library. 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). With it, nothing changes. A blank app's client JS goes from 784.8 KB to 530.3 KB (240.3 KB to 171.0 KB gzipped). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LwJUCUWTSf3XdFxu31STfJ
[Medium impact] The PR appears safe to merge with the documented compatibility change. SummaryThe compiler now leaves implicitly registered libraries out of the app root when compiled components do not import
Reviews (2) · Last reviewed commit: "Keep implicit libraries off window.__ref..." · Reviewed by Greptile |
There was a problem hiding this comment.
2 issues found across 6 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="news/+window-libraries-dynamic-only.performance.md">
<violation number="1" location="news/+window-libraries-dynamic-only.performance.md:1">
P2: The note incorrectly says compiler-registered libraries are exposed only when a page evaluates a state-held component. With the default eager loading, the app root exposes them whenever the compiled app includes the dynamic-component evaluator, even if no dynamic component is ever rendered; distinguish eager and lazy behavior in this sentence.</violation>
<violation number="2" location="news/+window-libraries-dynamic-only.performance.md:1">
P2: `bundle_library` does not always make a library available on `window.__reflex` at startup. With `frontend_lazy_bundled_libraries=True`, explicitly registered non-default libraries are added only after `window.__reflex_load()` runs, so qualify this compatibility guidance and tell direct readers to await the loader.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
| @@ -0,0 +1 @@ | |||
| Apps that render no dynamic component no longer import whole libraries into the app root. A blank app ships about 70 KB less gzipped JavaScript, because `@radix-ui/themes` is tree-shaken to the components its pages use. Libraries the compiler registers on its own (a plugin's frontend dependencies, Radix Themes) are now exposed on `window.__reflex` only when a page evaluates a component held in state. A script that reads one of them from `window.__reflex` directly must register it with `bundle_library("<library>")`, which always exposes it. | |||
There was a problem hiding this comment.
P2: The note incorrectly says compiler-registered libraries are exposed only when a page evaluates a state-held component. With the default eager loading, the app root exposes them whenever the compiled app includes the dynamic-component evaluator, even if no dynamic component is ever rendered; distinguish eager and lazy behavior in this sentence.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At news/+window-libraries-dynamic-only.performance.md, line 1:
<comment>The note incorrectly says compiler-registered libraries are exposed only when a page evaluates a state-held component. With the default eager loading, the app root exposes them whenever the compiled app includes the dynamic-component evaluator, even if no dynamic component is ever rendered; distinguish eager and lazy behavior in this sentence.</comment>
<file context>
@@ -0,0 +1 @@
+Apps that render no dynamic component no longer import whole libraries into the app root. A blank app ships about 70 KB less gzipped JavaScript, because `@radix-ui/themes` is tree-shaken to the components its pages use. Libraries the compiler registers on its own (a plugin's frontend dependencies, Radix Themes) are now exposed on `window.__reflex` only when a page evaluates a component held in state. A script that reads one of them from `window.__reflex` directly must register it with `bundle_library("<library>")`, which always exposes it.
</file context>
| Apps that render no dynamic component no longer import whole libraries into the app root. A blank app ships about 70 KB less gzipped JavaScript, because `@radix-ui/themes` is tree-shaken to the components its pages use. Libraries the compiler registers on its own (a plugin's frontend dependencies, Radix Themes) are now exposed on `window.__reflex` only when a page evaluates a component held in state. A script that reads one of them from `window.__reflex` directly must register it with `bundle_library("<library>")`, which always exposes it. | |
| Apps that render no dynamic component no longer import implicit libraries into the app root. A blank app ships about 70 KB less gzipped JavaScript, because `@radix-ui/themes` is tree-shaken to the components its pages use. Libraries the compiler registers on its own (a plugin's frontend dependencies, Radix Themes) are exposed for apps whose compiled code includes a dynamic-component evaluator, and lazy loading defers optional libraries until that evaluator runs. A script that reads one of them from `window.__reflex` directly must register it with `bundle_library("<library>")`, which always exposes it. |
| @@ -0,0 +1 @@ | |||
| Apps that render no dynamic component no longer import whole libraries into the app root. A blank app ships about 70 KB less gzipped JavaScript, because `@radix-ui/themes` is tree-shaken to the components its pages use. Libraries the compiler registers on its own (a plugin's frontend dependencies, Radix Themes) are now exposed on `window.__reflex` only when a page evaluates a component held in state. A script that reads one of them from `window.__reflex` directly must register it with `bundle_library("<library>")`, which always exposes it. | |||
There was a problem hiding this comment.
P2: bundle_library does not always make a library available on window.__reflex at startup. With frontend_lazy_bundled_libraries=True, explicitly registered non-default libraries are added only after window.__reflex_load() runs, so qualify this compatibility guidance and tell direct readers to await the loader.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At news/+window-libraries-dynamic-only.performance.md, line 1:
<comment>`bundle_library` does not always make a library available on `window.__reflex` at startup. With `frontend_lazy_bundled_libraries=True`, explicitly registered non-default libraries are added only after `window.__reflex_load()` runs, so qualify this compatibility guidance and tell direct readers to await the loader.</comment>
<file context>
@@ -0,0 +1 @@
+Apps that render no dynamic component no longer import whole libraries into the app root. A blank app ships about 70 KB less gzipped JavaScript, because `@radix-ui/themes` is tree-shaken to the components its pages use. Libraries the compiler registers on its own (a plugin's frontend dependencies, Radix Themes) are now exposed on `window.__reflex` only when a page evaluates a component held in state. A script that reads one of them from `window.__reflex` directly must register it with `bundle_library("<library>")`, which always exposes it.
</file context>
| Apps that render no dynamic component no longer import whole libraries into the app root. A blank app ships about 70 KB less gzipped JavaScript, because `@radix-ui/themes` is tree-shaken to the components its pages use. Libraries the compiler registers on its own (a plugin's frontend dependencies, Radix Themes) are now exposed on `window.__reflex` only when a page evaluates a component held in state. A script that reads one of them from `window.__reflex` directly must register it with `bundle_library("<library>")`, which always exposes it. | |
| Apps that render no dynamic component no longer import whole libraries into the app root. A blank app ships about 70 KB less gzipped JavaScript, because `@radix-ui/themes` is tree-shaken to the components its pages use. Libraries the compiler registers on its own (a plugin's frontend dependencies, Radix Themes) are now exposed on `window.__reflex` only when a page evaluates a component held in state. A script that reads one of them from `window.__reflex` directly must register it with `bundle_library("<library>")`; when `frontend_lazy_bundled_libraries` is enabled, it must also await `window.__reflex_load()` before reading it. |
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
Type of change
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.RadixThemesPluginregisters@radix-ui/themesas soon as one Radix Themes component is compiled, so an app that renders a heading and a button ships all of@radix-ui/themesand 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=Truedoes not help here: the lazyimport("@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
evalReactComponentcan read an optional library fromwindow.__reflex.compile_appnow passescompile_app_rootwhether any compiled module (page, memo or app wrap) has that import: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:6f5c39cac)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.__reflexdirectly, in an app that renders no component held in state, will now find it missing. The remedy isbundle_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 asperformance; say if you would rather have it underbreaking.Related
_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 [].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.test_radix_components.py, dev and prod: a Radix app with no dynamic component has exactly the five default keys onwindow.__reflex.test_dynamic_components.py(eager and lazy) still passes.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
https://claude.ai/code/session_01LwJUCUWTSf3XdFxu31STfJ