Skip to content

fix: smooth scroll to deep link#14569

Merged
Rich-Harris merged 3 commits intomainfrom
smooth-scroll-to-deep-link
Sep 30, 2025
Merged

fix: smooth scroll to deep link#14569
Rich-Harris merged 3 commits intomainfrom
smooth-scroll-to-deep-link

Conversation

@Rich-Harris
Copy link
Member

This addresses a very finicky and hard-to-reproduce (and test for, unfortunately) bug: when navigating to a hash fragment on another page, the captured scroll position is wrong if the page has scroll-behavior: smooth, because it gets captured at the start of the scroll rather than the end.

If my understanding is correct, the buggy sequence of events is:

  • we navigate, and update the DOM
  • we scroll to the desired place (either a previous scroll position, if navigating via popstate, or the position of a deep-linked element, or 0, 0
  • we reset focus:
    • we capture the scroll position (incorrectly in the smooth case)
    • reset everything
    • scroll back to the desired scroll position

This PR changes the behaviour slightly: where appropriate, it passes the desired scroll state to reset_focus so that we end up in the correct place.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot
Copy link

changeset-bot bot commented Sep 30, 2025

🦋 Changeset detected

Latest commit: ead1261

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunate there's no way to test this...

Removed console log for scroll position.
Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

logic looks good, but we should have a comment for this

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
@Rich-Harris Rich-Harris merged commit dbf9d12 into main Sep 30, 2025
38 of 39 checks passed
@Rich-Harris Rich-Harris deleted the smooth-scroll-to-deep-link branch September 30, 2025 22:01
@github-actions github-actions bot mentioned this pull request Sep 30, 2025
Copilot AI pushed a commit to Stadly/kit that referenced this pull request Mar 6, 2026
* fix: smooth scroll to deep link

* remove log

Removed console log for scroll position.

* Update packages/kit/src/runtime/client/client.js

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>

---------

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants