Performance: Why load functions fetch data server-side
HIGH IMPACT
This affects the initial page load speed and reduces client-side JavaScript execution time by fetching data on the server before sending HTML to the browser.
export async function load({ fetch }) { const res = await fetch('/api/data'); const data = await res.json(); return { data }; } <script> export let data; </script> <p>{data.message}</p>
export async function load() { return { props: {} }; } <script> import { onMount } from 'svelte'; let data; onMount(async () => { const res = await fetch('/api/data'); data = await res.json(); }); </script> <p>{data ? data.message : 'Loading...'}</p>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side fetch in onMount | Extra DOM updates after load | Triggers 1 reflow per data update | Higher paint cost due to delayed content | [X] Bad |
| Server-side fetch in load function | DOM ready with data on first paint | Single reflow during initial layout | Lower paint cost, faster LCP | [OK] Good |