Skip to content

Keep implicit libraries off window.__reflex in apps without dynamic components - #8036

Open
FarhanAliRaza wants to merge 1 commit into
mainfrom
claude/reflex-ui-library-migration-1nl2b8
Open

FarhanAliRaza wants to merge 1 commit into
mainfrom
claude/reflex-ui-library-migration-1nl2b8

Conversation

@FarhanAliRaza

@FarhanAliRaza FarhanAliRaza commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

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

Tests

  • test_compiler.py: _get_window_libraries(implicit=False) returns exactly the default and app-registered libraries. compile_app on an app with RadixThemesPlugin and an explicit bundle_library emits exactly the expected namespace imports and lazy loaders, with and without a component held in state, with and without frontend_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 on window.__reflex.
  • Playwright test_dynamic_components.py (eager and lazy) still passes.
  • Run locally: the two Playwright files above (6 passed), ruff check, ruff format, and pyright on the changed files. Full unit suite: 16,580 passed. The 264 that did not pass are all in tests/units/reflex_cli/v2 (my checkout has no tags, so the hosting CLI rejects its dev version) and the reflex_bench browser driver tests (no matching Chromium build locally); none are in the compiler.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LwJUCUWTSf3XdFxu31STfJ

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
@FarhanAliRaza
FarhanAliRaza requested a review from a team as a code owner November 27, 2026 03:50
@FarhanAliRaza FarhanAliRaza added breaking Public API facing change considered for minor release bump pre-v1.0 documentation Improvements or additions to documentation perf Performance-improving changes labels Nov 27, 2026 — with ChatGPT Codex Connector
@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge with the documented compatibility change.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge with the documented compatibility change.

Summary

The compiler now leaves implicitly registered libraries out of the app root when compiled components do not import evalReactComponent.

  • Apps without dynamic components leave compiler-added libraries out of the app root.

Reviews (2) · Last reviewed commit: "Keep implicit libraries off window.__ref..." · Reviewed by Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Suggested change
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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Suggested change
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.

@codspeed

codspeed Bot commented Nov 27, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 21 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 146 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing claude/reflex-ui-library-migration-1nl2b8 (bf24722) with main (6f5c39c)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Public API facing change considered for minor release bump pre-v1.0 documentation Improvements or additions to documentation perf Performance-improving changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants