File tree 2 files changed +29
-1
lines changed
packages/next/shared/lib/router
test/integration/client-navigation/test
2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -1080,7 +1080,15 @@ export default class Router implements BaseRouter {
1080
1080
if ( scroll ) {
1081
1081
this . scrollToHash ( cleanedAs )
1082
1082
}
1083
- this . set ( nextState , this . components [ nextState . route ] , null )
1083
+ try {
1084
+ await this . set ( nextState , this . components [ nextState . route ] , null )
1085
+ } catch ( err ) {
1086
+ if ( isError ( err ) && err . cancelled ) {
1087
+ Router . events . emit ( 'routeChangeError' , err , cleanedAs , routeProps )
1088
+ }
1089
+ throw err
1090
+ }
1091
+
1084
1092
Router . events . emit ( 'hashChangeComplete' , as , routeProps )
1085
1093
return true
1086
1094
}
Original file line number Diff line number Diff line change @@ -1714,6 +1714,26 @@ describe('Client Navigation', () => {
1714
1714
expect ( value ) . toBe ( false )
1715
1715
} )
1716
1716
1717
+ it ( 'should emit routeChangeError on hash change cancel' , async ( ) => {
1718
+ const browser = await webdriver ( context . appPort , '/' )
1719
+
1720
+ await browser . eval ( `(function() {
1721
+ window.routeErrors = []
1722
+
1723
+ window.next.router.events.on('routeChangeError', function (err) {
1724
+ window.routeErrors.push(err)
1725
+ })
1726
+ window.next.router.push('#first')
1727
+ window.next.router.push('#second')
1728
+ window.next.router.push('#third')
1729
+ })()` )
1730
+
1731
+ await check ( async ( ) => {
1732
+ const errorCount = await browser . eval ( 'window.routeErrors.length' )
1733
+ return errorCount > 0 ? 'success' : errorCount
1734
+ } , 'success' )
1735
+ } )
1736
+
1717
1737
it ( 'should navigate to paths relative to the current page' , async ( ) => {
1718
1738
const browser = await webdriver ( context . appPort , '/nav/relative' )
1719
1739
let page
You can’t perform that action at this time.
0 commit comments