Skip to content

Direct eval in utils/state.js stops minification of the shared chunk: ~72 KB extra JS with frontend_lazy_bundled_libraries #7860

Closed

Description

@masenf

Problem

.web/utils/state.js (template: packages/reflex-base/src/reflex_base/.templates/web/utils/state.js) calls direct eval in four places:

  • L469 await eval(\import(dataUri)`)`
  • L544 eval(event.payload.callback)
  • L762–763 eval(event.payload.javascript_code) / eval(event.payload.function)()

A direct eval can read and write every variable in scope, so the minifier must keep all top-level names in the chunk that contains it unmangled. With frontend_lazy_bundled_libraries=True, @radix-ui/themes and its primitives are reached both statically and through the lazy import("@radix-ui/themes") loader, so the bundler moves them into the same shared chunk as state.js (preload-helper-*.js), where none of their names can be shortened.

Measured in prod (reflex run --env prod, single port) on a test app with bundle_library(...) for lucide icons and a recharts chart, reflex 0.11.0a2, decoded bytes:

page flag off flag on delta
/ (no recharts) 2,134,012 B 1,608,413 B −525.6 KB (the #7276 win)
/recharts (uses recharts directly) 1,392,087 B 1,463,610 B +71.5 KB (+5.3 KB gzip)
  • The extra bytes come from minification, not from extra code. Sourcemaps show the same modules in both builds. @radix-ui/themes is 86,953 B with the flag off and 98,717 B with it on. With the flag on, the shared chunk keeps names like SELECT_NAME and createSelectContext; with it off, root-*.js shortens them to yh, mh and so on.
  • 0.10.1 behaves the same. Its shared chunk is 464,556 B and also holds the eval.
  • The fix is worth about 89 KB. In a scratch build, changing eval( to (0, eval)( shrank the shared chunk from 468,178 B to 379,101 B. With that, /recharts with the flag on is about 17.5 KB lighter than with it off, and every page that loads the shared chunk benefits.

This is the remaining part of #7262: #7276 fixed the every-page growth, and this is the leftover.

Fix

  • Make every eval in state.js indirect ((0, eval)(...) or globalThis.eval(...)), or replace it:
    • for import(dataUri), use import(/* @vite-ignore */ dataUri) directly, or keep new Function if the dynamic import must stay hidden from the bundler;
    • for the event payloads, use new Function(...).
  • Indirect eval runs in global scope, so first confirm that none of the evaluated strings depend on state.js's local scope. The callback and call_script payloads are generated by reflex_base (rx.call_script, rx.call_function, event-chain callbacks). Grep the generators for identifiers that are only in state.js's module scope (for example applyEvent, refs or getRefValue), and export to globalThis or pass in as parameters whatever they rely on. refs is already published as a global since Publish refs as a global for 0.10 compatibility #7806.
  • Tests: tests/js/ vitest for the eval call sites with module-local names unavailable, and the existing integration tests for rx.call_script / rx.call_function callbacks. Optionally a size check: build an app with the flag on and off and assert the shared chunk shrinks.

Activity

  1. added
    perf
    release-train
    on Nov 12, 2026
  2. linear-code commented on Nov 12, 2026

    @linear-code
  3. added a commit that references this issue on Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    perfrelease-train

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions