File tree Expand file tree Collapse file tree 4 files changed +29
-14
lines changed
test/integration/client-navigation Expand file tree Collapse file tree 4 files changed +29
-14
lines changed Original file line number Diff line number Diff line change @@ -94,8 +94,8 @@ function linkClicked(
94
94
e . preventDefault ( )
95
95
96
96
// avoid scroll for urls with anchor refs
97
- if ( scroll == null ) {
98
- scroll = as . indexOf ( '#' ) < 0
97
+ if ( scroll == null && as . indexOf ( '#' ) >= 0 ) {
98
+ scroll = false
99
99
}
100
100
101
101
// replace state instead of push if prop is present
Original file line number Diff line number Diff line change @@ -816,11 +816,6 @@ export default class Router implements BaseRouter {
816
816
this . isReady = true
817
817
}
818
818
819
- // Default to scroll reset behavior unless explicitly specified to be
820
- // `false`! This makes the behavior between using `Router#push` and a
821
- // `<Link />` consistent.
822
- options . scroll = ! ! ( options . scroll ?? true )
823
-
824
819
let localeChange = options . locale !== this . locale
825
820
826
821
if ( process . env . __NEXT_I18N_SUPPORT ) {
@@ -1165,9 +1160,6 @@ export default class Router implements BaseRouter {
1165
1160
! ( routeInfo . Component as any ) . getInitialProps
1166
1161
}
1167
1162
1168
- // shallow routing is only allowed for same page URL changes.
1169
- const isValidShallowRoute = options . shallow && this . route === route
1170
-
1171
1163
if (
1172
1164
( options as any ) . _h &&
1173
1165
pathname === '/_error' &&
@@ -1179,14 +1171,18 @@ export default class Router implements BaseRouter {
1179
1171
props . pageProps . statusCode = 500
1180
1172
}
1181
1173
1174
+ // shallow routing is only allowed for same page URL changes.
1175
+ const isValidShallowRoute = options . shallow && this . route === route
1176
+
1177
+ const shouldScroll = options . scroll ?? ! isValidShallowRoute
1178
+ const resetScroll = shouldScroll ? { x : 0 , y : 0 } : null
1182
1179
await this . set (
1183
1180
route ,
1184
1181
pathname ! ,
1185
1182
query ,
1186
1183
cleanedAs ,
1187
1184
routeInfo ,
1188
- forcedScroll ||
1189
- ( isValidShallowRoute || ! options . scroll ? null : { x : 0 , y : 0 } )
1185
+ forcedScroll ?? resetScroll
1190
1186
) . catch ( ( e ) => {
1191
1187
if ( e . cancelled ) error = error || e
1192
1188
else throw e
Original file line number Diff line number Diff line change @@ -22,10 +22,10 @@ export default withRouter(
22
22
return router . query . counter ? parseInt ( router . query . counter ) : 0
23
23
}
24
24
25
- increase ( ) {
25
+ increase ( scroll ) {
26
26
const counter = this . getCurrentCounter ( )
27
27
const href = `/nav/shallow-routing?counter=${ counter + 1 } `
28
- Router . push ( href , href , { shallow : true } )
28
+ Router . push ( href , href , { shallow : true , scroll } )
29
29
}
30
30
31
31
increaseNonShallow ( ) {
@@ -56,6 +56,9 @@ export default withRouter(
56
56
< button id = "increase" onClick = { ( ) => this . increase ( ) } >
57
57
Increase
58
58
</ button >
59
+ < button id = "increaseWithScroll" onClick = { ( ) => this . increase ( true ) } >
60
+ Increase with scroll
61
+ </ button >
59
62
< button id = "increase2" onClick = { ( ) => this . increaseNonShallow ( ) } >
60
63
Increase Non-Shallow
61
64
</ button >
Original file line number Diff line number Diff line change @@ -881,6 +881,22 @@ describe('Client Navigation', () => {
881
881
} )
882
882
} )
883
883
884
+ it ( 'should scroll to top when the scroll option is set to true' , async ( ) => {
885
+ const browser = await webdriver ( context . appPort , '/nav/shallow-routing' )
886
+ await browser . eval ( ( ) =>
887
+ document . querySelector ( '#increaseWithScroll' ) . scrollIntoView ( )
888
+ )
889
+ const scrollPosition = await browser . eval ( 'window.pageYOffset' )
890
+
891
+ expect ( scrollPosition ) . toBeGreaterThan ( 3000 )
892
+
893
+ await browser . elementByCss ( '#increaseWithScroll' ) . click ( )
894
+ await check ( async ( ) => {
895
+ const newScrollPosition = await browser . eval ( 'window.pageYOffset' )
896
+ return newScrollPosition === 0 ? 'success' : 'fail'
897
+ } , 'success' )
898
+ } )
899
+
884
900
describe ( 'with URL objects' , ( ) => {
885
901
it ( 'should work with <Link/>' , async ( ) => {
886
902
const browser = await webdriver ( context . appPort , '/nav' )
You can’t perform that action at this time.
0 commit comments