Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shallow routing scroll #21437

Merged
merged 3 commits into from Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/next/next-server/lib/router/router.ts
Expand Up @@ -1093,13 +1093,16 @@ 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 || (options.scroll ? { x: 0, y: 0 } : null)
forcedScroll ||
(isValidShallowRoute || !options.scroll ? null : { x: 0, y: 0 })
).catch((e) => {
if (e.cancelled) error = error || e
else throw e
Expand Down
Expand Up @@ -35,7 +35,9 @@ export default class extends Component {
Home
</a>
</Link>
<div id="counter">Counter: {this.getCurrentCounter()}</div>
<div id="counter" style={{ marginBottom: 4000 }}>
Counter: {this.getCurrentCounter()}
</div>
<div id="get-initial-props-run-count">
getInitialProps run count: {this.props.getInitialPropsRunCount}
</div>
Expand Down
15 changes: 15 additions & 0 deletions test/integration/client-navigation/test/index.test.js
Expand Up @@ -854,6 +854,21 @@ describe('Client Navigation', () => {

await browser.close()
})

it('should keep the scroll position on shallow routing', async () => {
const browser = await webdriver(context.appPort, '/nav/shallow-routing')
await browser.eval(() =>
document.querySelector('#increase').scrollIntoView()
)
const scrollPosition = await browser.eval('window.pageYOffset')

const newScrollPosition = await browser
.elementByCss('#increase')
.click()
.eval('window.pageYOffset')

expect(newScrollPosition).toBe(scrollPosition)
})
})

describe('with URL objects', () => {
Expand Down