Performance: SvelteKit overview
SvelteKit affects page load speed by optimizing server rendering and client hydration, reducing JavaScript bundle size and improving interaction responsiveness.
export async function load({ fetch }) { const res = await fetch('/api/data'); const data = await res.json(); return { data }; } <p>{data.text}</p>
import { onMount } from 'svelte'; let data; onMount(async () => { const res = await fetch('/api/data'); data = await res.json(); }); {#if data} <p>{data.text}</p> {/if}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side data fetch in onMount | High (delayed DOM update) | Multiple reflows during hydration | High paint cost due to delayed content | [X] Bad |
| Server-side data fetch with load function | Low (HTML ready on load) | Single reflow on initial paint | Low paint cost, fast LCP | [OK] Good |
src/routes folder contains files that become routes automatically.src/routes folder -> Option A <script>
export let data;
</script>
<h1>User: {data.name}</h1>
<p>Age: {data.age}</p>
<!-- Assume load function returns { name: 'Alice', age: 30 } -->load function provides data as props to the page component.{ name: 'Alice', age: 30 }, the template renders those values inside the tags.export async function load() {
const res = await fetch('/api/data');
const data = await res.json();
return data;
}[id].svelte inside the users folder.[id].svelte inside src/routes/users/ -> Option APass the test and this pattern moves unknown