mefechoel / svelte-navigator

Simple, accessible routing for Svelte
Other
504 stars 39 forks source link

(Vite) useLocation hook throws `Function called outside... #34

Closed mataslib closed 3 years ago

mataslib commented 3 years ago

Describe the bug A clear and concise description of what the bug is.

useLocation() hook throws error Function called outside component initialization.

To Reproduce Steps to reproduce the behavior:

  1. npm init @vitejs/app (choose svelte > javascript)
  2. cd vite-project && npm install
  3. npm install svelte-navigator --save
  4. add Router with Link to Route having useLocation()
  5. click the link and see error in console

Please add a link to an example in the Svelte REPL or in a Codesandbox, if you can.

https://github.com/mataslib/uselocation-bug-repl

Expected behavior A clear and concise description of what you expected to happen.

Show route content.

Screenshots If applicable, add screenshots to help explain your problem.

errorprtscr

Additional context Add any other context about the problem here.

Seems to not work only with Vite dev. It works with vite build, when u visit builded dist html. I used useLocation() before with rollup and it was working. Then I switched to vite for its speed and it also did work. Today, I was removing and updating npm deps and it stopped working with this error. Unfortunately I wasn't able to revert to working state and detect the culprit, so I made this clean repl.

mefechoel commented 3 years ago

Hey, thanks for the detailed description and the repo to reproduce the bug, that's super helpful! I'll try this out in the next days.

I'll close #33 as it seems to be a duplicate.

mefechoel commented 3 years ago

Oops, closed the wrong issue, sorry...

mataslib commented 3 years ago

Hey @mefechoel, thanks for your help and sorry for duplicate, I rly don't know how I managed to do that, heh.

mefechoel commented 3 years ago

This actually seems to be an issue with vite. Checkout this pr in svelte-routing for a workaround. I'll also add an explanation in the readme.

mataslib commented 3 years ago

@mefechoel Thank you very much, this workaround works. 👍