(enabled: boolean)
| 63 | // reading History. Reads document.title after a short delay so the page's |
| 64 | // SEO effect has set it. |
| 65 | export const useRecordRecentPages = (enabled: boolean): void => { |
| 66 | const router = useRouter(); |
| 67 | |
| 68 | useEffect(() => { |
| 69 | if (!enabled || typeof window === 'undefined' || !router?.events) { |
| 70 | return undefined; |
| 71 | } |
| 72 | |
| 73 | let timeoutId: number | undefined; |
| 74 | const capture = (url: string) => { |
| 75 | if (withoutLayoutVariantPrefix(router.pathname) === '/posts/[id]') { |
| 76 | return; |
| 77 | } |
| 78 | const path = url.split('?')[0].split('#')[0]; |
| 79 | // Only the latest navigation matters — drop any pending capture so |
| 80 | // rapid navigation doesn't stack timers or record stale titles. |
| 81 | window.clearTimeout(timeoutId); |
| 82 | timeoutId = window.setTimeout(() => { |
| 83 | const title = cleanTitle(document.title); |
| 84 | if (title) { |
| 85 | recordRecentPage({ |
| 86 | path, |
| 87 | title, |
| 88 | type: typeForPathname(router.pathname), |
| 89 | }); |
| 90 | } |
| 91 | }, 250); |
| 92 | }; |
| 93 | |
| 94 | capture(router.asPath); |
| 95 | router.events.on('routeChangeComplete', capture); |
| 96 | return () => { |
| 97 | window.clearTimeout(timeoutId); |
| 98 | router.events.off('routeChangeComplete', capture); |
| 99 | }; |
| 100 | }, [enabled, router]); |
| 101 | }; |
no test coverage detected