Install CookieAsk on Next.js
Templates and presets are not legal advice.
The loader is a plain, synchronous <script> that must be the first script in <head>. In Next.js, put it in the document head yourself. Do not load it with next/script or from a client component's useEffect: those load it after the page's JavaScript, which is too late to block anything.
Replace YOUR-LOADER-ORIGIN and YOUR-SITE-KEY below with the values on the CookieAsk install page (or copy the snippet there and translate it as shown).
App Router (app/layout.tsx)
Put a <head> in the root layout and the script as its first child. React expects attribute names in camelCase (referrerPolicy) and the boolean-style nowprocket as an empty string, so the markup that reaches the browser is the same as the install snippet:
// app/layout.tsx
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<script
src="https://YOUR-LOADER-ORIGIN/l/YOUR-SITE-KEY.js"
referrerPolicy="origin"
data-cfasync="false"
data-skip-moving="true"
data-no-optimize="1"
{...{ nowprocket: '' }}
/>
{/* your other head elements come after the loader */}
</head>
<body>{children}</body>
</html>
);
}
Do not add async or defer. Without async, React renders the tag where you wrote it and the browser runs it first.
For the snippet as the install page shows it, the equivalent HTML is:
<script src="https://YOUR-LOADER-ORIGIN/l/YOUR-SITE-KEY.js" referrerpolicy="origin"
data-cfasync="false" data-skip-moving="true" data-no-optimize="1" nowprocket></script>
Pages Router (pages/_document.tsx)
// pages/_document.tsx
import { Html, Head, Main, NextScript } from 'next/document';
export default function Document() {
return (
<Html lang="en">
<Head>
<script
src="https://YOUR-LOADER-ORIGIN/l/YOUR-SITE-KEY.js"
referrerPolicy="origin"
data-cfasync="false"
data-skip-moving="true"
data-no-optimize="1"
{...{ nowprocket: '' }}
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
If you use a Content-Security-Policy with nonces
When a middleware creates a nonce per request, give the loader tag the same nonce as your other scripts. The loader passes it on to the scripts it releases, and it works with strict-dynamic:
// app/layout.tsx
import { headers } from 'next/headers';
export default async function RootLayout({ children }: { children: React.ReactNode }) {
const nonce = (await headers()).get('x-nonce') ?? undefined; // set by your middleware
return (
<html lang="en">
<head>
<script
src="https://YOUR-LOADER-ORIGIN/l/YOUR-SITE-KEY.js"
nonce={nonce}
referrerPolicy="origin"
data-cfasync="false"
data-skip-moving="true"
data-no-optimize="1"
{...{ nowprocket: '' }}
/>
</head>
<body>{children}</body>
</html>
);
}
Also add the two entries from the install page (a script-src entry for the loader address and a connect-src entry for the event endpoint) to your policy. Reading headers() makes the page dynamic; if you need static pages, use a hash-based policy instead of nonces.
Your own code: a "Cookie settings" link and reading the choice
The loader defines window.__cmp before the page's other scripts run. In a client component:
'use client';
export function CookieSettingsLink() {
return (
<button type="button" onClick={() => window.__cmp?.showPreferences()}>
Cookie settings
</button>
);
}
For TypeScript, declare the global once, for example in types/cmp.d.ts:
declare global {
interface Window {
__cmp?: {
getConsent(): { decided: boolean; categories: Record<string, boolean> };
onChange(callback: (consent: { decided: boolean; categories: Record<string, boolean> }) => void): () => void;
showPreferences(): void;
};
}
}
export {};
Third-party scripts in a Next.js app
- Scripts you add with
next/scriptare not held back by default. For a tracker that must wait for consent, render it as<script type="text/plain" data-cmp-cat="analytics" src="...">(use your category slug); the loader releases it when the visitor allows that category. - Tag Manager: do not put the CookieAsk loader inside a tag; put it in the head as above, and let the Tag Manager snippet come after it. See
integrations/gtm-template/README.mdfor the Consent Mode template.
Check it works
next build && next start, then view the page source: the CookieAsk tag is the first script in<head>.- Open the site in a private window: the banner appears, and no tracker request is made before you answer.
- Answer it, and look at the Installation status card on the install page for the last consent event.