Move the frontend javascript into its own package, reflex-frontend

Merged
FA
15d ago
+2780-413

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

What this does

The javascript every Reflex app runs on lived inside the reflex-base python package, at .templates/web, with its tests in a separate harness (tests/js) that could not install the modules' real dependencies. utils/state.js, the largest module, could not be imported from a test at all.

It is now a javascript package of its own, reflex-frontend, at packages/reflex-frontend/:

  • src/ is the old .templates/web tree. tests/ runs under vitest + jsdom against the real socket.io-client, react-router, mergician and universal-cookie, pinned to the versions an app installs.
  • The package is private and is not published to npm. reflex-frontend is a placeholder name.
  • The reflex-base wheel embeds src/ as reflex_base/.templates/web when it is built, through a build hook (packages/reflex-base/hatch_build.py). The move itself changes nothing in an installed Reflex: after the first two commits the wheel carries the same 26 files as main, byte for byte. The later commits change the source, as described below.
  • In a checkout there is no embedded copy: Templates.Dirs.WEB_TEMPLATE resolves to packages/reflex-frontend/src, so the next reflex init picks up an edit without a rebuild. An editable install embeds nothing.

The commits, in order

CommitWhat
76e7e0c, 804887eThe move, and everything it needed around it. No change to shipped files.
bd55640Bug 1: generateUUID variant.
d81bb8eBug 2: IPv6 wildcard backend.
3d37da2Bugs 3 and 4: queue drains spinning on a dropped connection.
725b494Bug 5: unmounted event loop reconnecting on tab visibility.
e08cae4Lint and type-check gates, their behaviour-preserving fixes in the source, and the export contract test.
fec30c8Bug 6: a failed connection attempt looping through the error handler; floating-promise lint rule.

What had to change around the move

  • uv workspace / CI discovery: it is the one directory under packages/ without a pyproject.toml, so the workspace excludes it and build_packages.yml skips directories that have none.
  • reflex-release: paths are mapped to packages by directory, so a frontend change would no longer count as a change to reflex-base. New setting [tool.reflex-release] extra-source-dirs names directories outside a package that it also ships; this repository lists packages/reflex-frontend/src under reflex-base.
  • Pins: a unit test fails when packages/reflex-frontend/package.json drifts from PackageJson.DEPENDENCIES. Bumping an npm pin is now two edits plus the lockfile.
  • Python tests that read a frontend file go through Templates.Dirs.WEB_TEMPLATE rather than a path.

Bugs the new tests found, fixed here

Six, all in utils/state.js. Each is described in a comment below before its fix, and has a regression test that failed first and its own bugfix fragment:

  1. generateUUID set a reserved variant on about half its ids, so those session tokens were not valid version 4 UUIDs.
  2. getBackendURL never treated an IPv6 wildcard backend as same-domain: URL.hostname returns "[::]", which the list's entries could never match.
  3. The socket's connect handler could spin forever draining the queue if the connection dropped while an awaited frontend event was pending with a state event behind it. Nothing but microtasks ran again: the tab froze.
  4. The event loop's effect had the same loop, with the same result.
  5. An unmounted event loop reconnected when the tab became visible: its visibilitychange listener was never removed, and treated the cleared socket as "not connected yet".
  6. A connection that could not be attempted looped through the error handler: with site storage blocked, an un-awaited promise rejected, the rejection handler reported it by trying to connect again, and the page raised about 12,000 unhandled rejections a second (measured in Chromium) without ever showing a connection error.

Found and reported, not changed: debounce/throttle state shared between components that fire the same handler; window.onerror being assigned rather than listened to; the eval scope contract that keeps state.js one module.

Quality gates added (npm run check, run by the js-unit-tests job)

  • Lint: biome, configured in lint.biome.jsonc, with each exception explained next to it. Its findings are fixed: strict equality, Array.isArray, a parseInt radix, Number.isNaN, a scoped case declaration, one accumulating spread. No behaviour change intended; the diff is mechanical. noFloatingPromises is on for src: a promise nothing awaits is either marked void or a finding.
  • Type check: tsc over the JSDoc with checkJs, nothing emitted, so there is still no compile step. types/globals.d.ts declares every global the runtime reads implicitly (window.__reflex*, $/env.json, import.meta.hot).
  • Export contract: tests/public_api.test.js pins the names each module exports, and imports each one in node, where production builds prerender.
  • npm pack on every run, so the manifest stays publishable.

Testing

  • JS: 143 tests in the package (29 moved, 114 new); lint and type check clean.
  • Python: 13,825 unit tests passed on the move. After the later commits I re-ran the suites that read or execute the frontend source, not the whole suite. ruff and pyright clean.
  • Real browser: a small app run from the checkout and driven through Chromium after the last commit: state events, an awaited rx.call_script with a state event queued behind it, a valid v4 session token, no console errors; and the same app with sessionStorage blocked, which raised 45,128 unhandled rejections in four seconds before fec30c8 and none after.
  • Builds (on the move commits): reflex-base as sdist and wheel, wheel-from-sdist equal to the direct wheel, and the sdist installed into a clean venv outside the repository embeds all 26 files.
  • Not run locally: tests/units/reflex_cli and tests/units/reflex_bench (sandbox limits), the Playwright integration tests, the Docker init test, Windows.

Not in this PR

  • The older node-driven tests under tests/units (*.mjs, about 1,900 lines) still load state.js through a source-rewriting harness; only their paths changed.
  • JS owned by other python packages (otel.js, GradientProfile.js, bench_page.js, the site-shared assets) stays where it is.
  • Splitting state.js. Event code is eval'd inside it and resolves names from its module scope, so moving functions out needs an integration test for that contract first.
  • Publishing. $/... imports and the per-app $/env.json only resolve inside an app's .web, and npm leaves src/.gitignore out of the tarball; the package README lists what is missing.

https://claude.ai/code/session_01LwJUCSZCLWw5meAowrx3Qt