Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit dbbcab3

Browse files
committed
Improve redirect function
1 parent 5498894 commit dbbcab3

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

framework/core/redirect.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ import util from "../../lib/util.ts";
22
import events from "./events.ts";
33

44
let routerReady = false;
5-
let hasPreRedirect = false;
5+
let preRedirect: { url: URL; replace?: boolean } | null = null;
66

77
const onrouterready = (_: Record<string, unknown>) => {
88
events.off("routerready", onrouterready);
9-
if (hasPreRedirect) {
10-
events.emit("popstate", { type: "popstate", resetScroll: true });
9+
if (preRedirect) {
10+
events.emit("popstate", { type: "popstate", ...preRedirect });
11+
preRedirect = null;
1112
}
1213
routerReady = true;
1314
};
1415
events.on("routerready", onrouterready);
1516

1617
export function redirect(url: string, replace?: boolean) {
17-
const { location, history } = window;
18+
const { location } = window;
1819

1920
if (!util.isFilledString(url)) {
2021
return;
@@ -25,15 +26,10 @@ export function redirect(url: string, replace?: boolean) {
2526
return;
2627
}
2728

28-
if (replace) {
29-
history.replaceState(null, "", new URL(url, location.href));
30-
} else {
31-
history.pushState(null, "", new URL(url, location.href));
32-
}
33-
29+
const redirectOptions = { url: new URL(url, location.href), replace };
3430
if (routerReady) {
35-
events.emit("popstate", { type: "popstate", resetScroll: true });
36-
} else if (!hasPreRedirect) {
37-
hasPreRedirect = true;
31+
events.emit("popstate", { type: "popstate", ...redirectOptions });
32+
} else {
33+
preRedirect = redirectOptions;
3834
}
3935
}

framework/react/link.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export function Link(props: LinkProps) {
9393
timerRef.current = setTimeout(() => {
9494
timerRef.current = null;
9595
prefetch();
96-
}, 300);
96+
}, 150);
9797
}
9898
}, [prefetch, href, propOnMouseEnter]);
9999
const onMouseLeave = useCallback((e: MouseEvent) => {

framework/react/router.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export const Router: FC<RouterProps> = ({ ssrContext }) => {
134134
});
135135
};
136136
const onpopstate = async (e: Record<string, unknown>) => {
137-
const url = new URL(window.location.href);
137+
const url = (e.url as URL | undefined) || new URL(window.location.href);
138138
const matches = matchRoutes(url, routes);
139139
const modules = await Promise.all(matches.map(async ([ret, meta]) => {
140140
const { filename } = meta;
@@ -159,7 +159,12 @@ export const Router: FC<RouterProps> = ({ ssrContext }) => {
159159
}));
160160
setModules(modules);
161161
setUrl(url);
162-
if (e.resetScroll) {
162+
if (e.url) {
163+
if (e.replace) {
164+
history.replaceState(null, "", e.url as URL);
165+
} else {
166+
history.pushState(null, "", e.url as URL);
167+
}
163168
window.scrollTo(0, 0);
164169
}
165170
};

0 commit comments

Comments
 (0)