|
1 | 1 | // `prettier` does not yet support `import type` |
2 | 2 |
|
3 | 3 | import type React from "react"; |
4 | | -import {useCallback, useEffect, useState} from "react"; |
| 4 | +import {useCallback, useEffect, useMemo, useRef, useSyncExternalStore} from "react"; |
5 | 5 |
|
6 | 6 | type URLState = [URL, (value: React.SetStateAction<URL>, noHistoryEntry?: boolean) => void]; |
7 | 7 |
|
| 8 | +interface PendingUrlUpdate { |
| 9 | + url: URL; |
| 10 | + shouldPush: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +// `info` is not persisted in history; it only identifies the navigate event |
| 14 | +// synchronously generated by this hook. |
| 15 | +const browserUrlNavigationInfo = {}; |
| 16 | + |
| 17 | +function getBrowserUrl(): string { |
| 18 | + return window.navigation.currentEntry?.url ?? window.location.href; |
| 19 | +} |
| 20 | + |
| 21 | +function subscribeToBrowserUrl(onStoreChange: () => void): () => void { |
| 22 | + window.navigation.addEventListener("currententrychange", onStoreChange); |
| 23 | + return () => window.navigation.removeEventListener("currententrychange", onStoreChange); |
| 24 | +} |
| 25 | + |
8 | 26 | export function useBrowserUrl(): URLState { |
9 | | - const [url, setUrlInternal] = useState<URL>(() => new URL(window.location.toString())); |
| 27 | + const href = useSyncExternalStore(subscribeToBrowserUrl, getBrowserUrl); |
| 28 | + const url = useMemo(() => new URL(href), [href]); |
| 29 | + const pendingUrlUpdate = useRef<PendingUrlUpdate | undefined>(undefined); |
| 30 | + |
10 | 31 | useEffect(() => { |
11 | | - const listener = (_e: PopStateEvent) => { |
12 | | - setUrlInternal(new URL(window.location.toString())); |
| 32 | + const listener = (event: NavigateEvent) => { |
| 33 | + if (event.canIntercept && event.info === browserUrlNavigationInfo) { |
| 34 | + // These navigations only update URL-backed state. Preserve the user's |
| 35 | + // current focus and scroll position while making them same-document. |
| 36 | + event.intercept({ |
| 37 | + focusReset: "manual", |
| 38 | + scroll: "manual", |
| 39 | + }); |
| 40 | + } |
13 | 41 | }; |
14 | | - window.addEventListener("popstate", listener); |
15 | | - return () => window.removeEventListener("popstate", listener); |
16 | | - }, [setUrlInternal]); |
17 | | - const setUrl = useCallback( |
18 | | - (action: React.SetStateAction<URL>, noHistoryEntry?: boolean) => { |
19 | | - let newUrl: URL; |
20 | | - if (action instanceof Function) newUrl = action(url); |
21 | | - else newUrl = action; |
22 | | - setUrlInternal(newUrl); |
23 | | - // Don't change the URL if the parameter didn't change. |
24 | | - // This is important to ensure the history stays intact. |
25 | | - if (newUrl.toString() != new URL(window.location.toString()).toString()) { |
26 | | - if (noHistoryEntry) { |
27 | | - window.history.replaceState(null, "", newUrl); |
28 | | - } else { |
29 | | - window.history.pushState(null, "", newUrl); |
| 42 | + window.navigation.addEventListener("navigate", listener); |
| 43 | + return () => window.navigation.removeEventListener("navigate", listener); |
| 44 | + }, []); |
| 45 | + const setUrl = useCallback((action: React.SetStateAction<URL>, noHistoryEntry?: boolean) => { |
| 46 | + let pending = pendingUrlUpdate.current; |
| 47 | + if (!pending) { |
| 48 | + pending = { |
| 49 | + url: new URL(getBrowserUrl()), |
| 50 | + shouldPush: false, |
| 51 | + }; |
| 52 | + pendingUrlUpdate.current = pending; |
| 53 | + queueMicrotask(() => { |
| 54 | + const update = pendingUrlUpdate.current; |
| 55 | + pendingUrlUpdate.current = undefined; |
| 56 | + if (update && update.url.href !== getBrowserUrl()) { |
| 57 | + window.navigation.navigate(update.url.toString(), { |
| 58 | + history: update.shouldPush ? "push" : "replace", |
| 59 | + info: browserUrlNavigationInfo, |
| 60 | + }); |
30 | 61 | } |
31 | | - } |
32 | | - }, |
33 | | - [url, setUrlInternal], |
34 | | - ); |
| 62 | + }); |
| 63 | + } |
| 64 | + pending.url = typeof action === "function" ? action(pending.url) : action; |
| 65 | + pending.shouldPush ||= !noHistoryEntry; |
| 66 | + }, []); |
35 | 67 | return [url, setUrl]; |
36 | 68 | } |
37 | 69 |
|
|
0 commit comments