Performance: Why form actions handle mutations
MEDIUM IMPACT
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 |