Skip to content

Commit

Permalink
Add test case and apply fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Feb 6, 2022
1 parent 7554cad commit 0d574a3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
5 changes: 0 additions & 5 deletions packages/next/client/link.tsx
Expand Up @@ -96,11 +96,6 @@ function linkClicked(

e.preventDefault()

// avoid scroll for urls with anchor refs
if (scroll == null && as.indexOf('#') >= 0) {
scroll = false
}

// replace state instead of push if prop is present
router[replace ? 'replace' : 'push'](href, as, {
shallow,
Expand Down
5 changes: 4 additions & 1 deletion packages/next/shared/lib/router/router.ts
Expand Up @@ -1045,7 +1045,10 @@ export default class Router implements BaseRouter {
this.asPath = cleanedAs
Router.events.emit('hashChangeStart', as, routeProps)
// TODO: do we need the resolved href when only a hash change?
this.changeState(method, url, as, options)
this.changeState(method, url, as, {
...options,
scroll: false,
})
if (scroll) {
this.scrollToHash(cleanedAs)
}
Expand Down
5 changes: 5 additions & 0 deletions test/integration/client-navigation/pages/nav/hash-changes.js
Expand Up @@ -27,6 +27,11 @@ const HashChanges = ({ count }) => {
<Link href="#name-item-400">
<a id="scroll-to-name-item-400">Go to name item 400</a>
</Link>
<Link href="#name-item-400" scroll={false}>
<a id="scroll-to-name-item-400-no-scroll">
Go to name item 400 (no scroll)
</a>
</Link>
<p>COUNT: {count}</p>
{Array.from({ length: 500 }, (x, i) => i + 1).map((i) => {
return (
Expand Down
11 changes: 11 additions & 0 deletions test/integration/client-navigation/test/index.test.js
Expand Up @@ -618,6 +618,17 @@ describe('Client Navigation', () => {
}
})

it('should not scroll to hash when scroll={false} is set', async () => {
const browser = await webdriver(context.appPort, '/nav/hash-changes')
const curScroll = await browser.eval(
'document.documentElement.scrollTop'
)
await browser.elementByCss('#scroll-to-name-item-400-no-scroll').click()
expect(curScroll).toBe(
await browser.eval('document.documentElement.scrollTop')
)
})

it('should scroll to the specified position on the same page with a name property', async () => {
let browser
try {
Expand Down

0 comments on commit 0d574a3

Please sign in to comment.