Performance: Loader functions for data fetching
MEDIUM IMPACT
Loader functions impact the initial page load speed by fetching data before rendering, affecting how fast main content appears.
export async function loader() { const [userRes, postsRes] = await Promise.all([ fetch('/api/user'), fetch('/api/posts') ]); const user = await userRes.json(); const posts = await postsRes.json(); return { user, posts }; }
export async function loader() { const user = await fetch('/api/user'); const posts = await fetch('/api/posts'); return { user: await user.json(), posts: await posts.json() }; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Sequential fetch in loader | Minimal DOM nodes initially | 0 during fetch but delays rendering | Delayed paint due to waiting | [X] Bad |
| Parallel fetch with Promise.all | Minimal DOM nodes initially | 0 during fetch, faster rendering start | Paint happens sooner | [OK] Good |