Performance: Using data in pages
MEDIUM IMPACT
This affects how quickly the page loads and updates by controlling data fetching and rendering timing.
<script context="module"> export async function load() { const res = await fetch('/api/data'); const data = await res.json(); return { props: { data } }; } </script> <script> export let data; </script> <p>{data.message}</p>
<script>
let data;
fetch('/api/data')
.then(res => res.json())
.then(json => data = json);
</script>
{#if data}
<p>{data.message}</p>
{/if}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fetch data inside component after render | Minimal initially, but triggers updates | Triggers 1 reflow per data update | Multiple paints as data arrives | [X] Bad |
| Fetch data in load function before render | DOM built with data ready | Single layout and paint | Single paint with stable content | [OK] Good |
| Load minimal data first, fetch large data lazily | Initial DOM small, updates later | 1 reflow when large data arrives | Initial paint fast, later paint deferred | [!] OK |