kit icon indicating copy to clipboard operation
kit copied to clipboard

navigating (back) to a route with hash does not work

Open lukasIO opened this issue 5 years ago • 4 comments

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

  1. run and go to localhost:3000/blog/my-blog-post in a new tab
  2. click "anchor link to content"
  3. click "view detail"
  4. try to navigate back via the link (or browser)
  5. 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

lukasIO avatar Apr 01 '21 15:04 lukasIO

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 avatar Apr 02 '21 11:04 lukasIO

@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?

benmccann avatar May 04 '21 21:05 benmccann

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.

lukasIO avatar May 09 '21 09:05 lukasIO

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.

days-later avatar May 13 '21 13:05 days-later