Performance: Action return data
This concept affects how efficiently Svelte actions communicate data back to components, impacting interaction responsiveness and rendering updates.
function myAction(node) { const data = node.getAttribute('data-info'); return { data }; } // Component accesses returned data directly from action result
function myAction(node) { node.addEventListener('click', () => { const data = node.getAttribute('data-info'); node.dispatchEvent(new CustomEvent('data', { detail: data })); }); return {}; } // Component listens for 'data' event and reads detail
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Return data via event dispatch | Multiple event listeners | 0 | Low | [!] OK |
| Return data directly from action | No extra listeners | 0 | Low | [OK] Good |
export const actions = {
default: async ({ request }) => {
const formData = await request.formData();
const name = formData.get('name');
if (!name) {
return { error: 'Name is required' };
}
return { success: `Hello, ${name}!` };
}
};data contain if the form is submitted with name = "Anna"?export const actions = {
default: async ({ request }) => {
const formData = await request.formData();
const age = formData.get('age');
if (age < 18) {
return { error: 'Must be 18 or older' };
}
return { success: true };
}
};<form method="POST">
<input name="email" type="email" />
<button type="submit">Subscribe</button>
</form>
<script>
export let data;
</script>
<p>{data?.success ? data.success : ''}</p>Pass the test and this pattern moves unknown