ItalyPaleAle / svelte-spa-router

Router for SPAs using Svelte 3
MIT License
1.53k stars 105 forks source link

Index rendered when hash not present #287

Closed j-bullard closed 1 year ago

j-bullard commented 1 year ago

I noticed that if a user navigates to a page (i.e. http://example.com/login) and doesn't include the hash the index ("/") component is rendered instead of not found. To get by this, I added a small check to App.svelte that will check if the hash is present and if not, will replace the current url with one that includes the hash. Then, spa router will route accordingly.

  if (!window.location.hash) {
    const path = window.location.pathname;
    window.location.replace(`/#${path}`);
  }

Encountering /login would replace the url with /#/login.

Can this be added natively?

ItalyPaleAle commented 1 year ago

Thanks for the suggestion!

However, adding this natively would make it impossible to run the app that uses this router outside of the root path. Some people may very well want to run the app in paths like /app (for example), and that's a fully-supported scenario.