Consider a SvelteKit form action that returns a redirect. What will the user see immediately after the form submission?
import { redirect } from '@sveltejs/kit'; export const actions = { default: async ({ request }) => { // some processing throw redirect(303, '/success'); } };
Think about what a redirect response does in a web app.
Returning a redirect from an action causes the browser to navigate to the specified URL immediately after the form submission.
Choose the valid syntax to redirect the user to '/dashboard' after an action completes.
Check the SvelteKit documentation for the redirect helper function.
SvelteKit provides a redirect function to throw a redirect response with status and location. Returning an object with redirect key is not valid.
Given this action code, the user is not redirected after form submission. What is the cause?
import { redirect } from '@sveltejs/kit'; export const actions = { default: async ({ request }) => { // some logic throw redirect(303, '/home'); } };
Think about how SvelteKit expects redirects to be handled in actions.
In SvelteKit, to redirect from an action you must throw the redirect helper, not return an object with a redirect key.
When an action throws redirect(303, '/profile'), what happens to the current page's state and data?
Consider what a redirect response does in HTTP.
Throwing a redirect causes the browser to navigate away, so the current page state is lost and replaced by the new page.
Explain why SvelteKit requires throwing redirect() in actions instead of returning an object with redirect info.
Think about how exceptions control flow in JavaScript and HTTP responses.
Throwing redirect() immediately stops the action and sends a redirect HTTP response to the browser. Returning an object would not interrupt the flow properly.