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
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,11 @@ export function completeSoftNavigation(
apply: shouldScroll
? segmentPathsToScrollTo !== null
? true
: oldState.focusAndScrollRef.apply
: // Also apply scroll if we have a hash fragment, even if the hash didn't change.
// This allows clicking the same hash link multiple times to scroll each time.
onlyHashChange || (shouldScroll && url.hash !== '')
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldScroll is always true at this point

i think this is equivalent to

 shouldScroll
  ? segmentPathsToScrollTo !== null || onlyHashChange || url.hash !== ''
  : oldState.focusAndScrollRef.apply

Copy link
Author

Choose a reason for hiding this comment

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

Looking back, it could be a consideration.

yes, my initial goal was to force scroll to true

? true
: oldState.focusAndScrollRef.apply
: oldState.focusAndScrollRef.apply,
onlyHashChange,
hashFragment:
Expand Down
39 changes: 39 additions & 0 deletions test/e2e/app-dir/navigation/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,45 @@ describe('app dir - navigation', () => {
expect(browser.eval('window.pageYOffset')).resolves.toEqual(0)
)
})

it('should scroll to the same hash when clicked multiple times', async () => {
// Test for issue #88986: clicking the same hash link multiple times should scroll each time
const browser = await next.browser('/hash-link-back-to-same-page')

// First click to hash-6
await browser.elementByCss('#link-to-6').click()
await retry(() =>
expect(browser.eval('window.pageYOffset')).resolves.toEqual(114)
)

// Manually scroll down to simulate user scrolling away
await browser.eval('window.scrollBy(0, 500)')
let scrollPos = await browser.eval('window.pageYOffset')
expect(scrollPos).toBeGreaterThan(114)

// Click the same hash link again - should scroll back to hash-6
await browser.elementByCss('#link-to-6').click()
await retry(() =>
expect(browser.eval('window.pageYOffset')).resolves.toEqual(114)
)

// Click hash-50
await browser.elementByCss('#link-to-50').click()
await retry(() =>
expect(browser.eval('window.pageYOffset')).resolves.toEqual(730)
)

// Manually scroll down again
await browser.eval('window.scrollBy(0, 500)')
scrollPos = await browser.eval('window.pageYOffset')
expect(scrollPos).toBeGreaterThan(730)

// Click the same hash link again - should scroll back to hash-50
await browser.elementByCss('#link-to-50').click()
await retry(() =>
expect(browser.eval('window.pageYOffset')).resolves.toEqual(730)
)
})
})

describe('relative hashes and queries', () => {
Expand Down
Loading