Move the frontend javascript into its own package, reflex-frontend
Merged
FA
15d ago+2780+2780+0123456789012345678901234567890123456789-413-413-012345678901234567890123456789
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/webtree.tests/runs under vitest + jsdom against the realsocket.io-client,react-router,mergiciananduniversal-cookie, pinned to the versions an app installs.- The package is
privateand is not published to npm.reflex-frontendis a placeholder name. - The reflex-base wheel embeds
src/asreflex_base/.templates/webwhen 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 asmain, byte for byte. The later commits change the source, as described below. - In a checkout there is no embedded copy:
Templates.Dirs.WEB_TEMPLATEresolves topackages/reflex-frontend/src, so the nextreflex initpicks up an edit without a rebuild. An editable install embeds nothing.
The commits, in order
| Commit | What |
|---|---|
76e7e0c, 804887e | The move, and everything it needed around it. No change to shipped files. |
bd55640 | Bug 1: generateUUID variant. |
d81bb8e | Bug 2: IPv6 wildcard backend. |
3d37da2 | Bugs 3 and 4: queue drains spinning on a dropped connection. |
725b494 | Bug 5: unmounted event loop reconnecting on tab visibility. |
e08cae4 | Lint and type-check gates, their behaviour-preserving fixes in the source, and the export contract test. |
fec30c8 | Bug 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 apyproject.toml, so the workspace excludes it andbuild_packages.ymlskips 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-dirsnames directories outside a package that it also ships; this repository listspackages/reflex-frontend/srcunder reflex-base. - Pins: a unit test fails when
packages/reflex-frontend/package.jsondrifts fromPackageJson.DEPENDENCIES. Bumping an npm pin is now two edits plus the lockfile. - Python tests that read a frontend file go through
Templates.Dirs.WEB_TEMPLATErather 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:
generateUUIDset a reserved variant on about half its ids, so those session tokens were not valid version 4 UUIDs.getBackendURLnever treated an IPv6 wildcard backend as same-domain:URL.hostnamereturns"[::]", which the list's entries could never match.- 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.
- The event loop's effect had the same loop, with the same result.
- An unmounted event loop reconnected when the tab became visible: its
visibilitychangelistener was never removed, and treated the cleared socket as "not connected yet". - 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, aparseIntradix,Number.isNaN, a scopedcasedeclaration, one accumulating spread. No behaviour change intended; the diff is mechanical.noFloatingPromisesis on forsrc: a promise nothing awaits is either markedvoidor a finding. - Type check:
tscover the JSDoc withcheckJs, nothing emitted, so there is still no compile step.types/globals.d.tsdeclares every global the runtime reads implicitly (window.__reflex*,$/env.json,import.meta.hot). - Export contract:
tests/public_api.test.jspins the names each module exports, and imports each one in node, where production builds prerender. npm packon 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.
ruffandpyrightclean. - Real browser: a small app run from the checkout and driven through Chromium after the last commit: state events, an awaited
rx.call_scriptwith a state event queued behind it, a valid v4 session token, no console errors; and the same app withsessionStorageblocked, which raised 45,128 unhandled rejections in four seconds beforefec30c8and 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_cliandtests/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 loadstate.jsthrough 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 iseval'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.jsononly resolve inside an app's.web, and npm leavessrc/.gitignoreout of the tarball; the package README lists what is missing.