SSR with Loaders: Next.js and Remix Patterns
What You'll Learn
You'll learn how Next.js and Remix let you fetch data on the server before rendering, eliminating waterfalls and useEffect fetch logic entirely.
The Core Idea
Both Next.js and Remix offer loader patterns that run on the server during page requests. These functions fetch your data before React even starts rendering, then pass that data directly to your component as props. No useEffect, no loading spinners, no race conditions—just data that's already there when your component mounts.
How It Works
In Next.js, you export getServerSideProps from a page component:
export async function getServerSideProps() {
const res = await fetch('https://api.example.com/user/1');
const user = await res.json();
return { props: { user } };
}
export default function UserPage({ user }) {
// user is already here, no useEffect needed
return <h1>{user.name}</h1>;
}
In Remix, you export a loader function:
export async function loader() {
const user = await fetch('https://api.example.com/user/1').then(r => r.json());
return user;
}
export default function UserPage() {
const user = useLoaderData();
return <h1>{user.name}</h1>;
}
Both patterns fetch on the server, hydrate the HTML with the data, and send a fully-rendered page to the client. Your component never enters a loading state.
Why This Solves useEffect Problems
No waterfalls. No race conditions. No AbortController juggling. The data arrives with the HTML, not after three render cycles.
Key Takeaway: Server-side loaders (Next.js getServerSideProps, Remix loader) fetch data before rendering, eliminating client-side useEffect fetch complexity and delivering instant, ready-to-render pages.