Skip to content

How to redirect to a URL in Sapper

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>

→ Here's my latest YouTube video

→ Get my Svelte Handbook

→ I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter

JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list