AlexxNB / tinro

Highly declarative, tiny, dependency free router for Svelte's web applications.
MIT License
675 stars 30 forks source link

router.goto doesn't take base into account in hash mode #106

Closed Estyms closed 2 years ago

Estyms commented 2 years ago

In the following piece of code, one can see that there is two write in the history, a first one with base+href while using history mode and another one with #${href} when using hash mode.

https://github.com/AlexxNB/tinro/blob/d06c191836340e6c44beec72d7e28be3052cb1bd/src/location.js#L66-L70

This, while using hash mode, result in the loss of the base, in the case of a refresh for example.

I believe, a way to fix this would be to use this function instead

MODES.run( MODE, 
     _ => setURL(base+href), 
     _ => setURL(`${base}/#${href}`), 
     _ => memoURL = href 
 ); 
AlexxNB commented 2 years ago

Hello, can you provide a case when base is needed in hash mode? For example, your app is situated under https://domain.tld/foo/bar. And all programatic navigation is allowed only under this path, so goto or relative links will change #hash part only in this case,

Estyms commented 2 years ago

I have an express app that can only be served on a ip:80/vm/ endpoint, meaning that the express.get("/") points to ip:80/vm/

In my svelte app, I've set router.base("/vm"), when I use router.goto("/login") it redirects me to the page as intended, but the url is now ip:80/#/login instead of ip:80/vm/#/login

I've made a fork, which changes the line as I explained in my first message, and it works flawlessly for my needs, maybe there're some aspects that no longer works in the app, but it doesn't seem like it.

AlexxNB commented 2 years ago

Is Express serving Svelte's app (index.html etc)? So your app first page is ip:80/vm/. But using of goto or a-links should not change URL, only the hash part. And page shouldn't be reloaded during navigation, just content changing according Routes. So when app loaded in ip:80/vm/ and you use hash navigation method calling router.goto('/login') will add a hash in URL ip:80/vm/#/login. The router.base option doesn't use with hash navigation method at all, only with history one. Can you provide a minimal example of your case in zip-file or repo?

Estyms commented 2 years ago

Ok so, it seems like it was a one time thing, even with the code that had the bug I can't reproduce it anymore after having used my patched version and switched back to yours.

Kind of a weird thing but I guess it is a closed issue.

Estyms commented 2 years ago

However, I achieved another bug which is the following:

If I enter a page such as this /vm/#/register and if on this page theres an href to subpage like this <a href="something"> it will not take me to /vm/#/register/something but to /vm/#/vm/something/

AlexxNB commented 2 years ago

This is expected behavior. When used relative path, last part of the URL (current level) will be replaced. Browsers resolve pathes in that way also. So /foo/bar will go to /foo/something But /foo/bar/ will go to /foo/bar/something` (pay attention on trailing slash)