How to redirect to a URL in Sapper
New Courses Coming Soon
Join the waiting lists
I was working on a Svelte+Sapper application when I had the need to redirect to a page, with URL /spreadsheet/1
, when the user visited the root domain /
, instead of showing the home page.
So I opened src/routes/index.svelte
, removed everything in that file, and I added this code:
<script context="module">
export async function preload(page, session) {
return this.redirect(301, 'spreadsheet/1')
}
</script>
→ Get my Svelte Handbook
Here is how can I help you:
- COURSES where I teach everything I know
- CODING BOOTCAMP cohort course - next edition in 2025
- BOOKS 16 coding ebooks you can download for free on JS Python C PHP and lots more
- Follow me on X