Performance: Why form actions handle mutations
This affects how quickly the page responds to user input and updates the UI after form submissions.
export const actions = { default: async ({ request }) => { const formData = await request.formData(); // perform mutation here return { success: true }; } }; <script> import { enhance } from '$app/forms'; let status; const form = enhance(({ result }) => { status = result.data.success ? 'Saved!' : 'Error'; }); </script> <form use:form method="post"> <!-- form fields --> <button type="submit">Submit</button> </form>
function handleSubmit(event) { event.preventDefault(); fetch('/submit', { method: 'POST', body: new FormData(event.target) }) .then(() => window.location.reload()); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full page reload on form submit | Rebuilds entire DOM | Multiple reflows | High paint cost | [X] Bad |
| Form actions with progressive enhancement | Updates minimal DOM nodes | Single reflow | Low paint cost | [OK] Good |
actions with async functions for each action.export const actions = { default: async ({ request }) => { /* handle */ } }; correctly exports actions with a default async function receiving { request }.export const actions = {
default: async ({ request }) => {
const data = await request.formData();
const name = data.get('name');
if (!name) return { error: 'Name required' };
return { success: `Hello, ${name}!` };
}
};name field empty?export const actions = {
default: async ({ request }) => {
const data = request.formData();
const age = data.get('age');
if (age < 18) return { error: 'Too young' };
return { success: 'Welcome!' };
}
};Pass the test and this pattern moves unknown