Next.js (Pages Router)
Add the CookieBeam loader to pages/_document.tsx in a Next.js Pages Router site and read consent from your components.
Last updated:
In the Pages Router, the loader goes in the custom document, pages/_document.tsx, inside <Head>. The document renders the HTML shell of every page on the server. Read Install in a web framework first for the rules every framework shares.
Add the loader
Create pages/_document.tsx if you don't have one, and put the loader first in <Head>:
// pages/_document.tsx
import { Html, Head, Main, NextScript } from 'next/document';
const COOKIEBEAM_LOADER =
'https://cdn.cookiebeam.com/banner/PUBLIC_BANNER_ID/default/loader.js';
export default function Document() {
return (
<Html lang="en">
<Head>
{/* CookieBeam first and synchronous: it must run before any other tag. */}
{/* eslint-disable-next-line @next/next/no-sync-scripts */}
<script src={COOKIEBEAM_LOADER} />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
Use <Head> from next/document here, not next/head. Tags added with next/head belong to a single page and change on navigation. Don't use next/script either: its loading strategies decide when the script runs, and none of them guarantee the parser-blocking behavior CookieBeam needs. The @next/next/no-sync-scripts lint rule flags the synchronous tag; the synchronous load is deliberate, so disable the rule on that line.
Put Google Tag Manager, gtag.js and other tags after the loader.
If your site uses a nonce-based Content-Security-Policy, add nonce={nonce} to the tag with the same per-request value your policy uses, and pair it with 'strict-dynamic'. See Content security policy.
Route changes
Navigation with next/link or router.push doesn't reload the document. The loader runs once per full page load, and the runtime, banner and consent stay in place across pages. Keep the loader in _document.tsx only, not in _app.tsx or a page.
Read consent in your components
Wrap the hasConsent and onConsentChange helpers from Read consent in your code in a hook:
// lib/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;
}
Gate a component on consent
import { useConsent } from '../lib/use-consent';
export function MarketingVideo() {
const allowed = useConsent('marketing');
if (!allowed) return null;
return <VideoEmbed />;
}
To keep a vendor's code out of the bundle until consent, load it with next/dynamic and ssr: false, and render it only when the hook returns true.
For the cookie settings link, use a plain button with data-cb="show-preferencesModal", not next/link. See Add a cookie settings link.
Check the installation
- Run a production build (
next buildandnext start) 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 pages. 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.