Skip to content

Commit f36483d

Browse files
committed
add onRouteUpdated
1 parent d362416 commit f36483d

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

hydrate.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ const globalX = globalThis as unknown as {
1313
const match = getRouteMatcher(globalX.__ROUTES__);
1414

1515
export async function hydrate(
16-
Shell: React.ComponentType<{ children: React.ReactElement } & ServerSideProps>
16+
Shell: React.ComponentType<
17+
{ children: React.ReactElement } & ServerSideProps
18+
>,
19+
options?: Omit<React.PropsWithoutRef<typeof RouterHost>, "children" | "Shell">
1720
) {
1821
const matched = match(globalX.__INITIAL_ROUTE__.split("?")[0])!;
1922
const Initial = await import(matched.value);
2023
return hydrateRoot(
2124
document,
22-
<RouterHost Shell={Shell}>
25+
<RouterHost Shell={Shell} {...options}>
2326
<Shell {...globalX.__SERVERSIDE_PROPS__}>
2427
<Initial.default {...globalX.__SERVERSIDE_PROPS__?.props} />
2528
</Shell>

router/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ export const ReloadContext = createContext(async (): Promise<void> => {});
4242
export const RouterHost = ({
4343
children,
4444
Shell,
45+
onRouteUpdated,
4546
}: {
4647
children: React.ReactElement;
4748
Shell: React.ComponentType<{ children: React.ReactElement }>;
49+
onRouteUpdated?: (path: string) => void;
4850
}) => {
4951
const pathname = useLocationProperty(
5052
() => location.pathname + location.search,
@@ -64,6 +66,7 @@ export const RouterHost = ({
6466
if (props?.redirect) {
6567
navigate(props.redirect);
6668
} else {
69+
onRouteUpdated?.(target);
6770
setCurrent(
6871
<Shell {...props}>
6972
<module.default {...props?.props} />
@@ -81,6 +84,7 @@ export const RouterHost = ({
8184
location.href = pathname;
8285
});
8386
} else {
87+
onRouteUpdated?.(pathname);
8488
// @ts-ignore
8589
delete globalX.__INITIAL_ROUTE__;
8690
}

0 commit comments

Comments
 (0)