Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next/src/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2332,6 +2332,7 @@ export default class Router implements BaseRouter {
}

urlIsNew(asPath: string): boolean {
if (this.onlyAHashChange(asPath)) return true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (this.onlyAHashChange(asPath)) return true

The urlIsNew method incorrectly returns true when navigating to the same URL with the same hash, causing duplicate browser history entries.

Fix on Vercel

return this.asPath !== asPath
}

Expand Down
15 changes: 15 additions & 0 deletions test/e2e/app-dir/hash-navigation/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Link from 'next/link'

export default function Page() {
return (
<div style={{ paddingTop: '100px' }}>
<Link href="#section" id="link">
Go to section
</Link>

<div style={{ marginTop: '15000px' }} id="section">
<h1 style={{ color: 'black' }}>Section</h1>
</div>
</div>
)
}
28 changes: 28 additions & 0 deletions test/e2e/app-dir/hash-navigation/hash-navigation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { nextTestSetup } from 'e2e-utils'

describe('hash navigation', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
})

if (skipped) return

it('should scroll again when navigating to the same hash', async () => {
const browser = await next.browser('/')

// First click
await browser.elementByCss('#section').click()
const firstScroll = await browser.eval(() => window.scrollY)
expect(firstScroll).toBeGreaterThan(0)

// Scroll back to top
await browser.eval(() => window.scrollTo(0, 0))
const resetScroll = await browser.eval(() => window.scrollY)
expect(resetScroll).toBe(0)

// Second click
await browser.elementByCss('#section').click()
const secondScroll = await browser.eval(() => window.scrollY)
expect(secondScroll).toBeGreaterThan(0)
})
})
Loading