Skip to content

Commit c126be9

Browse files
committed
Temporary fix for History's first load behavior
1 parent 831aa20 commit c126be9

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/js/src/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ export function History({ onHistoryChangeCallback }) {
5656
return null;
5757
}
5858

59+
/**
60+
* FirstLoad component that captures the URL during the initial page load and notifies the server.
61+
*
62+
* @param {Object} props - The properties object.
63+
* @param {Function} props.onFirstLoadCallback - Callback function to notify the server about the first load.
64+
* @returns {null} This component does not render any visible output.
65+
* @description
66+
* This component sends the current URL to the server during the initial page load.
67+
* @see https://github.com/reactive-python/reactpy/pull/1224
68+
*/
69+
export function FirstLoad({ onFirstLoadCallback }) {
70+
// FIXME: This component only exists because of a ReactPy core rendering bug, and should be removed when the bug
71+
// is fixed. Ideally all this logic would be handled by the `History` component.
72+
React.useEffect(() => {
73+
onFirstLoadCallback({
74+
pathname: window.location.pathname,
75+
search: window.location.search,
76+
});
77+
return () => {};
78+
}, []);
79+
return null;
80+
}
5981

6082
/**
6183
* Link component that captures clicks on anchor links and notifies the server.

src/reactpy_router/components.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
)
3333
"""Client-side portion of the navigate component"""
3434

35+
FirstLoad = export(
36+
module_from_file("reactpy-router", file=Path(__file__).parent / "static" / "bundle.js"),
37+
("FirstLoad"),
38+
)
39+
3540
link_js_content = (Path(__file__).parent / "static" / "link.js").read_text(encoding="utf-8")
3641

3742

src/reactpy_router/routers.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from reactpy.backend.types import Connection, Location
1212
from reactpy.types import ComponentType, VdomDict
1313

14-
from reactpy_router.components import History
14+
from reactpy_router.components import FirstLoad, History
1515
from reactpy_router.hooks import _route_state_context, _RouteState
1616
from reactpy_router.resolvers import StarletteResolver
1717
from reactpy_router.types import CompiledRoute, Resolver, Router, RouteType
@@ -46,6 +46,7 @@ def router(
4646

4747
old_conn = use_connection()
4848
location, set_location = use_state(old_conn.location)
49+
first_load, set_first_load = use_state(True)
4950

5051
resolvers = use_memo(
5152
lambda: tuple(map(resolver, _iter_routes(routes))),
@@ -69,8 +70,15 @@ def on_history_change(event: dict[str, Any]) -> None:
6970
if location != new_location:
7071
set_location(new_location)
7172

73+
def on_first_load(event: dict[str, Any]) -> None:
74+
"""Callback function used within the JavaScript `FirstLoad` component."""
75+
if first_load:
76+
set_first_load(False)
77+
on_history_change(event)
78+
7279
return ConnectionContext(
7380
History({"onHistoryChangeCallback": on_history_change}), # type: ignore[return-value]
81+
FirstLoad({"onFirstLoadCallback": on_first_load}) if first_load else "",
7482
*route_elements,
7583
value=Connection(old_conn.scope, location, old_conn.carrier),
7684
)

0 commit comments

Comments
 (0)