Install in a web framework
Where the CookieBeam loader goes in Next.js, React, Vue, Nuxt, SvelteKit, Angular, Astro, Remix and Gatsby, and how to read consent from your components.
Senast uppdaterad:
CookieBeam works in every web framework through the same script tag. There's no framework package to install. What changes is where the tag goes, so it ends up as a plain, synchronous <script> near the start of <head> in the HTML the server sends.
Pick your framework:
- Next.js (App Router)
- Next.js (Pages Router)
- React with Vite or Create React App
- Vue and Nuxt
- SvelteKit
- Angular
- Astro
- Remix and React Router
- Gatsby
The rules every framework shares
Put the loader in the document shell, not in a component. Use the file that renders <head> for every page: a root layout, index.html, app.html or a document file. A tag inside a page or route component is missing from other pages, or is added again on navigation.
Keep it synchronous and first. CookieBeam blocks trackers by intercepting them as they load and by wrapping the browser APIs they send data through. It can only do that for code that runs after it. So the loader goes above Google tags, analytics and pixels, with no async, defer or type="module", and it must not go through a framework's script component that changes when it runs. Load order explains what escapes when something runs first.
Leave it out of bundling. Load the tag from cdn.cookiebeam.com as it is. Don't import it, bundle it, self-host it or route it through a script optimiser.
Add every hostname you test on. The loader only runs on hostnames listed under Banner domains, and you must publish the banner after adding one. A dev server on localhost or a preview deployment shows no banner until its hostname is listed; the console says [CookieBeam] Domain not authorized. For staging, you can use an environment loader instead of the production one.
Client-side route changes
The loader runs once per full page load. When a single-page app changes route without reloading, the CookieBeam runtime, the banner and the visitor's choice stay in place, and scripts, iframes and images your app adds later are still checked like any other. Don't call CookieBeam.run() again on navigation: the loader has already started the runtime, and calling run() with a new configuration tears it down and creates a new one.
If your router changes the lang attribute on <html> from one language to another, the banner follows it. See Languages.
Read consent in your code
The loader installs window.CookieBeam. The banner script it adds loads asynchronously, so your app can start before the runtime is ready, and calls made too early throw an error. Two small helpers handle that, using only the Browser API:
// consent.ts
type CookieBeamApi = {
acceptedCategory(category: string): boolean;
};
const CONSENT_EVENTS = [
'cookiebeam:ready',
'cookiebeam:consentUpdate',
'cookiebeam:consentRestored',
];
/** True only when the visitor has accepted the category. */
export function hasConsent(category: string): boolean {
try {
const api = (window as unknown as { CookieBeam?: CookieBeamApi }).CookieBeam;
return api?.acceptedCategory(category) === true;
} catch {
// Server render, or the runtime has not started yet.
return false;
}
}
/** Calls `callback` when consent is ready, restored or changed. Returns an unsubscribe function. */
export function onConsentChange(callback: () => void): () => void {
if (typeof window === 'undefined') return () => {};
CONSENT_EVENTS.forEach((name) => window.addEventListener(name, callback));
return () => CONSENT_EVENTS.forEach((name) => window.removeEventListener(name, callback));
}
hasConsent answers false until the runtime has started, so anything you gate on it stays off until CookieBeam says otherwise. onConsentChange re-runs your check when the runtime becomes ready, when a stored choice is re-applied on page load and when the visitor changes their choice. Category names are the ones configured on your banner, such as analytics or marketing. Each framework page shows how to wrap these helpers in that framework's own state.
Gate a component on consent
Render the component only while hasConsent is true, and subscribe with onConsentChange so it appears as soon as the visitor accepts, without a reload. Keep the gated component out of the server-rendered HTML: start from false on the server and on the first client render, then read consent in the browser.
Two limits apply:
- Hiding a component later doesn't undo what its script already did. If the vendor has no way to switch itself off, its code keeps running until the next page load.
<script type="text/plain" data-category="...">placeholders from Script blocking are scanned once, when the runtime starts. Placeholders your app renders on the client after start-up are never activated during that page view. The runtime doesn't re-scan. In client-rendered components, use the helpers above or callCookieBeam.loadScript(src)once consent is given.
Add a cookie settings link
Visitors need a way to change their choice. A data-cb attribute does it with no JavaScript:
<button type="button" data-cb="show-preferencesModal">Cookie settings</button>
Use a plain <button> or <a href="#">, not your router's link component. The runtime ignores a click whose default action was already prevented, and router links prevent it in order to navigate.
Content security policy
If your site sends a Content-Security-Policy header, see Content security policy for the hosts to allow, the nonce and 'strict-dynamic' pattern, and why styles need 'unsafe-inline'.
Check the installation
- View the page source (not the Elements panel) and find
cdn.cookiebeam.com. The loader appears once, near the start of<head>, before any tracker or third-party script you added, with noasync,deferortype="module". The framework's own application scripts (e.g. Next.js/_next/static/chunks/) may appear before it, and that's fine. - In a private window, the banner appears and the console shows no
Domain not authorizedwarning and no CSP error. - Navigate between a few routes in the app. The Network panel shows one
loader.jsrequest per full page load, not one per route. - Reject optional cookies. Gated components don't render, and the Network panel shows no requests to their vendors.
- Open the cookie settings link and accept a category. Its gated components appear without a reload.
- The console shows no hydration warning about the CookieBeam tag.
Then work through Verify your installation.