Skip to content

Commit 4a76e7d

Browse files
committedApr 15, 2024
fix: register event listener only for client
1 parent e820f4d commit 4a76e7d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed
 

‎.changeset/lucky-dingos-happen.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/router": patch
3+
---
4+
5+
Fix state updates detection on history back/forward

‎src/routing.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
createSignal,
88
on,
99
onCleanup,
10-
onMount,
1110
untrack,
1211
useContext,
1312
startTransition,
@@ -327,9 +326,11 @@ export function createRouterContext(
327326
}
328327
};
329328

330-
const handlePopState = () => setState(window.history.state);
331-
onMount(() => window.addEventListener("popstate", handlePopState));
332-
onCleanup(() => window.removeEventListener("popstate", handlePopState));
329+
if (!isServer) {
330+
const syncState = () => setState(window.history.state);
331+
window.addEventListener("popstate", syncState);
332+
onCleanup(() => window.removeEventListener("popstate", syncState));
333+
}
333334

334335
createRenderEffect(() => {
335336
const { value, state } = source();

0 commit comments

Comments
 (0)
Please sign in to comment.