Performance: Loader functions for data fetching
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 |
loader function in Remix?useLoaderData() return in the component?export const loader = async () => {
return { message: 'Hello Remix' };
};export const loader = () => {
fetch('/api/data').then(res => res.json());
};Pass the test and this pattern moves unknown