Server-Side Utilities

Fetch consent data on the server for SSR hydration — eliminate the loading flash and improve performance.

The @c15t/react/server module provides utilities for fetching consent data on the server 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.

Info

If you're using Next.js, the @c15t/nextjs package wraps these utilities with automatic header resolution. See the Next.js docs for framework-specific usage.

fetchSSRData

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

Options

Loading…

Return Value

Returns 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 result as a Promise to the provider's ssrData option:

extractRelevantHeaders

Extracts geo-location and forwarding headers from the incoming request. Supports headers from Cloudflare, Vercel, AWS CloudFront, and generic proxies.

Recognized Headers

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

normalizeBackendURL

Resolves a backend URL to an absolute URL. Absolute URLs are returned as-is. Relative URLs (starting with /) are resolved using the request's Host or Referer headers.

validateBackendURL

Validates and normalizes a URL without resolving relative paths. Useful for configuration validation.

Debugging SSR

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