Integrations

Add CookieConsentHQ to Next.js

Put the CookieConsentHQ script tag directly inside the root layout head in Next.js App Router (or _document.tsx in Pages Router), before any analytics or tag manager scripts. Avoid deferring it with afterInteractive so trackers and Consent Mode v2 defaults are set before tags run.

Get your install codeFree plan, no card needed.
Installation type
One script tag in root head
App Router
app/layout.tsx inside <head>
Pages Router
pages/_document.tsx inside <Head>
Script loading
Synchronous; no async, defer or afterInteractive
Next.js chunks
Verify installation ignores Next's own async /_next/static/ scripts
Verification
Verify installation in the dashboard; free checker at /check

How do I install CookieConsentHQ in Next.js App Router?

Open your root layout (app/layout.tsx) and add a plain synchronous <script> tag inside <head>, ahead of any analytics, Google Tag Manager or third-party script.

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <head>
        {/* eslint-disable-next-line @next/next/no-sync-scripts */}
        <script src="https://cdn.cookieconsenthq.com/s/YOUR_SITE_KEY.js" />
      </head>
      <body>{children}</body>
    </html>
  );
}

Replace YOUR_SITE_KEY with the key from your CookieConsentHQ Install tab. CookieConsentHQ's own Next.js site uses this exact setup.

ESLint's @next/next/no-sync-scripts rule flags synchronous scripts by default, so add the one-line disable comment above the tag. A synchronous loader is intentional here: an async, defer or afterInteractive script lets Google tags and pixels fire before the banner can block them or set Google Consent Mode v2 defaults.

How do I install CookieConsentHQ in Next.js Pages Router?

In the Pages Router, place the loader in pages/_document.tsx inside <Head>, or use next/script with strategy="beforeInteractive" in _document.tsx.

import { Head, Html, Main, NextScript } from "next/document";

export default function Document() {
  return (
    <Html lang="en">
      <Head>
        <script src="https://cdn.cookieconsenthq.com/s/YOUR_SITE_KEY.js" />
      </Head>
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  );
}

Do not move the banner loader into _app.tsx with afterInteractive or lazyOnload, because those strategies wait until hydration finishes.

Load analytics tags after the banner script, or mark inline and external scripts so they wait for a specific category:

<script type="text/plain" data-cchq="analytics" src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX" />

When a visitor accepts analytics, CookieConsentHQ activates the script and sends the Consent Mode v2 update. If you run tags through Google Tag Manager instead, follow the Google Tag Manager guide so the container respects Consent Mode defaults.

If you use a Content Security Policy in next.config.ts, allow https://cdn.cookieconsenthq.com in script-src and connect-src, and https://api.cookieconsenthq.com in connect-src.

To reopen preferences from a footer link or button, create a small client component that calls window.CCHQ?.showPreferences() on click. To display your scanned cookie table on a policy page, render <div data-cchq-cookie-table="" />.

After deploying, run Verify installation in your CookieConsentHQ dashboard or test a public page with the free cookie checker. Next.js always puts its own async /_next/static/ chunks before custom <head> tags; Verify installation ignores your site's own async framework chunks and still catches any tracker or cookie that runs before consent.

For full manual blocking attributes and API calls, see the installation documentation and consent log.

This page is technical installation guidance, not legal advice.

Questions

Should I use next/script or a plain script tag in App Router?
Use a plain synchronous script tag inside head in app/layout.tsx so it runs before any analytics or tag manager script. Do not use next/script with strategy afterInteractive for the banner loader.
Does Next.js putting /_next/static/ chunks first in head break Verify installation?
No. Next.js prints its own async framework chunks at the top of head. CookieConsentHQ's Verify installation check ignores your site's own async and module chunks when checking script order.
Does the banner reload on client-side route transitions?
No. Placing the script in the root layout loads it once for the session and keeps the visitor's consent state across client-side navigations.
How do I open the preferences dialog from a React component?
Call window.CCHQ?.showPreferences() from a client component button click handler, for example in your site footer.