SSR Prefetch Data
Prefetch data on the server and pass it to the client as dehydrated state, so the first paint is full and the client hydrates without a flash of empty content.
On this page
Introduction
This guide covers SSR data prefetch and dehydration, a recurring concern in the data-fetching scenario. A client-only fetch shows an empty page until the data arrives, which is a flash of loading and a slower first paint. Prefetching on the server and passing the result to the client as dehydrated state lets the first paint be full, and the client hydrates with the data already in hand.
Fetch on the server
Run the query in the server loader so the data is ready before the HTML is sent, which is what makes the first paint full.
The trade-off worth naming is that run the query in the server loader so the data is ready before the html is sent, which is what makes the first paint full adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
export async function getServerSideProps() {
const data = await fetch('https://api/orders').then(r => r.json());
return { props: { dehydratedState: dehydrate(queryClient) } };
}
Serialize into HTML
Put the dehydrated state in the HTML as a script tag or prop, so the client can read it without a round trip, which is the handoff.
The trade-off worth naming is that put the dehydrated state in the html as a script tag or prop, so the client can read it without a round trip, which is the handoff adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Hydrate on mount
Read the dehydrated state and hydrate the client cache so the first render uses it and no fetch fires on mount, which is the payoff.
The trade-off worth naming is that read the dehydrated state and hydrate the client cache so the first render uses it and no fetch fires on mount, which is the payoff adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
const queryClient = new QueryClient();
if (window.__DEHYDRATED__) hydrate(queryClient, window.__DEHYDRATED__);
Only what the first paint needs
Dehydrate only the data the first paint uses, so the HTML does not carry data for routes the user may never open, which keeps the document small.
The trade-off worth naming is that dehydrate only the data the first paint uses, so the html does not carry data for routes the user may never open, which keeps the document small adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Background revalidate
After hydration, revalidate in the background so the data stays fresh without blocking the first paint, which combines SSR speed with client freshness.
The trade-off worth naming is that after hydration, revalidate in the background so the data stays fresh without blocking the first paint, which combines ssr speed with client freshness adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test the first paint
Render SSR and assert the data is in the HTML and no fetch fires on mount, so prefetch and hydration are wired correctly.
The trade-off worth naming is that render ssr and assert the data is in the html and no fetch fires on mount, so prefetch and hydration are wired correctly adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: React Query SSR, Astro data fetching.
FAQ
- Dehydrated state or just props?
- For a single query, props are fine; for a cache that the client will keep using, dehydrated state lets you hydrate the whole cache so subsequent navigation is instant.
- How do I avoid shipping huge state?
- Only dehydrate what the first paint needs, and let the client fetch the rest, so the HTML does not balloon with data for routes the user may never visit.
Report a problem with this articleOpens a pre-filled issue on GitHub.