Skip to content

fix: load only the optional libraries each dynamic component uses - #7276

Merged
FarhanAliRaza merged 16 commits into
reflex-dev:mainfrom
harsh21234i:issue-7262-lazy-bundle-investigation
Nov 2, 2026
Merged

FarhanAliRaza merged 16 commits into
reflex-dev:mainfrom
harsh21234i:issue-7262-lazy-bundle-investigation

Conversation

@harsh21234i

@harsh21234i harsh21234i commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7262.

With frontend_lazy_bundled_libraries=True, the first dynamic-component evaluation loaded every registered optional library. A page that rendered only a dynamic rx.icon also downloaded Radix Themes. So the flag added JavaScript instead of removing it.

Changes

  • make_component writes the optional libraries of each dynamic component on the first line of the generated module: //__reflex_evaluate:["lib", ...]. Default libraries (React, @emotion/react, $/utils/state, $/utils/context) are always loaded, so they are not listed.
  • evalReactComponent reads that line and calls window.__reflex_load(libraries) before import(). A failed load rejects before the module is cached, so the next evaluation of the same source tries the load again.
  • window.__reflex_load loads each library separately, shares requests in progress, and clears a failed entry so it can retry. Without arguments, it still loads all optional libraries.
  • The changelog and the docs README no longer say that the flag always reduces JavaScript size or requests.
  • test_dynamic_components runs with the flag off and on. With the flag on, it checks that an optional library loads only when a component that uses it renders.

Measurements

Production build, decoded JS loaded until the dynamic components render:

Page Flag off Main, flag on This PR, flag on
One dynamic rx.icon 1 037 022 B 1 111 997 B 867 632 B
Dynamic components that use Radix 1 728 255 B 1 803 162 B 1 803 540 B

When a page needs Radix, the lazy chunks stay about 75 KB larger than the static import. This PR does not change that chunking.

Review in cubic

@harsh21234i
harsh21234i requested review from a team and Alek99 as code owners September 23, 2026 17:22
@greptile-apps

greptile-apps Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Dynamic components now request only the optional libraries they use.

  • Dynamic components load only the optional libraries they use.

Reviews (12) · Last reviewed commit: "Merge upstream main into PR #7276" · 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.

No issues found across 3 files

Re-trigger cubic

@codspeed

codspeed Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 4.06%

2 warnings

⚠️ 22 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

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

❌ 1 regressed benchmark
✅ 147 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_get_state[disk] 939.3 µs 979 µs -4.06%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing harsh21234i:issue-7262-lazy-bundle-investigation (d9ffd71) with main (58d798f)

Open in CodSpeed

@masenf

masenf commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

the finding in the issue is that with the setting on, it actually ships larger initial JS payload than with it turned off... is that incorrect? the proposed change seems to paper over the reported issue

@harsh21234i

Copy link
Copy Markdown
Contributor Author

Implemented the runtime fix for the reported regression in 6f44747. The lazy path no longer preloads every registered optional library on the first dynamic-component evaluation; generated dynamic modules request only their own bundled module paths, and the loader caches each requested module independently. Added regression coverage plus root/reflex-base performance fragments. Local validation: 322 compiler/dynamic tests passed (1 skipped), targeted tests passed, Ruff, Pyright, and pre-commit passed.

@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.

All reported issues were addressed across 6 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

@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.

All reported issues were addressed across 3 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

@FarhanAliRaza FarhanAliRaza 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.

I exercised lazy dynamic components in a real app and ran a production export. I reran the optional-registry test and compared identical-source failure and retry behavior with the baseline evaluator using a controlled async loader.

See the inline comments.

Copy link
Copy Markdown
Contributor Author

Addressed the review findings:

  • Moved selective library loading outside the cached data-URI module evaluation. Generated components now carry their required libraries in the //__reflex_evaluate metadata header, and evalReactComponent loads them before importing the module, so a transient loader failure remains retryable for the same component source.
  • Updated the docs regression test to use a distinct uncached failure fixture and retry that exact same source.
  • Scoped the hasOwn helper inside window.__reflex_load.

The docs retry test passes, and all CI workflows are green on 21cf5ffe.

@masenf masenf added documentation Improvements or additions to documentation perf Performance-improving changes labels Oct 6, 2026
@FarhanAliRaza FarhanAliRaza added the on deck PRs lined up to review / merge next label Oct 30, 2026
Parse the //__reflex_evaluate library list with startsWith on the first
line instead of a multiline regex over the whole component source.

Drop the unconditional "fewer initial library requests" claim from the
changelog and README: with the flag on, a page that renders dynamic
components can make more requests, not fewer. Describe the per-component
loading in the docs README.
@FarhanAliRaza FarhanAliRaza changed the title docs: clarify lazy library loading tradeoffs fix: load only the optional libraries each dynamic component uses Nov 2, 2026
List only optional libraries in the //__reflex_evaluate line: the default
libraries are always in window.__reflex. Always write the list, so the
marker has one form and make_component loses the extra branch.

Use Object.hasOwn in the app-root loader instead of a local helper.

Replace string assertions on loader source with behavior checks:
- the codegen test asserts the exact library list, including an empty one
- the docs node test evaluates a module generated by make_component, so a
  marker format change on either side fails it, and it loads a library
  named constructor
- test_dynamic_components runs with the flag off and on; with it on, the
  banana icon must stay out of window.__reflex until its component renders
Resolve conflicts with reflex-dev#7604 and reflex-dev#7441:
- test_dynamic_components keeps reflex-dev#7604's deep Lucide checks; the banana
  icon is expected at first render only when libraries load eagerly
- the docs node test passes reflex-dev#7441's context-registry library to setup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation on deck PRs lined up to review / merge next perf Performance-improving changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Confirm: frontend_lazy_bundled_libraries=True added ~65 KB of initial JS on every page of the QA app (possible regression against #7078's claim)

3 participants