lit-element-router icon indicating copy to clipboard operation
lit-element-router copied to clipboard

Suggestion: Hash Routing

Open sijakret opened this issue 4 years ago • 0 comments

Hi Folks,

loving the lit-element-router!

I recently had to use hash routing in a constrained scenario (karma tests). Found this commit that adds hashes when matching urls: https://github.com/hamedasemi/lit-element-router/pull/22/commits/84e677a967e34cd6e03fcdfd87488696d51f6ef7 I think it does not fully get the job done since, if i am not mistaken

  • every route needs to have hash: true option
  • all navigate calls are need to be adjusted as well.

Since i wanted a solution that allows to transparently switch between hash routing and proper routing i did this: https://github.com/sijakret/lit-element-router/commit/3452507a0308042e4d35ec43af81703b87e5ae13#diff-455606f5cd3de66aefea6c9c80a4026eR40-R55 the location getter could even just be this by default

//..
get location() {
  return window.location
}
//..

So this change would be minimally intrusive (2 lines changed, 3 lines added) and it would allow to switch to hash routing like this (i am already doing it, it works)

const hashRouting = true; // in my case from config
class Router extends router(LitElement) {
//..
  get location() {
    return hashRouting ? {
      get pathname() {
        return window.location.hash.slice(1);
      },
      get search() {
        return window.location.search;
      }
    } : window.location
  }
//..
}
class Navigator extends navigator(LitElement) {
//..
  navigate(href) {
    super.navigate((hashRouting ? '#' :'')+href);
  }
//..
}

This change would also enable many more routing setups where state may be stored wherever the user wants. What do you guys think?

Stay isolated, Jan

sijakret avatar Apr 18 '20 20:04 sijakret