Expose window.__reflex before the first render

Closed
MA
24d ago
+131-19

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)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

What

Assign window.__reflex at module scope of the compiled app root (inside one if (typeof window !== "undefined") block, merged with any existing value, with the lazy-library loaders and window.__reflex_load in the same block) instead of in a ReflexProviders useEffect. The boot delta is unchanged.

Fixes #7468.

Why

Since #7064 the first hydrate_and_load delta carries only the substates that differ from their compiled defaults, so a component that consumes an unchanged substate renders exactly once, during hydration. window.__reflex used to be assigned in a useEffect that runs after that render, so any component reading window.__reflex at render time on a prerendered prod route never saw it. 0.9.12 re-sent every substate at boot, which re-rendered every consumer and hid the ordering. User-visible result: the reflex-enterprise AG Grid with Var-valued column_defs or detail_cell_renderer_params rendered no columns on full load in prod (0.10.0a1 and a2, 0.9.12 fine). Found and independently confirmed by the 0.10.0a2 pre-release QA (prerelease_testing/2026-10-07/ent_grid/verification/NOTES.md on claude/reflex-prerelease-testing-t0sd90). Lazy mode with no optional libraries also still used the effect and had the same bug. Re-sending every substate at boot was rejected on purpose: it would give up #7064's gain and only hide the ordering again.

Test plan

  • Unit: tests/units/compiler/test_compiler.py::test_compile_app_root_exposes_window_libraries_before_first_render (default and lazy mode) checks the guarded module-scope assignment and that ReflexProviders no longer touches window.__reflex; the default case failed before the fix.
  • Integration: tests/integration/tests_playwright/test_hydration_storage.py gains an UntouchedState, a ReflexGlobalProbe component that reads typeof window.__reflex during render, and test_window_reflex_available_at_first_render. Before the fix dev passed and prod failed with untouched|undefined; after, 6/6 in dev and prod. test_mount_target.py, test_hmr.py, test_stateless_app.py still pass.
  • End to end against the QA verifier's fixtures in prod (worktree venv): the core-only probe on unchanged substates goes from NO_REFLEX (render count 1) to HAS_REFLEX; with the offline reflex-enterprise 0.9.7a4 wheel the State-var grid goes from 0 headers / 0 cells to 2 / 6 on full load, reload, after an unrelated event and in a second context; the State-params detail grid from [] to ['Count', 'Value']; the lambda rx.badge renderer and the dynamic component unchanged. Also passes in dev and in prod with REFLEX_FRONTEND_LAZY_BUNDLED_LIBRARIES=true.
  • ruff check / ruff format clean, pyright reflex tests 0 errors, pre-commit on the changed files passes, tests/units/compiler 328 passed; the full unit suite's only failures are the 232 reflex_cli/v2 version-check tests that fail identically on unmodified main in a shallow checkout.

Note for review: a component whose rendered output itself depends on window.__reflex now differs from the prerendered HTML, so React logs hydration error #418 and re-renders on the client. Lazy mode already behaved this way; the real grid pages show no #418.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SjCrbdguMkfe4bhbaREiwo


Generated by Claude Code