Timotei Oros
Timotei Oros

Reputation: 107

How to route programmatically in SveteKit 1.0?

SvelteKit 1.0 has been out for a very short amount of time as per this writing, and I've encountered something that is nowhere in the new documentation. How can I route programmatically in SvelteKit 1.0? Before SvelteKit 1.0 there was a solution to do this, like it's written in this stackoverflow question. There was a goto function that did this (and now this function is out of the framework). You would've pass a route like "/homepage" and the function would route your app to the homepage page. How can I do this in SvelteKit 1.0? I have found nothing until now.

Upvotes: 3

Views: 497

Answers (1)

Tholle
Tholle

Reputation: 112917

goto still exists, but it is only usable in the browser (in an onMount callback, in an event handler, ...).

If you try to use it in the top-level of your page script tag it will be run on the server as well, which will not work as you might expect. You could e.g. create a +page.server.js file and use the built-in redirect helper to do a server-side redirect instead.

Upvotes: 3

Related Questions