fix: load only the optional libraries each dynamic component uses
Merged
HA
1mo ago+132+132+012345678901234567890123456789-40-40-01234567890123456789
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_componentwrites 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.evalReactComponentreads that line and callswindow.__reflex_load(libraries)beforeimport(). A failed load rejects before the module is cached, so the next evaluation of the same source tries the load again.window.__reflex_loadloads 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_componentsruns 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.