Server-Side Data Fetching

Pre-fetch consent data in Server Components with fetchInitialData — eliminate the loading flash and enable streaming.

The @c15t/nextjs package provides fetchInitialData, a server-side function that fetches consent data before rendering. This enables SSR hydration — consent state is available immediately on page load without a client-side fetch, eliminating the consent banner flash.

Info

SSR hydration is part of the initialization flow. When SSR data is available, the client skips the API fetch entirely.

fetchInitialData

The primary function for fetching consent data on the server. It calls the c15t backend's /init endpoint with the user's request headers (resolved automatically from next/headers) and returns the initial consent data.

Info

Do not await fetchInitialData() in Server Components. Pass the Promise directly to your client component so Next.js can stream the page while the consent data loads in parallel.

How Streaming Works

The diagram below shows how the prefetch avoids blocking the page render. The server fires the /init request and immediately starts streaming HTML — the resolved consent data is sent as a later chunk once the backend responds.

Options

Loading…

Return Value

Returns Promise<SSRInitialData | undefined>. The data includes the init response (jurisdiction, translations, consent model) and GVL data when IAB is configured.

Passing SSR Data to the Provider

Pass the unresolved Promise to the provider's ssrData option via a client component:

How Headers Are Resolved

Unlike @c15t/react/server where you must pass headers manually, fetchInitialData uses next/headers to automatically resolve the incoming request headers. This means geo-location headers from Vercel, Cloudflare, or AWS CloudFront are forwarded to the c15t backend without any extra configuration.

HeaderSourceContains
cf-ipcountryCloudflareCountry code
x-vercel-ip-countryVercelCountry code
x-amz-cf-ipcountryAWS CloudFrontCountry code
x-vercel-ip-country-regionVercelRegion code
accept-languageBrowserLanguage preference
x-forwarded-hostProxyOriginal host
x-forwarded-forProxyClient IP

Advanced: Using @c15t/react/server Directly

For advanced use cases (custom server frameworks, edge functions, or non-standard header resolution), the underlying utilities from @c15t/react/server are available:

See the React Server-Side Utilities docs for full API details.

Debugging SSR

Use the useSSRStatus hook on the client to verify SSR data was consumed: