Run apps without state on a local event loop, without the backend transport

BE
14h ago
+3778-2832

Type of change

  • New feature (non-breaking change which adds functionality)

Description

Problem. An app with rx.App(enable_state=False) never connects, but its bundle still carries the whole websocket runtime: socket.io, hydration, deltas, the client storage sync, uploads and cookies. In a small stateless app that's a 93 KB chunk (31 KB gzip) on every page's critical path. Three things keep it there:

  • state.js imports its own namespace to hand every export to server-provided scripts;
  • the app root puts import * as utils_state on window.__reflex for dynamic components (#8035);
  • the context module always runs the backend-capable useEventLoop.

Change.

  • utils/state.js now re-exports two modules, so every name compiled pages and component packages import keeps working:
    • utils/runtime.js: the helpers compiled code calls, ReflexEvent, applyEventActions, the events the browser applies itself (isFrontendEvent, applyFrontendEvent, including the browser storage and cookie events) and useLocalEventLoop;
    • utils/transport.js: the socket, hydration, deltas, synced browser storage, uploads, cookies and useEventLoop. Nothing in it runs on import: Cookies is created on first use, and the transport warm-up is warmUpTransport(), which the context module of an app with state calls right after registerApp.
  • After a browser storage event, the transport hydrates again as before; the local loop only changes the storage.
  • The context template picks the loop at compile time. With state it runs useEventLoop from the transport, as before. Without state it runs useLocalEventLoop, which queues frontend events and applies them in dispatch order, like the transport's queue, and logs a console.error for an event that needs the backend; the state store serves the compiled initial state in the browser too, since no delta arrives. Nothing it imports reaches the transport.
  • socket.io declares no sideEffects and has top-level side effects (engine.io-client adds an offline listener), so the re-export alone would keep it in every build. The vite config declares utils/transport.js side-effect free, which it now is, so a build where nothing calls into it drops it and socket.io.
  • Script scope: unchanged with state (every export of utils/state.js, socket, token, ...). Without state, runLocalScript passes the exports of utils/runtime.js, addEvents, navigate and params. It can't be smaller: compiled rx.call_script/rx.call_function callbacks are strings that call ReflexEvent and addEvents (format_queue_events). The exports of utils/state.js would pull the transport back in.
  • Components import getBackendURL, isBackendDisabled and evalReactComponent from $/utils/transport (Dirs.TRANSPORT_PATH), which is also bundled for dynamic components.

On its own this PR doesn't shrink a stateless bundle yet, since window.__reflex still keeps every export; #8035 removes that, and #8034 turns a transport import in an app without state into a compile error.

Measured with #8035, on a two-page stateless app with client state (reflex export, JS loaded by the first view of /): 548.7 → 482.7 KB raw, 157.0 → 135.9 KB gzip. The shared chunk with state.js and socket.io (93.4 KB, 30.9 KB gzip) is gone; what stays of the runtime moves into the root and page chunks.

Tests

  • vitest local_event_loop.test.js: frontend events run in order without connecting, an event dispatched while an earlier one awaits running after the events queued before it, redirects, results reaching callbacks, client state events, browser storage and cookie events, a compiled callback string dispatching through the mounted loop, scripts seeing the runtime's exports but nothing of the transport, splat params, backend events reported, event actions.
  • vitest run_script.test.js: the upload URL rewrite of _download and the rehydration after _clear_local_storage, both now in the transport. public_api.test.js lists utils/runtime and utils/transport.
  • The node harnesses (state_js.test.mjs, client_event_queue_runtime.mjs, client_session.mjs) link the three modules and start the warm-up as the context module does.
  • test_context_js_template.py: stateless and stateful output, and the stateless provider tree. test_vite_plugin_react_compiler.py: only utils/transport.js is declared side-effect free.
  • Playwright test_stateless_app.py, dev and prod: client state, call_function/call_script with callbacks, links and redirects, no websocket to the backend and no console error.
  • The stateful Playwright suites for call_script, event chains and actions, uploads, client storage, hydration storage, navigation before connect, the connection banner, dynamic components, client state and server-side events pass (137).

Related: #6932 replaces socket.io inside the transport. #7949 adds reflex to the stateful script scope; runLocalScript should get it too, whichever lands second.