---
title: "Remix and React Router"
description: "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."
url: "https://cookiebeam.com/docs/install/frameworks/remix"
lang: "en"
last_updated: "2026-11-12"
---

# 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.

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](https://cookiebeam.com/docs/install/frameworks) 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](https://cookiebeam.com/docs/install/frameworks/react) page instead.

## Add the loader

Put the loader first in `<head>`, above `<Meta />` and `<Links />`:

```tsx
// 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`:

```tsx
<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](https://cookiebeam.com/docs/install/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](https://cookiebeam.com/docs/install/frameworks#read-consent-in-your-code):

```tsx
// 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

```tsx
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](https://cookiebeam.com/docs/install/frameworks#add-a-cookie-settings-link).

## Check the installation

1. 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 no `async` or `defer`.
2. Add `localhost` or your preview hostname under **Banner domains** and publish, or the banner won't appear there.
3. Navigate with links between routes. The Network panel shows a single `loader.js` request.
4. The console shows no CSP error and no hydration warning.
5. Reject, then accept, a category and confirm gated components follow the choice without a reload.

Then work through [Verify your installation](https://cookiebeam.com/docs/getting-started/verify-installation).
