SSR & Hydration
Use the same Orval operation and query key on the server and in the client.
React Query v5 then reuses the server result after hydration instead of
starting a second, unrelated cache. Authentication and transport continue to
come from the shared @djangocfg/api runtime.
For generated operations, Zod models, query keys, and MSW handlers see Data Layer. For Context architecture see State Management.
The React Query v5 hydration pattern
Step 1: Prefetch in a Server Component
// app/[locale]/(pages)/catalog/page.tsx — Server Component
import { dehydrate, HydrationBoundary, QueryClient } from '@tanstack/react-query';
import { getCryptoCoinsListQueryOptions } from '@api/generated/endpoints';
import { CatalogClient } from './CatalogClient';
export default async function CatalogPage() {
const queryClient = new QueryClient();
const params = { page: 1, page_size: 20 };
await queryClient.prefetchQuery(
getCryptoCoinsListQueryOptions(params),
);
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<CatalogClient params={params} />
</HydrationBoundary>
);
}prefetchQuery uses the generated query key and query function. Use
Promise.all when prefetching independent resources. If a non-critical query
may fail, catch that query separately so the page can still render.
Step 2: Read the hydrated query in a Client Component
// CatalogClient.tsx — 'use client'
'use client';
import { useCryptoCoinsList } from '@api/generated/endpoints';
export function CatalogClient({ params }) {
const { data, isPending, error } = useCryptoCoinsList(params, {
query: { staleTime: 60_000 },
});
if (isPending) return <CatalogSkeleton />;
if (error) return <CatalogError error={error} />;
return <CatalogGrid items={data?.results ?? []} />;
}The hook resolves from the hydrated cache on the first client render. It can
still refetch in the background when staleTime or the query policy says it
should.
A smaller alternative: initialData
For a single query, a Server Component can call the generated operation and pass the result to a Client Component:
// Server Component
import { cryptoCoinsList } from '@api/generated/endpoints';
const initialData = await cryptoCoinsList({ page: 1 });
return <CatalogClient initialData={initialData} />;
// Client Component
const query = useCryptoCoinsList(params, { query: { initialData } });Prefer HydrationBoundary for pages with several queries or shared layouts;
use initialData when the data is local to one component and the simpler prop
boundary is clearer.
force-dynamic for Auth-Dependent Pages
Pages that depend on authenticated state, request cookies, or real-time data disable static generation:
export const dynamic = 'force-dynamic';Use it for dashboards, project editors, and other private routes whose result cannot be shared between users.
Client-Only Rendering
For highly interactive pages (video editors, complex canvas), SSR can be skipped with a mounted-state gate:
const [mounted, setMounted] = useState(false);
useEffect(() => { setMounted(true); }, []);
if (!mounted) return <EditorLoader />;
return <EditorLayout />;Alternatively, use a Next.js dynamic import with ssr: false:
const EditorView = dynamic(() => import('./EditorView'), { ssr: false });Static Generation for Locale Segments
Only locale parameters are statically generated; request-dependent content uses SSR:
export function generateStaticParams() {
return LOCALES.map((locale) => ({ locale }));
}Metadata Generation
Server Components handle Next.js metadata via generateMetadata:
export async function generateMetadata({ params }: Props): Promise<Metadata> {
const t = await getTranslations('catalog');
return generateAppMetadata({
title: t('meta.title'),
description: t('meta.description'),
});
}The generateAppMetadata helper from @core/metadata adds OG images,
canonical URLs, and other defaults.
Data Shape Adaptation
When API data has a different shape than a view expects, adapt it with
useMemo at the view boundary:
const listItem = useMemo(() => ({
...data,
main_photo_url: data.photos[0]?.url ?? null,
photos_count: data.photos.length,
}), [data]);Rules
- Prefetch with the generated
get*QueryOptionsand hydrate withdehydrate/HydrationBoundary. An operation markedinvalidated_by(realtime) hasuse<Op>QueryOptionsinstead ofget<Op>QueryOptions, because Orval names an options factory that goes through aqueryOptionsmutator as a hook; the query key factoryget<Op>QueryKeykeeps its name. - Use the matching generated hook in the Client Component; never create a second cache key by hand.
- Use
initialDatafor a small, local boundary andHydrationBoundaryfor shared or multi-query pages. - Use
Promise.allfor independent SSR queries and catch only non-critical failures. - Set
export const dynamic = 'force-dynamic'on auth-dependent and request-specific pages. - Use
dynamic(() => import(...), { ssr: false })or a mounted-state gate for client-only editors.