Skip to content

Commit 1b93177

Browse files
committed
fix: detect redirect the dumb way
1 parent 30382b3 commit 1b93177

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/utils/hooks/useFastRedirect.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useConfigs } from 'containers/ConfigsContext'
22
import { platform } from 'platforms'
33
import * as React from 'react'
4-
import { useEvent } from 'react-use'
4+
import { useEvent, useInterval } from 'react-use'
55

66
const config: import('pjax-api').Config = {
77
areas: [
@@ -54,8 +54,17 @@ export const loadWithFastRedirect = (url: string, element: HTMLElement) => {
5454
}
5555

5656
export function useAfterRedirect(callback: () => void) {
57-
useEvent('pjax:end', callback, document) // legacy support
58-
useEvent('turbo:render', callback, document) // prevent page content shift after first redirect to new page via turbo when sidebar is pinned
57+
const latestHref = React.useRef(location.href)
58+
const raceCallback = React.useCallback(() => {
59+
const { href } = location
60+
if (latestHref.current !== href) {
61+
latestHref.current = href
62+
callback()
63+
}
64+
}, [callback])
65+
useInterval(raceCallback, 500)
66+
useEvent('pjax:end', raceCallback, document) // legacy support
67+
useEvent('turbo:render', raceCallback, document) // prevent page content shift after first redirect to new page via turbo when sidebar is pinned
5968
}
6069

6170
export function useRedirectedEvents(

0 commit comments

Comments
 (0)