(pathname: string, push = false, popstateState: HistoryState | null = null)
| 63 | // and in a React transition, stream in the new page. Once complete, we'll pushState to |
| 64 | // update the URL in the browser. |
| 65 | async function navigate(pathname: string, push = false, popstateState: HistoryState | null = null) { |
| 66 | let url = new URL(pathname, location.href); |
| 67 | let basePath = url.pathname; |
| 68 | let pathAnchor = url.hash.slice(1); |
| 69 | let currentPath = location.pathname; |
| 70 | let isSamePageAnchor = (!basePath || basePath === currentPath) && pathAnchor; |
| 71 | |
| 72 | // Save scroll position to current history entry before navigating away |
| 73 | if (push) { |
| 74 | saveScrollPosition(); |
| 75 | } |
| 76 | |
| 77 | if (isSamePageAnchor) { |
| 78 | if (push) { |
| 79 | history.pushState(null, '', pathname); |
| 80 | } |
| 81 | |
| 82 | // Scroll to the anchor |
| 83 | let element = document.getElementById(pathAnchor); |
| 84 | if (element) { |
| 85 | element.scrollIntoView(); |
| 86 | } |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | let rscPath = getRSCUrl(pathname); |
| 91 | |
| 92 | // Cancel any in-flight navigation |
| 93 | if (currentAbortController) { |
| 94 | currentAbortController.abort('Aborting due to new navigation'); |
| 95 | } |
| 96 | |
| 97 | // Create a new abort controller for this navigation |
| 98 | const abortController = new AbortController(); |
| 99 | currentAbortController = abortController; |
| 100 | const navigationId = ++currentNavigationId; |
| 101 | |
| 102 | const navigationPromise = (async () => { |
| 103 | window.dispatchEvent(new CustomEvent('rsc-navigation-start')); |
| 104 | |
| 105 | // Use prefetched result if available, otherwise fetch |
| 106 | const prefetchedPromise = getPrefetchedPromise(rscPath); |
| 107 | const fetchPromise = prefetchedPromise ?? fetchRSC<ReactElement>(rscPath); |
| 108 | |
| 109 | try { |
| 110 | let res = await fetchPromise; |
| 111 | |
| 112 | // Check if this navigation is still current before updating |
| 113 | if (navigationId !== currentNavigationId) { |
| 114 | // A newer navigation has started, ignore this result |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | // Check if this navigation was aborted |
| 119 | if (abortController.signal.aborted) { |
| 120 | return; |
| 121 | } |
| 122 |
no test coverage detected