Skip to content

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

Open
benedikt-bartscher wants to merge 14 commits into
reflex-dev:mainfrom
benedikt-bartscher:stateless-runtime
Open

benedikt-bartscher wants to merge 14 commits into
reflex-dev:mainfrom
benedikt-bartscher:stateless-runtime

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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:

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.

@greptile-apps

greptile-apps Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge, with no outstanding findings.

Summary

The PR gives stateless apps a browser-only event loop and splits the shared runtime from the backend connection code.

  • Stateless apps run browser events in order without a backend connection.
  • Shared frontend helpers and backend transport now have separate module paths.

Reviews (8) · Last reviewed commit: "docs(news): keep the stateless runtime f..." · Reviewed by Greptile

@codspeed

codspeed Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 5.55%

2 warnings

⚠️ 13 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 1 improved benchmark
✅ 145 untouched benchmarks
⏩ 8 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_state_access[read_cached_computed] 48.2 ms 45.6 ms +5.55%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing benedikt-bartscher:stateless-runtime (e56e3dc) with main (8ef3f5f)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@benedikt-bartscher
benedikt-bartscher marked this pull request as ready for review November 26, 2026 22:37
@benedikt-bartscher
benedikt-bartscher requested a review from a team as a code owner November 26, 2026 22:37

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 32 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

# Conflicts:
#	packages/reflex-frontend/src/utils/state.js
@FarhanAliRaza FarhanAliRaza added the priority: P2 Medium: valid edge-case bug, reasonable feature, or docs fix label Nov 30, 2026 — with Claude
@benedikt-bartscher

Copy link
Copy Markdown
Contributor Author

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review

@benedikt-bartscher I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 35 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement Anything you want improved priority: P2 Medium: valid edge-case bug, reasonable feature, or docs fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants