Skip to content

Commit 9dfbbc4

Browse files
authored
Do a full re-hydrate on reconnect (#5980)
It turns out that just rehydrating and not also re-running the on_load handlers can leave applications in a bad state; particularly when their background tasks exiting are predicated on the `sid`, that will change on reconnect, but without an on_load, no mechanism is there to restart the tasks.
1 parent a8bf59e commit 9dfbbc4

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed

reflex/.templates/web/utils/state.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -618,19 +618,13 @@ export const connect = async (
618618
window.addEventListener("unload", disconnectTrigger);
619619
if (socket.current.rehydrate) {
620620
socket.current.rehydrate = false;
621-
const events = initialEvents();
622-
if (events.length > 0) {
623-
// On reconnect, we only hydrate, do not re-run on_load events.
624-
const hydrate_event = initialEvents()[0];
625-
hydrate_event.payload.is_reconnect = true;
626-
queueEvents(
627-
[hydrate_event],
628-
socket,
629-
true,
630-
navigate,
631-
() => params.current,
632-
);
633-
}
621+
queueEvents(
622+
initialEvents(),
623+
socket,
624+
true,
625+
navigate,
626+
() => params.current,
627+
);
634628
}
635629
// Drain any initial events from the queue.
636630
while (event_queue.length > 0 && !event_processing) {

reflex/middleware/hydrate_middleware.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,11 @@ async def preprocess(
3535
if event.name != get_hydrate_event(state):
3636
return None
3737

38-
# In reconnect mode, don't reset client storage or call on_load.
39-
is_reconnect = event.payload.get("is_reconnect", False)
40-
if not is_reconnect:
41-
# Clear client storage, to respect clearing cookies
42-
state._reset_client_storage()
43-
44-
# Mark state as not hydrated (until on_loads are complete)
45-
setattr(state, constants.CompileVars.IS_HYDRATED, False)
38+
# Clear client storage, to respect clearing cookies
39+
state._reset_client_storage()
40+
41+
# Mark state as not hydrated (until on_loads are complete)
42+
setattr(state, constants.CompileVars.IS_HYDRATED, False)
4643

4744
# Get the initial state.
4845
delta = await _resolve_delta(state.dict())

0 commit comments

Comments
 (0)