fix: load only the optional libraries each dynamic component uses

Merged
HA
1mo ago
+132-40

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:

PageFlag offMain, flag onThis PR, flag on
One dynamic rx.icon1 037 022 B1 111 997 B867 632 B
Dynamic components that use Radix1 728 255 B1 803 162 B1 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.