Repository navigation
Add reflex, the API browser code reaches Reflex through - #7949
FarhanAliRaza wants to merge 7 commits into
Conversation
Code run by rx.call_script sees every export of utils/state.js, so apps and Reflex itself depend on frontend internals by accident, and none of them can move. This adds one defined object instead, utils/script_env.js: env.ref(id) the ref of a component, named as format_ref names it env.refs the ref registry env.addEvents(...) dispatch through the event loop mounted at call time env.event(...) build an event for addEvents env.library(name) a bundled library, loaded first when loading is lazy Script code sees it as `env`. On the Python side rx.js.env (reflex_base.js) builds the same calls as vars; it renders under the alias __reflexEnv, which runScript also passes in, so one expression works compiled into a page and sent from the backend, and cannot collide with the `env` pages import from env.json. Every member reads the runtime at call time, so the module loads in either order despite its import cycle with utils/state.js. Nothing existing changes: scripts keep every name they saw before. Part of ENG-9127. Claude-Session: https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf
[Medium impact] The PR reaches browser scripts and Python event code in Reflex apps. The stack needs caller updates before merging because both dependent PRs still use the removed API. Findings
SummaryAdds
Reviews (4) · Last reviewed commit: "Document reflex and rx.js usage-first" · Reviewed by Greptile |
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
`env` inside scripts shadowed the `env` that `rx.get_upload_url` compiles to (`getBackendURL(env.UPLOAD)`), so a backend script linking to an upload opened `.../ping/a.png`; since the Function-constructor runScript it was a ReferenceError. The API is now `reflex`, exported from utils/reflex.js, and runScript passes `env` (the app's env.json) as the direct eval did in 0.11.0. The API is narrowed to what is documented: `refs` is gone (`ref(id)` covers it and keeps the registry key format private), `addEvents` takes only the events, and `library` rejects `$/` frontend modules. Each `library` error names the library, the cause and the fix, including a call made before the app root registered its libraries. Claude-Session: https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf
`rx.js.env.ref(...)` becomes `rx.js.ref(...)`, and likewise for
`add_events` and `library`; each renders as a call on `reflex`, which a
page imports from `$/utils/reflex` and runScript passes to backend
scripts, so the `__reflexEnv` alias and the public `ScriptEnvVar` class
are gone, as is `refs`. `rx.js.ref(id).current` is typed, so
`rx.js.ref("search").current.value` reads the element's value.
`add_events` now builds each event through `call_event_handler`, so a
handler that still needs arguments fails when the var is built, with a
message naming the handler and the fix, instead of dispatching an empty
payload at runtime.
Claude-Session: https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf
Covers `reflex.ref` from a string script, `rx.js.add_events` from a trigger and from the backend, `rx.js.ref` read by the backend, `reflex.library` from on_mount, and an `rx.get_upload_url` script sent from the backend resolving to the upload endpoint. Claude-Session: https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf
Rename "The Script Environment" to "Reaching Reflex from JavaScript": a runnable demo from a script, a JavaScript module, rx.js from Python, the rule to prefer `reflex` over other names in scope, and each error with its fix. News fragments follow the rename, and the unreleased script scope fragment lists `env` again. Claude-Session: https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf
reflex, the API browser code reaches Reflex through
| */ | ||
| const refName = (id) => `ref_${String(id).replace(/[^\p{L}\p{N}_]+/gu, "_")}`; | ||
|
|
||
| export const reflex = Object.freeze({ |
There was a problem hiding this comment.
PRs #7950 and #7951 still write a page_script.js that imports env from $/utils/script_env and calls env.addEvents. This rename removes that module, so their test app cannot bundle.
Both PRs also retain environment.py, which reads the removed Dirs.SCRIPT_ENV_PATH and builds calls to __reflexEnv, which runScript no longer provides. Update those dependent callers and exports to the new API before merging the stack.
There was a problem hiding this comment.
Correct. #7950 and #7951 are being rebased onto this change now: page_script.js will import { reflex } from $/utils/reflex, and environment.py, Dirs.SCRIPT_ENV_PATH and __reflexEnv are being removed from them. I'll resolve this thread once both PRs are pushed with the new API.
Generated by Claude Code
Type of change
Description
First of a stacked series for ENG-9127 (safer script evaluation).
Problem. Code run by
rx.call_scriptsees every export ofutils/state.js, plussocket,navigate,paramsandtoken. Apps and Reflex itself come to depend on frontend internals by accident, and none of them can move.Change. This PR adds one defined object,
reflex(utils/reflex.js), and its Python mirror onrx.js:rx.js)reflex.ref(id)rx.js.ref(id)id. Itscurrentis the element.reflex.addEvents(events)rx.js.add_events(*events)reflex.event(name, payload)addEvents.reflex.library(name)rx.js.library(name)Why the name
reflex. Comparable frameworks name their browser API after the framework: Shiny'sShiny, htmx'shtmx, Livewire's$wire, Dash'sdash_clientside. A generic name collides with an app's own names. An earlier draft called itenv, which shadowed theenvfromenv.jsonthatrx.get_upload_urlrenders (getBackendURL(env.UPLOAD)). That made upload URLs in backend scripts silently wrong. Scripts now see both names:env(the config, as in v0.11) andreflex.Errors. Each one states the problem, its cause and the fix:
rx.js.add_eventswith a handler that needs arguments fails at definition time. The message says which handler failed and how to pass the arguments.reflex.libraryrejects Reflex internals ($/...), a library the app does not bundle (the message says to callbundle_library(...)), and calls made before the app has mounted (the message says to call it from an event such ason_mount).Out of scope.
reflexexposes nostateand noqueueEvents; those need a concrete use first.ref_<id>registry keys stay private.Tests
tests/reflex.test.js: each member, with exact values and error texts; late binding; the object is frozen; load order across the import cycle withstate.js.run_script.test.js: the names a backend script sees, plus a regression test forgetBackendURL(env.UPLOAD)→…/_upload/a.png.tests/units/reflex_base/test_js.py: the rendered expressions and imports, and the exact error for a handler that needs arguments.test_call_script.py::test_reflex_api:reflexfrom a string script;rx.jsfrom an event trigger and from a backend handler;refread from the backend;libraryfromon_mount.npm run checkare clean.https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf