jorgegorka / svelte-router

Svelte Router adds routing to your Svelte apps. It's designed for Single Page Applications (SPA). Includes localisation, guards and nested layouts.
MIT License
508 stars 42 forks source link

navigateTo in routes causes 404 render instead of navigating to page #109

Closed alex-way closed 3 years ago

alex-way commented 3 years ago

I'm trying to use the navigateTo helper function to redirect a user after a successful form submission. However, It seems that navigateTo is unable to find the route and is redirecting to the 404 page instead.

Example code of issue:

// routes.js

const routes = [{
        name: '/',
        component: Index
    },
    {
        name: '/login',
        component: Login
    }
]
<!-- Index.svelte-->
<div>Index</div>
<!-- Login.svelte-->
<script>
  import { navigateTo } from "svelte-router-spa";
navigateTo("/");
</script>

Essentially this is a more simplified version of https://github.com/jorgegorka/demanda/blob/8ab37c751d65e7fe82d2eb1da2880cd37d98d355/frontend/src/views/protected/admin/languages/new/index.svelte#L22

Please let me know if I'm doing something obviously incorrect.

alex-way commented 3 years ago

I've tried to do some digging but I'm not the best at javascript.

I think the issue may be this line here: https://github.com/jorgegorka/svelte-router/blob/master/src/spa_router.js#L8

I assume that when I'm importing navigateTo in my route component (Login.svelte) the userDefinedRoutes variable is reset back to an empty list instead of the one defined in routes.js?

I'm honestly not sure how to go about resolving such an issue and my investigation is probably not even correct.

alex-way commented 3 years ago

I think the issue is actually related to Hot Module Reload instead as I had similar problems when I tried out routify after running into this issue. With routify I was able to resolve the issue by importing the HMR function and wrapping my svelte initialisation call in main.js. However, with this project I'm not sure if it has something similar. Again I'm still fairly new to this so it's likely I'm doing something wrong.