Remix and React Router
Add the CookieBeam loader to app/root.tsx in a Remix or React Router framework-mode app, with a CSP nonce, and read consent with a hook.
Last updated:
In Remix, and in React Router used as a framework, the loader goes in app/root.tsx, in the <head> of the Layout (or the root component, if you have no Layout export). That component renders the document shell for every route. Read Install in a web framework first for the rules every framework shares.
If you use React Router only as a library inside a Vite or Create React App project, follow the React page instead.
Add the loader
Put the loader first in <head>, above <Meta /> and <Links />:
// app/root.tsx
import { Links, Meta, Outlet, Scripts, ScrollRestoration } from 'react-router';
// Remix v2: import the same components from '@remix-run/react'.
const COOKIEBEAM_LOADER =
'https://cdn.cookiebeam.com/banner/PUBLIC_BANNER_ID/default/loader.js';
export function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
{/* CookieBeam first and synchronous: it must run before any other tag. */}
<script src={COOKIEBEAM_LOADER} />
<Meta />
<Links />
</head>
<body>
{children}
<ScrollRestoration />
<Scripts />
</body>
</html>
);
}
export default function App() {
return <Outlet />;
}
Don't add the loader through a route's meta or links export: those change per route and don't produce a synchronous script.
With a nonce-based CSP
If your server adds a per-request nonce, pass the same value to the loader as you pass to <Scripts nonce> and <ScrollRestoration nonce>, and add suppressHydrationWarning:
<script src={COOKIEBEAM_LOADER} nonce={nonce} suppressHydrationWarning />
Browsers hide the nonce attribute once the page has been parsed, so the value React sees on the client differs from the server's. suppressHydrationWarning stops React from reporting that difference. Pair the nonce with 'strict-dynamic'; see Content security policy.
Route changes
Client-side navigation doesn't reload the document. The loader runs once per full page load, and the runtime, banner and consent stay in place across routes.
Read consent with a hook
Wrap the hasConsent and onConsentChange helpers from Read consent in your code:
// app/use-consent.ts
import { useEffect, useState } from 'react';
import { hasConsent, onConsentChange } from './consent';
export function useConsent(category: string): boolean {
// false on the server and the first client render, so hydration matches.
const [granted, setGranted] = useState(false);
useEffect(() => {
const update = () => setGranted(hasConsent(category));
update();
return onConsentChange(update);
}, [category]);
return granted;
}
Consent lives in the visitor's browser, so read it in components, not in route loader functions on the server.
Gate a component on consent
import { useConsent } from './use-consent';
export function MarketingVideo() {
return useConsent('marketing') ? <VideoEmbed /> : null;
}
For the cookie settings link, use a plain button with data-cb="show-preferencesModal", not <Link>. See Add a cookie settings link.
Check the installation
- Build and start the production server, or check the deployed site. View the page source and confirm the loader is the first script in
<head>, with noasyncordefer. - Add
localhostor your preview hostname under Banner domains and publish, or the banner won't appear there. - Navigate with links between routes. The Network panel shows a single
loader.jsrequest. - The console shows no CSP error and no hydration warning.
- Reject, then accept, a category and confirm gated components follow the choice without a reload.
Then work through Verify your installation.