Think about how SvelteKit handles form submissions and page updates after server mutations.
In SvelteKit, form actions that mutate data cause the page to reload automatically. This reload fetches fresh data from the server, so the form resets and the UI updates to show the new state.
Consider where your data lives and who should control changes to it.
Performing mutations on the server via form actions ensures that data changes are validated and secure. It prevents unauthorized or inconsistent updates that could happen if mutations were done only on the client.
export const actions = { default: async ({ request }) => { const data = await request.formData(); await db.updateUser(data.get('name')); return { success: true }; } };
Think about how SvelteKit knows to reload or update the page after a form action.
If a form action does not return a redirect or new data, SvelteKit may not reload the page or update the UI. Returning a redirect or updated data signals the framework to refresh the page state.
Check the correct property names and status code for redirects in SvelteKit form actions.
SvelteKit expects a redirect response with a status code (usually 303) and a 'redirect' property specifying the URL. Option A uses the correct syntax.
export const actions = { default: async ({ request }) => { const data = await request.formData(); const errors = {}; if (!data.get('email')) errors.email = 'Email required'; if (Object.keys(errors).length) return { errors }; await saveData(data); return { success: true }; } };
Think about how SvelteKit handles returned errors from form actions without redirects.
When a form action returns errors without redirecting, SvelteKit keeps the form inputs and displays the errors so the user can fix them without losing their input.