Unverified Commit d820542a authored by Chris Neven's avatar Chris Neven Committed by GitHub

fix(21606): consider scroll option when using shallow routing (#24888)


## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added

fixes [#21606](https://github.com/vercel/next.js/issues/21606)

### Description
When using shallow routing and wanting to scroll to top by setting the `scroll` option to `true` it didn't work. This PR fixes this issue.
parent 50725188
...@@ -94,8 +94,8 @@ function linkClicked( ...@@ -94,8 +94,8 @@ function linkClicked(
e.preventDefault() e.preventDefault()
// avoid scroll for urls with anchor refs // avoid scroll for urls with anchor refs
if (scroll == null) { if (scroll == null && as.indexOf('#') >= 0) {
scroll = as.indexOf('#') < 0 scroll = false
} }
// replace state instead of push if prop is present // replace state instead of push if prop is present
......
...@@ -816,11 +816,6 @@ export default class Router implements BaseRouter { ...@@ -816,11 +816,6 @@ export default class Router implements BaseRouter {
this.isReady = true this.isReady = true
} }
// Default to scroll reset behavior unless explicitly specified to be
// `false`! This makes the behavior between using `Router#push` and a
// `<Link />` consistent.
options.scroll = !!(options.scroll ?? true)
let localeChange = options.locale !== this.locale let localeChange = options.locale !== this.locale
if (process.env.__NEXT_I18N_SUPPORT) { if (process.env.__NEXT_I18N_SUPPORT) {
...@@ -1165,9 +1160,6 @@ export default class Router implements BaseRouter { ...@@ -1165,9 +1160,6 @@ export default class Router implements BaseRouter {
!(routeInfo.Component as any).getInitialProps !(routeInfo.Component as any).getInitialProps
} }
// shallow routing is only allowed for same page URL changes.
const isValidShallowRoute = options.shallow && this.route === route
if ( if (
(options as any)._h && (options as any)._h &&
pathname === '/_error' && pathname === '/_error' &&
...@@ -1179,14 +1171,18 @@ export default class Router implements BaseRouter { ...@@ -1179,14 +1171,18 @@ export default class Router implements BaseRouter {
props.pageProps.statusCode = 500 props.pageProps.statusCode = 500
} }
// shallow routing is only allowed for same page URL changes.
const isValidShallowRoute = options.shallow && this.route === route
const shouldScroll = options.scroll ?? !isValidShallowRoute
const resetScroll = shouldScroll ? { x: 0, y: 0 } : null
await this.set( await this.set(
route, route,
pathname!, pathname!,
query, query,
cleanedAs, cleanedAs,
routeInfo, routeInfo,
forcedScroll || forcedScroll ?? resetScroll
(isValidShallowRoute || !options.scroll ? null : { x: 0, y: 0 })
).catch((e) => { ).catch((e) => {
if (e.cancelled) error = error || e if (e.cancelled) error = error || e
else throw e else throw e
......
...@@ -22,10 +22,10 @@ export default withRouter( ...@@ -22,10 +22,10 @@ export default withRouter(
return router.query.counter ? parseInt(router.query.counter) : 0 return router.query.counter ? parseInt(router.query.counter) : 0
} }
increase() { increase(scroll) {
const counter = this.getCurrentCounter() const counter = this.getCurrentCounter()
const href = `/nav/shallow-routing?counter=${counter + 1}` const href = `/nav/shallow-routing?counter=${counter + 1}`
Router.push(href, href, { shallow: true }) Router.push(href, href, { shallow: true, scroll })
} }
increaseNonShallow() { increaseNonShallow() {
...@@ -56,6 +56,9 @@ export default withRouter( ...@@ -56,6 +56,9 @@ export default withRouter(
<button id="increase" onClick={() => this.increase()}> <button id="increase" onClick={() => this.increase()}>
Increase Increase
</button> </button>
<button id="increaseWithScroll" onClick={() => this.increase(true)}>
Increase with scroll
</button>
<button id="increase2" onClick={() => this.increaseNonShallow()}> <button id="increase2" onClick={() => this.increaseNonShallow()}>
Increase Non-Shallow Increase Non-Shallow
</button> </button>
......
...@@ -881,6 +881,22 @@ describe('Client Navigation', () => { ...@@ -881,6 +881,22 @@ describe('Client Navigation', () => {
}) })
}) })
it('should scroll to top when the scroll option is set to true', async () => {
const browser = await webdriver(context.appPort, '/nav/shallow-routing')
await browser.eval(() =>
document.querySelector('#increaseWithScroll').scrollIntoView()
)
const scrollPosition = await browser.eval('window.pageYOffset')
expect(scrollPosition).toBeGreaterThan(3000)
await browser.elementByCss('#increaseWithScroll').click()
await check(async () => {
const newScrollPosition = await browser.eval('window.pageYOffset')
return newScrollPosition === 0 ? 'success' : 'fail'
}, 'success')
})
describe('with URL objects', () => { describe('with URL objects', () => {
it('should work with <Link/>', async () => { it('should work with <Link/>', async () => {
const browser = await webdriver(context.appPort, '/nav') const browser = await webdriver(context.appPort, '/nav')
......
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