Performance: Redirect from actions
This affects page load speed and user interaction responsiveness by controlling navigation timing after form submissions or actions.
import { redirect } from '@sveltejs/kit'; export const actions = { default: async ({ request }) => { // process form data throw redirect(303, '/new-page'); } };
export const actions = { default: async ({ request }) => { // process form data return { success: true }; } }; // Redirect done client-side after action response
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side redirect after action | Full DOM load before redirect | Multiple reflows due to page load then redirect | High paint cost due to double rendering | [X] Bad |
| Server-side redirect from action | No DOM load for original page | No reflows before redirect | Minimal paint cost, direct navigation | [OK] Good |
throw redirect(status, url) inside a SvelteKit action?throw redirect(status, url) command is used to send the user to a new page after an action finishes, such as after submitting a form.throw the redirect, not just call or return it.throw redirect(status, url). So throw redirect(303, '/dashboard'); is correct.import { redirect } from '@sveltejs/kit';
export const actions = {
default: async () => {
throw redirect(303, '/success');
}
};redirect(303, '/success'), which instructs SvelteKit to send a redirect response with status 303 to '/success'.import { redirect } from '@sveltejs/kit';
export const actions = {
default: async () => {
return redirect(303, '/home');
}
};thrown, not returned.return redirect(303, '/home'); which is incorrect; it should be throw redirect(303, '/home');.throw redirect(status, '/profile') to follow best practices?throw redirect(303, '/profile') ensures proper redirect behavior after form submission.Pass the test and this pattern moves unknown