diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts
index a783d6b1c45a5..4a30241ffc67a 100644
--- a/packages/next/next-server/lib/router/router.ts
+++ b/packages/next/next-server/lib/router/router.ts
@@ -757,7 +757,7 @@ export default class Router implements BaseRouter {
// Default to scroll reset behavior unless explicitly specified to be
// `false`! This makes the behavior between using `Router#push` and a
// `` consistent.
- options.scroll = !!(options.scroll ?? true)
+ options.scroll = !!(options.scroll ?? (options.shallow ? false : true))
let localeChange = options.locale !== this.locale
@@ -1093,16 +1093,13 @@ export default class Router implements BaseRouter {
!(routeInfo.Component as any).getInitialProps
}
- // shallow routing is only allowed for same page URL changes.
- const isValidShallowRoute = options.shallow && this.route === route
await this.set(
route,
pathname!,
query,
cleanedAs,
routeInfo,
- forcedScroll ||
- (isValidShallowRoute || !options.scroll ? null : { x: 0, y: 0 })
+ forcedScroll || (options.scroll ? { x: 0, y: 0 } : null)
).catch((e) => {
if (e.cancelled) error = error || e
else throw e
@@ -1327,7 +1324,6 @@ export default class Router implements BaseRouter {
resetScroll: { x: number; y: number } | null
): Promise {
this.isFallback = false
-
this.route = route
this.pathname = pathname
this.query = query
diff --git a/test/integration/client-navigation/pages/nav/shallow-routing.js b/test/integration/client-navigation/pages/nav/shallow-routing.js
index 728467f9afde4..d83548061badc 100644
--- a/test/integration/client-navigation/pages/nav/shallow-routing.js
+++ b/test/integration/client-navigation/pages/nav/shallow-routing.js
@@ -27,6 +27,12 @@ export default class extends Component {
Router.push(href, href, { shallow: true })
}
+ increaseWithScroll() {
+ const counter = this.getCurrentCounter()
+ const href = `/nav/shallow-routing?counter=${counter + 1}`
+ Router.push(href, href, { shallow: true, scroll: true })
+ }
+
increaseNonShallow() {
const counter = this.getCurrentCounter()
const href = `/nav/shallow-routing?counter=${counter + 1}`
@@ -58,6 +64,9 @@ export default class extends Component {
+
diff --git a/test/integration/client-navigation/test/index.test.js b/test/integration/client-navigation/test/index.test.js
index 3e73719dd690d..cc73646bcaa0e 100644
--- a/test/integration/client-navigation/test/index.test.js
+++ b/test/integration/client-navigation/test/index.test.js
@@ -899,11 +899,24 @@ describe('Client Navigation', () => {
expect(scrollPositionDown).toBeGreaterThan(3000)
- await browser.elementByCss('#invalidShallow').click()
+ await browser.elementByCss('#increase3').click()
await waitFor(500)
const newScrollPosition3 = await browser.eval('window.pageYOffset')
expect(newScrollPosition3).toBe(0)
+
+ await browser.eval(() =>
+ document.querySelector('#increase3').scrollIntoView()
+ )
+ const scrollPositionDown2 = await browser.eval('window.pageYOffset')
+
+ expect(scrollPositionDown2).toBeGreaterThan(3000)
+
+ await browser.elementByCss('#invalidShallow').click()
+ await waitFor(500)
+ const newScrollPosition4 = await browser.eval('window.pageYOffset')
+
+ expect(newScrollPosition4).toBe(0)
})
})