Svelte · Middle · Question 40 of 75

How do you redirect from a server load or action?

import { redirect } from "@sveltejs/kit";

if (!locals.user) redirect(303, "/login");

Explanation

Use 303 after a POST so the browser follows with GET. Don't call redirect inside a try block that swallows it.

Further reading

Problem with a question

Svelte · Middle · question 40

Test yourself

Take the Svelte · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz