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

Commit d7e3ac3

Browse files
committed
Add router state to handle pre-redirect (#283)
1 parent e136a59 commit d7e3ac3

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

framework/core/redirect.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import util from '../../shared/util.ts'
22
import events from './events.ts'
33

4+
const routerState = {
5+
ready: false,
6+
hasDeferredRedirect: false,
7+
}
8+
9+
events.once('routerstate', state => {
10+
if (routerState.hasDeferredRedirect) {
11+
events.emit('popstate', { type: 'popstate', resetScroll: true })
12+
}
13+
Object.assign(routerState, state)
14+
})
15+
416
export async function redirect(url: string, replace?: boolean) {
517
const { location, history } = window as any
618

@@ -19,5 +31,10 @@ export async function redirect(url: string, replace?: boolean) {
1931
} else {
2032
history.pushState(null, '', url)
2133
}
22-
events.emit('popstate', { type: 'popstate', resetScroll: true })
34+
35+
if (routerState.ready) {
36+
events.emit('popstate', { type: 'popstate', resetScroll: true })
37+
} else if (!routerState.hasDeferredRedirect) {
38+
routerState.hasDeferredRedirect = true
39+
}
2340
}

framework/react/components/Router.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export default function Router({
7272
useEffect(() => {
7373
window.addEventListener('popstate', onpopstate)
7474
events.on('popstate', onpopstate)
75+
events.emit('routerstate', { ready: true })
7576

7677
return () => {
7778
window.removeEventListener('popstate', onpopstate)

0 commit comments

Comments
 (0)