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.
How do I load Google Analytics or other scripts after consent in Next.js?
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.
How do I add a Cookie settings button and check the deployment?
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.