Unverified Commit 5309c30c authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

make router restore action resilient to a missing tree (#62098)

### What
Following an anchor link to a hash param, and then attempting to use
`history.pushState` or `history.replaceState`, would result in an MPA
navigation to the targeted URL.

### Why
In #61822, a guard was added to prevent calling `ACTION_RESTORE` with a
missing tree, to match other call-sites where we do the same. This was
to prevent the app from crashing in the case where app router internals
weren't available in the history state. The original assumption was that
this is a rare / unlikely edge case. However the above scenario is a
very probable case where this can happen, and triggering an MPA
navigation isn't ideal.

### How
This updates `ACTION_RESTORE` to be resilient to an undefined router
state tree. When this happens, we'll still trigger the restore action to
sync params, but use the existing flight router state.

Closes NEXT-2502
parent 079fe543
...@@ -511,19 +511,14 @@ function Router({ ...@@ -511,19 +511,14 @@ function Router({
url: string | URL | null | undefined url: string | URL | null | undefined
) => { ) => {
const href = window.location.href const href = window.location.href
const urlToRestore = new URL(url ?? href, href) const tree: FlightRouterState | undefined =
if (!window.history.state?.__PRIVATE_NEXTJS_INTERNALS_TREE) { window.history.state?.__PRIVATE_NEXTJS_INTERNALS_TREE
// we cannot safely recover from a missing tree -- we need trigger an MPA navigation
// to restore the router history to the correct state.
window.location.href = urlToRestore.pathname
return
}
startTransition(() => { startTransition(() => {
dispatch({ dispatch({
type: ACTION_RESTORE, type: ACTION_RESTORE,
url: urlToRestore, url: new URL(url ?? href, href),
tree: window.history.state.__PRIVATE_NEXTJS_INTERNALS_TREE, tree,
}) })
}) })
} }
...@@ -542,6 +537,7 @@ function Router({ ...@@ -542,6 +537,7 @@ function Router({
if (data?.__NA || data?._N) { if (data?.__NA || data?._N) {
return originalPushState(data, _unused, url) return originalPushState(data, _unused, url)
} }
data = copyNextJsInternalHistoryState(data) data = copyNextJsInternalHistoryState(data)
if (url) { if (url) {
......
...@@ -13,6 +13,13 @@ export function restoreReducer( ...@@ -13,6 +13,13 @@ export function restoreReducer(
): ReducerState { ): ReducerState {
const { url, tree } = action const { url, tree } = action
const href = createHrefFromUrl(url) const href = createHrefFromUrl(url)
// This action is used to restore the router state from the history state.
// However, it's possible that the history state no longer contains the `FlightRouterState`.
// We will copy over the internal state on pushState/replaceState events, but if a history entry
// occurred before hydration, or if the user navigated to a hash using a regular anchor link,
// the history state will not contain the `FlightRouterState`.
// In this case, we'll continue to use the existing tree so the router doesn't get into an invalid state.
const treeToRestore = tree || state.tree
const oldCache = state.cache const oldCache = state.cache
const newCache = process.env.__NEXT_PPR const newCache = process.env.__NEXT_PPR
...@@ -20,7 +27,7 @@ export function restoreReducer( ...@@ -20,7 +27,7 @@ export function restoreReducer(
// data for any segment whose dynamic data was already received. This // data for any segment whose dynamic data was already received. This
// prevents an unnecessary flash back to PPR state during a // prevents an unnecessary flash back to PPR state during a
// back/forward navigation. // back/forward navigation.
updateCacheNodeOnPopstateRestoration(oldCache, tree) updateCacheNodeOnPopstateRestoration(oldCache, treeToRestore)
: oldCache : oldCache
return { return {
...@@ -37,7 +44,7 @@ export function restoreReducer( ...@@ -37,7 +44,7 @@ export function restoreReducer(
cache: newCache, cache: newCache,
prefetchCache: state.prefetchCache, prefetchCache: state.prefetchCache,
// Restore provided tree // Restore provided tree
tree: tree, tree: treeToRestore,
nextUrl: extractPathFromFlightRouterState(tree) ?? url.pathname, nextUrl: extractPathFromFlightRouterState(treeToRestore) ?? url.pathname,
} }
} }
...@@ -114,15 +114,17 @@ export interface NavigateAction { ...@@ -114,15 +114,17 @@ export interface NavigateAction {
/** /**
* Restore applies the provided router state. * Restore applies the provided router state.
* - Only used for `popstate` (back/forward navigation) where a known router state has to be applied. * - Used for `popstate` (back/forward navigation) where a known router state has to be applied.
* - Router state is applied as-is from the history state. * - Also used when syncing the router state with `pushState`/`replaceState` calls.
* - Router state is applied as-is from the history state, if available.
* - If the history state does not contain the router state, the existing router state is used.
* - If any cache node is missing it will be fetched in layout-router during rendering and the server-patch case. * - If any cache node is missing it will be fetched in layout-router during rendering and the server-patch case.
* - If existing cache nodes match these are used. * - If existing cache nodes match these are used.
*/ */
export interface RestoreAction { export interface RestoreAction {
type: typeof ACTION_RESTORE type: typeof ACTION_RESTORE
url: URL url: URL
tree: FlightRouterState tree: FlightRouterState | undefined
} }
/** /**
......
...@@ -5,6 +5,11 @@ export default function ShallowLayout({ children }) { ...@@ -5,6 +5,11 @@ export default function ShallowLayout({ children }) {
<> <>
<h1>Shallow Routing</h1> <h1>Shallow Routing</h1>
<div> <div>
<div>
<a href="#content" id="hash-navigation">
Hash Navigation (non-Link)
</a>
</div>
<div> <div>
<Link href="/a" id="to-a"> <Link href="/a" id="to-a">
To A To A
...@@ -85,7 +90,7 @@ export default function ShallowLayout({ children }) { ...@@ -85,7 +90,7 @@ export default function ShallowLayout({ children }) {
</Link> </Link>
</div> </div>
</div> </div>
{children} <div id="content">{children}</div>
</> </>
) )
} }
...@@ -447,6 +447,48 @@ createNextDescribe( ...@@ -447,6 +447,48 @@ createNextDescribe(
'Page A' 'Page A'
) )
}) })
it('should support hash navigations while continuing to work for pushState/replaceState APIs', async () => {
const browser = await next.browser('/a')
expect(
await browser
.elementByCss('#to-pushstate-string-url')
.click()
.waitForElementByCss('#pushstate-string-url')
.text()
).toBe('PushState String Url')
await browser.elementByCss('#hash-navigation').click()
// Check current url contains the hash
expect(await browser.url()).toBe(
`${next.url}/pushstate-string-url#content`
)
await browser.elementByCss('#push-string-url').click()
// Check useSearchParams value is the new searchparam
await check(() => browser.elementByCss('#my-data').text(), 'foo')
// Check current url is the new searchparams
expect(await browser.url()).toBe(
`${next.url}/pushstate-string-url?query=foo`
)
// Same cycle a second time
await browser.elementByCss('#push-string-url').click()
// Check useSearchParams value is the new searchparam
await check(
() => browser.elementByCss('#my-data').text(),
'foo-added'
)
// Check current url is the new searchparams
expect(await browser.url()).toBe(
`${next.url}/pushstate-string-url?query=foo-added`
)
})
}) })
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment