Skip to content

Commit 05e7012

Browse files
committed
Allow reconnections to re-obtain the current URL
1 parent 8e9b6a8 commit 05e7012

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/js/src/index.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ export function bind(node) {
1212
};
1313
}
1414

15-
export function History({ onBrowserBack }) {
15+
export function History({ onHistoryChange }) {
1616
// Capture browser "history go back" action and tell the server about it
17-
// Note: Browsers do not allow you to detect "history go forward" actions.
17+
// Note: Browsers do not allow us to detect "history go forward" actions.
1818
React.useEffect(() => {
19-
// Register a listener for the "popstate" event and send data back to the server using the `onBrowserBack` callback.
19+
// Register a listener for the "popstate" event and send data back to the server using the `onHistoryChange` callback.
2020
const listener = () => {
21-
onBrowserBack({
21+
onHistoryChange({
2222
pathname: window.location.pathname,
2323
search: window.location.search,
2424
});
@@ -30,6 +30,17 @@ export function History({ onBrowserBack }) {
3030
// Delete the event listener when the component is unmounted
3131
return () => window.removeEventListener("popstate", listener);
3232
});
33+
34+
// Tell the server about the URL during the initial page load
35+
// FIXME: This currently runs every time any component is mounted due to a ReactPy core rendering bug.
36+
// https://github.com/reactive-python/reactpy/pull/1224
37+
React.useEffect(() => {
38+
onHistoryChange({
39+
pathname: window.location.pathname,
40+
search: window.location.search,
41+
});
42+
return () => {};
43+
}, []);
3344
return null;
3445
}
3546

@@ -49,15 +60,17 @@ export function Link({ onClick, linkClass }) {
4960
};
5061

5162
// Register the event listener
52-
document
53-
.querySelector(`.${linkClass}`)
54-
.addEventListener("click", handleClick);
63+
let link = document.querySelector(`.${linkClass}`);
64+
if (link) {
65+
link.addEventListener("click", handleClick);
66+
}
5567

5668
// Delete the event listener when the component is unmounted
5769
return () => {
58-
document
59-
.querySelector(`.${linkClass}`)
60-
.removeEventListener("click", handleClick);
70+
let link = document.querySelector(`.${linkClass}`);
71+
if (link) {
72+
link.removeEventListener("click", handleClick);
73+
}
6174
};
6275
});
6376
return null;

src/reactpy_router/routers.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ def router(
6969
for element, params in match
7070
]
7171

72-
def on_browser_back(event: dict[str, Any]) -> None:
72+
def on_history_change(event: dict[str, Any]) -> None:
7373
"""Callback function used within the JavaScript `History` component."""
74-
set_location(Location(**event))
74+
new_location = Location(**event)
75+
if location != new_location:
76+
set_location(new_location)
7577

7678
return ConnectionContext(
77-
History({"onBrowserBack": on_browser_back}), # type: ignore[return-value]
79+
History({"onHistoryChange": on_history_change}), # type: ignore[return-value]
7880
html._(route_elements),
7981
value=Connection(old_conn.scope, location, old_conn.carrier),
8082
)

0 commit comments

Comments
 (0)