Skip to content

Detect frontend/backend state schema mismatch at connect and prompt a reload #7552

Closed

Description

@FarhanAliRaza

Problem

The compiled frontend bundle and the backend can come from different builds:

  • A backend deploy changes the state, and a browser keeps the old bundle.
  • Two backend versions run behind one load balancer during a rolling deploy.
  • api_url points at a backend that did not serve the bundle.

#7435 makes the frontend skip delta substates it does not know (#7247). The page stays usable, but the skew stays hidden:

  • A renamed or moved state keeps its old frontend substate, and that substate stops updating without a visible error.
  • An event sent to an old handler name fails on the backend.
  • A changed var inside a known state is not detected at all.

Only the developer sees a signal: one console warning and one backend log line.

This issue is the concrete design for #6250.

What exists

  • BaseState._to_schema() (reflex/state.py) hashes the base var names and types of one state class. The state manager uses it to discard stored state that does not match the code. It does not include computed vars, event handler names, or the state tree.
  • FrontendEventExceptionState.auto_reload_on_errors (reflex/state.py) reloads the page after some frontend errors. A sessionStorage cooldown (LAST_RELOADED_KEY, auto_reload_cooldown()) stops reload loops.
  • The frontend sends the hydrate event in the socket.io CONNECT packet (socket.current.auth, bootAuth in state.js). It sends it again on each reconnect. The backend reads it in EventNamespace.on_connect(..., auth).

Proposal

  1. Schema hash. Compute one hash of everything the compiled frontend depends on, for each state in the tree:

    • the full name
    • the frontend var names, base and computed
    • the event handler names

    Use one function for the compiler and the backend, so the two values cannot drift.

  2. Frontend. Compile the hash into the bundle next to SUBSTATES (in the context_template output). The hash then changes exactly when the compiled state tree changes.

  3. Handshake. Send the hash in the CONNECT auth payload next to the hydrate event. Every reconnect sends it again. A backend can only get new state code through a process restart, and a restart drops the socket. So checking on each connect is enough.

  4. Backend. Compute the hash once at startup. On a mismatch in on_connect, send a mismatch event to that client. Also log it once.

  5. Client.

    • In prod: show a visible "A new version is available" banner with a reload button, or reload once. Use the existing auto-reload cooldown to stop reload loops when two backend versions are behind one load balancer.
    • In dev: only warn. A backend hot reload can reconnect before Vite has the new bundle, which gives a short false mismatch.
  6. Keep the fix(state): recover from unknown frontend substates #7435 fallback. Deltas that arrive between connect and the reload still skip unknown substates.

Compatibility

  • An old frontend sends no hash. The backend must ignore a missing hash.
  • An old backend ignores the new auth key.

Out of scope

  • Load balancer skew needs sticky sessions or pinning each client to one deployment, like Vercel Skew Protection. Document this; the framework cannot fix it.

Acceptance

  • A frontend bundle compiled without a state, connected to a backend with that state, shows the banner in prod. Only a console warning appears in dev.
  • A changed var in a known state is detected. Today nothing detects it.
  • No banner or warning appears when the bundle and the backend match.
  • A reconnect to a different backend version runs the check again.
  • Two backend versions behind a non-sticky proxy cause at most one reload per cooldown window.

Prior art

  • Next.js deploymentId: a mismatch causes a hard navigation.
  • Inertia asset versioning: 409 plus X-Inertia-Location.
  • Phoenix LiveView phx-track-static / static_changed?: the app shows a reload prompt.
  • SvelteKit updated with version.pollInterval.

Related: #6250, #7247, #7435.

Activity

  1. linear-code commented on Oct 13, 2026

    @linear-code
  2. added a commit that references this issue on Oct 14, 2026
    02b92bb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions