CookieAskInstall guides

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.

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

Check it works