fix: address frontend template, visibility, and storage error follow-ups (#7869)

MI
3h ago
+723-77

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?

Summary

Fixes #7869 (follow-ups from #7794):

  1. Prefer packages/reflex-frontend/src in a source checkout (reflex_base/constants/base.py, packages/reflex-base/hatch_build.py)

    • Updated _web_template_dir(root_dir) to prefer packages/reflex-frontend/src when utils/state.js exists there, and to require utils/state.js inside .templates/web before treating it as an embedded copy (so leftover directories such as .templates/web/.DS_Store or stale files in a checkout never shadow the frontend package).
    • Updated FrontendBuildHook in packages/reflex-base/hatch_build.py to also require utils/state.js when checking for an sdist's embedded copy.
    • Updated tests/units/reflex_base/constants/test_base.py and added regression tests in test_base.py and tests/units/test_build_config.py.
  2. Remove dead visibility branch and keep cleanup next to the socket ref (packages/reflex-frontend/src/utils/state.js)

    • Removed the unreachable if (!socket.current) self-removal branch from checkVisibility.
    • Stopped mutating socket.current.stopWatchingVisibility onto the socket.io-client Socket instance: connect() now attaches the visibilitychange listener only if socket.current is still the active socket after await socket.current.reconnect() and returns the cleanup callback, which useEventLoop stores in a stopWatchingVisibility ref alongside the socket ref and invokes before clearing socket.current.
    • Updated packages/reflex-frontend/tests/state_connection.test.js and added a regression test for unmounting mid-connect.
  3. Surface a clear browser-storage-blocked connection error (packages/reflex-base/src/reflex_base/constants/event.py, packages/reflex-frontend/src/utils/state.js, packages/reflex-components-core/src/reflex_components_core/core/banner.py)

    • Added ClientErrorType.STORAGE_BLOCKED = storage_blocked and matching ERROR_TYPE_STORAGE_BLOCKED in state.js.
    • Wrapped sessionStorage / localStorage access in getToken, setToken (SecurityError), and hydrateClientStorage to throw a storage_blocked-coded Error (preserving the underlying cause) telling the user that site storage is blocked by the browser and to allow cookies/site data and reload; guarded optional localStorage refresh-hint accesses in reconnect and session_token error handling, and reported new_token storage errors via setConnectErrors instead of rejecting.
    • Added has_storage_blocked_error in banner.py, handled it immediately (before the 2-error threshold, without claiming the server is unreachable) in ConnectionToaster, and handled both has_storage_blocked_error and has_hydration_payload_error in default_connection_error() / ConnectionModal.
    • Added/updated unit tests in tests/units/components/core/test_banner.py, tests/units/test_app.py, tests/units/compiler/state_js.test.mjs, packages/reflex-frontend/tests/state.test.js, and packages/reflex-frontend/tests/state_connection.test.js.

Local Verification

  • Frontend (packages/reflex-frontend): npm run check (biome lint, tsc -p tsconfig.json, tsc -p tsconfig.node.json, vitest run: 10 files / 219 passed), biome format, npm run build (npm pack)
  • Python targeted suites: pytest tests/units/reflex_base/constants/ tests/units/components/core/test_banner.py tests/units/compiler/test_state_js_template.py tests/units/test_build_config.py tests/units/test_app.py (497 passed, 2 skipped) and broader pytest tests/units/reflex_base/ tests/units/components/ tests/units/compiler/ tests/units/test_app.py (8718 passed, 38 skipped)
  • Linters & type checks: ruff check ., ruff format --check ., codespell, make_pyi.py --check, pyright --pythonplatform Linux (touched packages & tests), ty check --python-version 3.14 tests/type_checking, pyright --pythonversion 3.11/3.14 tests/type_checking, reflex-release sync --check, reflex-release changelog-check, reflex-release check-headings