Skip to content

fix: correct hash routing behavior #13492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2025

Conversation

stephenlrandall
Copy link
Contributor

closes #13460 and #13322

As is, Kit doesn't correctly handle some aspects of hash routing. Spurious navigations are triggered when using the back button in the browser, but only for certain kinds of routes and in certain environments (see #13460 for details). This seems to be a consequence of differences in when current.url is modified, which makes the existing check for user edits to the URL error-prone. Instead (and to also fix #13322), the popstate event is used and triggers a simple page reload.


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.

Copy link

changeset-bot bot commented Feb 23, 2025

🦋 Changeset detected

Latest commit: 945805a

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

@svelte-docs-bot
Copy link

@stephenlrandall
Copy link
Contributor Author

@eltigerchino Sorry, I needed to recreate this since the original was on the main branch of my fork, so I couldn't open any other PRs.

@dummdidumm
Copy link
Member

You say it also closes #13460, which makes me nervous. I don't want us to reload everything when the user navigates via a back button.

@stephenlrandall
Copy link
Contributor Author

You say it also closes #13460, which makes me nervous. I don't want us to reload everything when the user navigates via a back button.

It doesn't, that code is only triggered -- as far as I can tell -- for non-navigation popstate events.

@stephenlrandall
Copy link
Contributor Author

@dummdidumm To clarify, the issue in #13460 is the deleted code. It does detect user changes to the URL, but it also triggers incorrectly on certain navigations in the browser. So deleting that code fixes #13460 but leaves the app unresponsive to user changes to the hash. To address that (while also resolving #13322), I added the reload for non-navigation popstate events that are triggered when a user edits the URL.

@elliott-with-the-longest-name-on-github elliott-with-the-longest-name-on-github added the needs-decision Not sure if we want to do this yet, also design work needed label Mar 4, 2025
@stephenlrandall
Copy link
Contributor Author

stephenlrandall commented Mar 4, 2025

@elliott-with-the-longest-name-on-github Mildly confused by the tag here -- what decision(s) would need to be made? The current hash router breaks the expected history stack. Deleting the offending code fixes the problem but leaves the app unresponsive to user URL edits. And while you could try to trigger a navigation where I currently have a page reload, you'd run back into #13322 and inconsistency with the pathname router. Is there something I'm overlooking? I'll admit I'm not overly familiar with the codebase. But I'm certainly not triggering page reloads on forward/back-button navigations, at least as far as I can tell.

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.

dug into this some more - the change is sound. Thank you!

@dummdidumm dummdidumm merged commit 3c0df50 into sveltejs:main Mar 4, 2025
15 checks passed
@github-actions github-actions bot mentioned this pull request Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-decision Not sure if we want to do this yet, also design work needed router
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug in hash-based routing Load functions are not always re-run on user changes to URL with hash-based router
4 participants