navigating (back) to a route with hash does not work
Describe the bug
When clicking an anchor link on a page (/blog/my-blog-post#content) and subsequently navigating somehwere else (/blog/my-blog-post/detail/my-detail) I cannot navigate back to /blog/my-blog-post#content, but navigation works when the link does not have a hash (e.g. /blog/my-blog-post)
To Reproduce Repository to reproduce: https://github.com/lukasIO/kit-hash-route-bug
- run and go to
localhost:3000/blog/my-blog-postin a new tab - click "anchor link to content"
- click "view detail"
- try to navigate back via the link (or browser)
- no navigation triggered
Expected behavior I expect navigation to work also when targeting hash based routes
Information about your SvelteKit Installation:
- The output of
npx envinfo --system --npmPackages svelte,@sveltejs/kit,vite --binaries --browsers
System:
OS: Linux 5.4 Ubuntu 20.04.2 LTS (Focal Fossa)
CPU: (4) x64 Intel(R) Core(TM) i5-3320M CPU @ 2.60GHz
Memory: 10.48 GB / 12.35 GB
Container: Yes
Shell: 5.0.17 - /bin/bash
Binaries:
Node: 14.15.5 - ~/.nvm/versions/node/v14.15.5/bin/node
npm: 6.14.11 - ~/.nvm/versions/node/v14.15.5/bin/npm
-
Your browser Tested with latest Chrome and Firefox on Windows
-
Your adapter (e.g. Node, static, Vercel, Begin, etc...) dev server
Severity Pretty severe, I think. On my site it completely breaks navigation
I looked more into it and it seems the router is skipping hash only navigation entirely.
Simply allowing the router to handle hash changes also fixes the problem.
I don't know if this entails anything else but the back-navigation to hash routes would be fixed that way.
@lukasIO thanks for providing the repo. I haven't been able to reproduce the issue on https://kit.svelte.dev/ though I was able to with your repo. I'm having a hard time understanding under what conditions exactly this issue occurs though since it happens in your repo, but not https://kit.svelte.dev/. Would you be able to help explain this?
What my assumption is, is that it only happens with links to/from nested folders, e.g. from blog/[slug] to blog/[slug]/detail/[detail].svelte and then back. Though I have not yet verified this.
this also happens on https://kit.svelte.dev/docs : click on a chapter. then click on the docs link in the main nav - you will see the page reloading.
the bug happens here: https://github.com/sveltejs/kit/blob/d965557112136a6ee4fce7c149c329a39758dbfd/packages/kit/src/runtime/client/router.js#L136
this is correct whenever a hash is present on the new url. but when you navigate from some url with a hash to the same url without the hash the browser behaviour is to reload.