47

I want to be able manage history of my SvelteKit app while simultaneously making sure the whole routing system of SvelteKit doesn't get affected in any way.

Something like:

function routeToPage(route: string) {
   router.push(`/${route}`) // equivalent of this function
}
Himujjal
  • 1,581
  • 1
  • 14
  • 16

4 Answers4

69

Answering my own question thanks to Theo from SvelteKit Discord:

Use $$app-navigation

import { goto } from '$app/navigation';

function routeToPage(route: string, replaceState: boolean) {
   goto(`/${route}`, { replaceState }) 
}

replaceState == true will replace the route instead of adding to the browser history. So, when you click back, you will not go back to the route you came from.

To go back use History API.

import { goto } from '$app/navigation';

function goBack(defaultRoute = '/home') {
  const ref = document.referrer;
  goto(ref.length > 0 ? ref : defaultRoute)
}
Roman Mahotskyi
  • 4,576
  • 5
  • 35
  • 68
Himujjal
  • 1,581
  • 1
  • 14
  • 16
32

You can programmatically navigate to a route in Svelte-Kit using the goto function. The most simple implementation would be something like this:

<script> 
  import { goto } from '$app/navigation';
  goto("/route")
</script>

But you can use more advanced options with it as well, which would be passed as a second argument with the target route.

Theo テオ
  • 565
  • 3
  • 5
5
import { goto } from '$app/navigation';

function routeToPage(route: string) {
  goto(route);
}
Jonathan
  • 3,893
  • 5
  • 46
  • 77
jugurtha moad
  • 304
  • 3
  • 5
3

It may be not exactly what you're searching for, but I think it worth mentioning that can make use of the beforeNavigate callback provided by $app/navigation.

then, you can save the nav history in a store to use it across your application in order to manage some special cases.

/routes/__layout.svelte

<script>
$: console.log($previousPage, $nextPage);

import { beforeNavigate } from "$app/navigation";
    beforeNavigate(({ from, to }) => {
    $history = [to.pathname, ...$history];
});
</script>

{#if $history}
  <ul>
    {#each $history as url}
      <li>{url}</li>
    {/each}
  </ul>
{/if history}

side note: from& to are URL Objects from the Web API: https://developer.mozilla.org/en-US/docs/Web/API/URL

/routes/stores.svelte

import { writable } from 'svelte/store';

export const history = writable(['/']);

Here's a demo based on the SvelteKit Demo app:

https://stackblitz.com/edit/sveltejs-kit-template-default-wivfds?file=src%2Froutes%2Fstores.js&terminal=dev

Big_Boulard
  • 799
  • 1
  • 13
  • 28
  • The demo crashes with error: Error: Failed to load data at Renderer._load (https://sveltejskittemplatedefaultwivfds-gpaa--3000.local-corp.webcontainer.io/@fs/home/projects/sveltejs-kit-template-default-wivfds/.svelte-kit/runtime/client/start.js:1355:16) at async Renderer._get_navigation_result (https://sveltejskittemplatedefaultwivfds-gpaa--3000.local-corp.webcontainer.io/@fs/home/projects/sveltejs-kit-template-default-wivfds/.svelte-kit/runtime/client/start.js:1058:19) – Pick Avana Jan 19 '23 at 14:35
  • It really rough and ready but it's the `todos` page that is returning an error, not crashing. I've just removed the api part so you can test it out once again – Big_Boulard Jan 19 '23 at 17:13