Skip to main content
CookieBeam
Back to Guides
Setup4 min readBy Armin Zaribaf

Remix Cookie Consent: Server-Side Rendering and GDPR

Add GDPR-compliant cookie consent to a Remix app. Covers script placement in root.tsx, streaming SSR considerations, consent-aware loaders, and the framework-specific quirks that trip up most implementations.

Why Remix needs a different approach

Remix renders HTML on the server and hydrates it on the client. That two-phase lifecycle matters for cookie consent: a script that runs during SSR can't read browser cookies, and a script injected only on the client misses the initial page load.

The consent banner itself is purely client-side (it reads and writes browser cookies), but it needs to load early enough to block tracking scripts before they fire. In Remix, that means placing it in root.tsx so it's present on every route from the first server-rendered response.

Quick setup: script in root.tsx

Add the CookieBeam script to the <head> section of your root layout:

// app/root.tsx
import { Links, Meta, Outlet, Scripts, ScrollRestoration } from "@remix-run/react";
export default function App() {
 return (
 <html lang="en">
 <head>
 <script
 src="https://cdn.cookiebeam.com/banner/YOUR_BANNER_ID/default/loader.js"
 />
 <meta charSet="utf-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1" />
 <Meta />
 <Links />
 
 </head>
 <body>
 <Outlet />
 <ScrollRestoration />
 <Scripts />
 </body>
 </html>
 );
}

Place the CookieBeam loader as the first element in <head>, loaded synchronously before GTM, analytics, or other scripts. Do not add async or defer, and exclude it from script-delay optimizers.

Streaming SSR considerations

Remix supports streaming responses via defer() and React Suspense. The consent banner handles this correctly because:

  • The script tag is in <head>, which is always sent in the first flush of the streamed response. The banner loads before any deferred content arrives.
  • Consent state is stored in browser cookies, not in React state. It survives route transitions, deferred data resolution, and error boundaries without re-rendering.
  • If a streamed chunk includes a tracking script, CookieBeam's auto-blocking intercepts it as it enters the DOM, regardless of when it arrives in the stream.

Remix loaders run on the server, where there's no consent state (browser cookies aren't available in loaders by default). Two approaches for server-side consent awareness:

Option 1: Client-only tracking (recommended)

Keep all tracking client-side. Load Google Analytics, Meta Pixel, and other trackers via <script> tags that CookieBeam blocks until consent. Your loaders don't need to know about consent at all.

Option 2: Read consent from the cookie header

If you need server-side consent awareness (rare), you can read the CookieBeam consent cookie in your loader:

// app/routes/some-route.tsx
// CookieBeam's consent cookie is cb_cookie by default: URL-encoded JSON
// whose "categories" array lists the accepted categories.
function acceptedCategories(cookieHeader: string): string[] {
 const match = cookieHeader.match(/(?:^|;\s*)cb_cookie=([^;]+)/);
 if (!match) return [];
 try {
 const record = JSON.parse(decodeURIComponent(match[1]));
 return Array.isArray(record.categories) ? record.categories : [];
 } catch {
 return [];
 }
}
export async function loader({ request }: LoaderFunctionArgs) {
 const cookieHeader = request.headers.get("Cookie") || "";
 const hasAnalyticsConsent = acceptedCategories(cookieHeader).includes("analytics");
 
 // Only fetch personalized data if the visitor consented
 const data = hasAnalyticsConsent
 ? await getPersonalizedContent()
 : getDefaultContent();
 
 return json({ data });
}

This is useful for server-side A/B tests or personalized content that depends on consent.

Place the CookieBeam script before any Google scripts in your <head>. CookieBeam sets region-aware consent defaults before GTM or gtag initializes.

In Remix, the script order in root.tsx determines the execution order. Put the CookieBeam script first:

<head>
 {/* CookieBeam first: sets consent defaults */}
 <script src="https://cdn.cookiebeam.com/banner/YOUR_BANNER_ID/default/loader.js" />
 <Meta />
 <Links />
 {/* GTM second: respects consent state */}
 <script dangerouslySetInnerHTML={{ __html: `...GTM snippet...` }} />
</head>

Remix-specific gotchas

  • Client-side navigation: Remix uses client-side routing after the first page load. The consent banner persists across route changes automatically since it's mounted in the root layout, not in individual routes.
  • Error boundaries: If a route throws and Remix renders an ErrorBoundary, the root layout (including the consent script) is still present. Consent isn't lost during error states.
  • Prefetching: Remix prefetches links on hover. This triggers loader calls but not script execution, so prefetching doesn't cause consent violations. Scripts only fire when the page actually renders.
  • Vite dev server: In development, Remix's Vite server runs on localhost. CookieBeam treats this as a development domain. The banner appears but consent signals aren't sent to production analytics.
Does the banner cause hydration mismatches?
No. The CookieBeam banner is added in the browser and isn't part of your React tree. It doesn't inject server-rendered HTML that would conflict with the client-side React tree.
Can I use Remix's cookie session for consent?
You can read the CookieBeam consent cookie in loaders (as shown above), but don't try to manage consent state through Remix's createCookieSessionStorage. CookieBeam manages its own cookie format and expiry. Using a separate session would create two sources of truth.
Will this work with Remix on Cloudflare Workers?
Yes. The consent banner is entirely client-side. It doesn't matter whether your Remix server runs on Node.js, Deno, or Cloudflare Workers. The script loads from CookieBeam's CDN and runs in the visitor's browser.
What about React Server Components in Remix?
Remix's RSC support (experimental) doesn't change the consent approach. The script tag in root.tsx is a static HTML element, not a React component. It renders identically regardless of whether the route uses RSC or traditional components.

6 min read

Manage CookieBeam from ChatGPT or Claude

Connect an AI assistant to your CookieBeam account over MCP and manage banners, scans and analytics from a conversation. Covers connecting, what the assistant can do, example prompts, access control and known limits.

8 min read

Shopify Cookie Consent: GDPR and CCPA Setup Guide for 2026

Shopify's built-in cookie banner covers the basics, but falls short for stores selling into the EU, California, or multiple regions at once. This guide walks through what Shopify gives you, where it stops, and how to add a proper consent layer with CookieBeam.

AltStyle によって変換されたページ (->オリジナル) /