-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gracefully handle client router segment mismatches (#60141)
### What? Next.js throws a hard `SEGMENT MISMATCH` error when the reducers were unable to apply the a patch to the router tree from the server response. ### How? Rather than crashing the router, this will treat segment mismatches as a MPA navigation, to restore the client router into a working state. ### Test Plan If there are specific scenarios where Next.js throws this error, it should most likely be fixed in Next and not in user-land. Since it's not currently obvious what scenarios will trigger this error, this PR serves to recover from a mismatch more gracefully and provides some debug information rather than crashing the application. As such, there's no easy way to create an E2E test for this and I've instead opted for a simple unit test. Closes NEXT-1878 [slack x-ref](https://vercel.slack.com/archives/C017QMYC5FB/p1704214439768469) [slack x-ref](https://vercel.slack.com/archives/C03KAR5DCKC/p1702565978694519) --------- Co-authored-by: JJ Kasper <jj@jjsweb.site>
- Loading branch information
Showing
6 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
packages/next/src/client/components/router-reducer/handle-segment-mismatch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { FlightRouterState } from '../../../server/app-render/types' | ||
import { handleExternalUrl } from './reducers/navigate-reducer' | ||
import type { | ||
ReadonlyReducerState, | ||
ReducerActions, | ||
} from './router-reducer-types' | ||
|
||
/** | ||
* Handles the case where the client router attempted to patch the tree but, due to a mismatch, the patch failed. | ||
* This will perform an MPA navigation to return the router to a valid state. | ||
*/ | ||
export function handleSegmentMismatch( | ||
state: ReadonlyReducerState, | ||
action: ReducerActions, | ||
treePatch: FlightRouterState | ||
) { | ||
if (process.env.NODE_ENV === 'development') { | ||
console.warn( | ||
'Performing hard navigation because your application experienced an unrecoverable error. If this keeps occurring, please file a Next.js issue.\n\n' + | ||
'Reason: Segment mismatch\n' + | ||
`Last Action: ${action.type}\n\n` + | ||
`Current Tree: ${JSON.stringify(state.tree)}\n\n` + | ||
`Tree Patch Payload: ${JSON.stringify(treePatch)}` | ||
) | ||
} | ||
|
||
return handleExternalUrl(state, {}, state.canonicalUrl, true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters