fix: link doesn't scroll to section anchor again (#88986)#89459
fix: link doesn't scroll to section anchor again (#88986)#89459kairosci wants to merge 4 commits intovercel:canaryfrom
Conversation
lukesandberg
left a comment
There was a problem hiding this comment.
There appear to be two different changes commingled in this PR
the changes to the link logic and an improvement to the no-html-link-for-pages lint rule, it isn't immediately obvious how these are related.... can you update the PR description to address (or maybe split the lint change into a new PR)
7209ed6 to
9311fc0
Compare
|
Allow CI Workflow Run
Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer |
9311fc0 to
fefb4a9
Compare
Fixes an issue where clicking a link with a hash fragment would not scroll to the target element if clicked multiple times. The problem was that after the first scroll, focusAndScrollRef.apply was set to false and never reset for subsequent clicks to the same hash. This fix ensures that when a hash fragment is present in the navigation, the scroll behavior is always applied, allowing users to click the same hash link multiple times and scroll each time. This handles both cases: - Clicking different hash links on the same page - Clicking the same hash link multiple times (e.g., after manual scroll) Test: test/e2e/app-dir/navigation (hash-link-back-to-same-page)
fefb4a9 to
cad25d2
Compare
|
Split the changes (which shouldn't have been there in the first place), updated the codebase, readjusted the fix, added the test. Everything should be OK now, @lukesandberg PTAL. |
lukesandberg
left a comment
There was a problem hiding this comment.
I patched your PR and observed that the test you added passes without the change to natigation.ts
this matches the observation posted in #88986 (comment)
This appears to have been fixed by #88046 still the test case is useful and i would be happy to accept it
| : 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 !== '') |
There was a problem hiding this comment.
shouldScroll is always true at this point
i think this is equivalent to
shouldScroll
? segmentPathsToScrollTo !== null || onlyHashChange || url.hash !== ''
: oldState.focusAndScrollRef.apply
There was a problem hiding this comment.
Looking back, it could be a consideration.
yes, my initial goal was to force scroll to true
|
So, can I delete fix and remain only the test? |
Description
Fixes an issue where clicking a link with a hash fragment would not scroll to the target element if clicked multiple times.
Problem
When a user clicks a link with a hash (e.g.,
<Link href="#section" />) and then scrolls manually away from the target, clicking the same link again would not scroll back to the section. This is becausefocusAndScrollRef.applywas set tofalseafter the first scroll and was never reset for subsequent navigations to the same hash.Solution
Modified
packages/next/src/client/components/segment-cache/navigation.tsin thecompleteSoftNavigationfunction to ensure that when a hash fragment is present in the navigation, the scroll behavior is always applied.Note: The original fix targeted
handle-mutable.ts, but that file was removed in upstream refactor #88046 (commit 8fe154f). The logic was consolidated into the new navigation system. This PR applies the same fix to the current codebase at the correct location.The fix ensures that when navigating to a URL with a hash fragment (
url.hash !== ''), thefocusAndScrollRef.applyflag is set totrueeven if other route segments haven't changed. This handles both cases:Changed File
packages/next/src/client/components/segment-cache/navigation.ts- Updated theapplylogic infocusAndScrollRefto check for hash fragmentsTesting
test/e2e/app-dir/navigation/navigation.test.ts(hash-link-back-to-same-page)Related
Fixes #88986