Unverified Commit a4668f29 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add handling for back/forward (popstate) between old and new router (#38453)

Handles the case where you navigate between routes in `pages` and `app`.



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent bb0013f5
...@@ -166,11 +166,16 @@ export default function AppRouter({ ...@@ -166,11 +166,16 @@ export default function AppRouter({
return return
} }
// Identifier is shortened intentionally.
// __NA is used to identify if the history entry can be handled by the app-router.
// __N is used to identify if the history entry can be handled by the old router.
const historyState = { __NA: true, tree }
if (pushRef.pendingPush) { if (pushRef.pendingPush) {
pushRef.pendingPush = false pushRef.pendingPush = false
window.history.pushState({ tree }, '', canonicalUrl)
window.history.pushState(historyState, '', canonicalUrl)
} else { } else {
window.history.replaceState({ tree }, '', canonicalUrl) window.history.replaceState(historyState, '', canonicalUrl)
} }
}, [tree, pushRef, canonicalUrl]) }, [tree, pushRef, canonicalUrl])
...@@ -185,6 +190,13 @@ export default function AppRouter({ ...@@ -185,6 +190,13 @@ export default function AppRouter({
return return
} }
// TODO: this case happens when pushState/replaceState was called outside of Next.js or when the history entry was pushed by the old router.
// It reloads the page in this case but we might have to revisit this as the old router ignores it.
if (!state.__NA) {
window.location.reload()
return
}
// @ts-ignore useTransition exists // @ts-ignore useTransition exists
// TODO: Ideally the back button should not use startTransition as it should apply the updates synchronously // TODO: Ideally the back button should not use startTransition as it should apply the updates synchronously
// Without startTransition works if the cache is there for this path // Without startTransition works if the cache is there for this path
...@@ -193,7 +205,7 @@ export default function AppRouter({ ...@@ -193,7 +205,7 @@ export default function AppRouter({
type: 'restore', type: 'restore',
payload: { payload: {
url: new URL(window.location.href), url: new URL(window.location.href),
historyState: state, tree: state.tree,
}, },
}) })
}) })
......
...@@ -247,8 +247,6 @@ type AppRouterState = { ...@@ -247,8 +247,6 @@ type AppRouterState = {
canonicalUrl: string canonicalUrl: string
} }
type HistoryState = { tree: FlightRouterState }
export function reducer( export function reducer(
state: AppRouterState, state: AppRouterState,
action: action:
...@@ -265,7 +263,7 @@ export function reducer( ...@@ -265,7 +263,7 @@ export function reducer(
} }
} }
} }
| { type: 'restore'; payload: { url: URL; historyState: HistoryState } } | { type: 'restore'; payload: { url: URL; tree: FlightRouterState } }
| { | {
type: 'server-patch' type: 'server-patch'
payload: { payload: {
...@@ -276,14 +274,14 @@ export function reducer( ...@@ -276,14 +274,14 @@ export function reducer(
} }
): AppRouterState { ): AppRouterState {
if (action.type === 'restore') { if (action.type === 'restore') {
const { url, historyState } = action.payload const { url, tree } = action.payload
const href = url.pathname + url.search + url.hash const href = url.pathname + url.search + url.hash
return { return {
canonicalUrl: href, canonicalUrl: href,
pushRef: state.pushRef, pushRef: state.pushRef,
cache: state.cache, cache: state.cache,
tree: historyState.tree, tree: tree,
} }
} }
......
...@@ -73,8 +73,9 @@ interface NextHistoryState { ...@@ -73,8 +73,9 @@ interface NextHistoryState {
export type HistoryState = export type HistoryState =
| null | null
| { __N: false } | { __NA: true; __N?: false }
| ({ __N: true; key: string } & NextHistoryState) | { __N: false; __NA?: false }
| ({ __NA?: false; __N: true; key: string } & NextHistoryState)
function buildCancellationError() { function buildCancellationError() {
return Object.assign(new Error('Route Cancelled'), { return Object.assign(new Error('Route Cancelled'), {
...@@ -840,6 +841,12 @@ export default class Router implements BaseRouter { ...@@ -840,6 +841,12 @@ export default class Router implements BaseRouter {
return return
} }
// __NA is used to identify if the history entry can be handled by the app-router.
if (state.__NA) {
window.location.reload()
return
}
if (!state.__N) { if (!state.__N) {
return return
} }
......
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