0
0
Svelteframework~8 mins

Why API routes serve data endpoints in Svelte - Performance Evidence

Choose your learning style9 modes available
Performance: Why API routes serve data endpoints
MEDIUM IMPACT
This affects how fast data loads on the page and how responsive the app feels when fetching or sending data.
Fetching data for a page in a Svelte app
Svelte
export const GET = async () => {
  const data = await fetchExternalData();
  return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json' } });
};

// API route serves data endpoint separately, page fetches from this route asynchronously.
Separates data fetching into an API route, allowing the page to render faster and fetch data asynchronously, improving LCP and INP.
📈 Performance GainPage renders immediately; data loads asynchronously, reducing LCP by 200-500ms and improving interaction responsiveness.
Fetching data for a page in a Svelte app
Svelte
export async function load() {
  const res = await fetch('https://external-api.com/data');
  const data = await res.json();
  return { data };
}

// This fetches data directly in the page load, blocking rendering until data arrives.
Fetching data directly in the page load blocks the rendering and increases Largest Contentful Paint (LCP). It also mixes data fetching with UI logic.
📉 Performance CostBlocks rendering until data fetch completes, increasing LCP by 200-500ms depending on network.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Direct fetch in page loadMinimal1 (delayed)High (blocks paint)[X] Bad
API route serving data endpointMinimal1 (async after render)Low (non-blocking)[OK] Good
Rendering Pipeline
When API routes serve data endpoints, the browser first renders the page shell quickly without waiting for data. Then it fetches data from the API route asynchronously, triggering minimal reflows and paints.
Network
JavaScript Execution
Layout
Paint
⚠️ BottleneckBlocking network fetch during page load delays Layout and Paint stages.
Core Web Vital Affected
LCP, INP
This affects how fast data loads on the page and how responsive the app feels when fetching or sending data.
Optimization Tips
1Do not fetch data directly during page load; use API routes instead.
2Serve data endpoints separately to avoid blocking initial render.
3Fetch data asynchronously after page shell renders to improve LCP and INP.
Performance Quiz - 3 Questions
Test your performance knowledge
Why is serving data from API routes better for page load performance?
AIt allows the page to render before data arrives, improving load speed.
BIt bundles data with the page increasing size.
CIt blocks rendering until data is fetched.
DIt reduces the number of network requests.
DevTools: Performance
How to check: Record a performance profile while loading the page. Look for long tasks blocking the main thread during initial load and network requests timing.
What to look for: Check if the main thread is blocked waiting for data fetch before first paint (bad) or if data fetch happens after initial render (good).