Performance: Static vs server-side data fetching
This concept affects how fast the main content appears and how quickly the page responds to user interactions.
--- export const prerender = true; const res = await fetch('https://api.example.com/posts/1'); const post = await res.json(); --- <article>{post.title}</article>
--- export const prerender = false; const res = await fetch('https://api.example.com/posts/1'); const post = await res.json(); --- <article>{post.title}</article>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Static data fetching | Minimal - pre-built DOM | 0 on load | Fast paint with ready HTML | [OK] Good |
| Server-side data fetching | Minimal but delayed DOM creation | 0 on load but delayed | Paint delayed until data arrives | [X] Bad |
getStaticPaths to fetch data at build time and generate static paths.getStaticPaths returning paths. Others use wrong function names or incomplete returns.export async function GET({ params }) {
const res = await fetch('https://api.example.com/greet');
const data = await res.json();
return {
body: JSON.stringify({ greeting: data.message })
};
}export async function GET() {
const response = await fetch('https://api.example.com/data');
const data = await response.json;
return {
body: JSON.stringify(data)
};
}response.json without parentheses, which is a function and must be called as response.json().export async function getStaticPaths() { ... } or export async function GET()?getStaticPaths fetches data once at build time, ideal for stable content. GET fetches on every request, unnecessary here.Pass the test and this pattern moves unknown