Skip to content

Framework · Install guide

Cookie consent for Next.js

Updated

Load Kiescy with next/script in your root layout and it runs before the rest of your app's scripts, so trackers are held back until visitors agree. No React provider or state to wire up.

Setup time
About 10 minutes
Coding
One line in your layout
Laws covered
GDPR, UK GDPR, CCPA, DPDP
Google Consent Mode v2
Built in, basic or advanced

Set up cookie consent on Next.js in 3 steps

  1. Create a free accountAdd your website. Kiescy scans it and finds the trackers that need consent.
  2. Paste one script into Next.jsYour banner appears and starts holding back trackers until visitors choose.
  3. Check it worksOpen your site in a private window and run the free check to confirm nothing loads early.

Why Next.js sites need cookie consent

Next.js apps often load analytics through @next/third-parties, Vercel, PostHog or Segment at startup. For EU visitors those must wait for consent, and React-based banners mount too late to stop scripts that already ran.

Your options on Next.js

ApproachGood forWatch out for
A React cookie consent libraryUI-only needsMounts after hydration, so it can't stop scripts that loaded earlier
A self-built bannerTeams with legal and engineering timeYou build regional rules, records and Consent Mode yourself
Kiescy with beforeInteractiveProduction appsStart SDKs from window.pira.onChange

Add your privacy policy

Render your hosted policy inside your own layout:

app/privacy/page.tsx
<div data-kiescy-doc="privacy" />
<Script src="https://script.kiescy.com/embed/policy/YOUR_SITE_KEY.js" strategy="afterInteractive" />

Good to know

If you use a Content Security Policy, allow https://script.kiescy.com in script-src and https://api.kiescy.com in connect-src (keep https://api.kiescy.com in script-src too if your tag still loads from it).

Check that it works

Open your site in a private window: the banner should appear for a first-time visitor. Then run the free privacy check, which opens your site as a new EU visitor and lists anything that still loads before consent.

Kiescy's monitoring repeats that check on a schedule and emails you if a new script starts tracking before consent.

What your Next.js visitors see

Kiescy detects where each visitor is and shows the version their law requires, so you don't have to choose between the strictest banner for everyone and no banner at all.

GoodEU, UK, India and other opt-in regions: an opt-in banner. Analytics, ad pixels and embeds wait until the visitor agrees; Reject is as easy as Accept. Banner text: We value your privacy. We use cookies to run this site and, with your permission, to measure visits and show relevant ads. Buttons: Accept all, Reject all, Customise.
GoodCalifornia and other US states: an opt-out notice with a "Do Not Sell or Share" link, and Global Privacy Control honoured automatically. Banner text: Your privacy choices. We share some data with advertising partners. You can opt out at any time. Buttons: Do Not Sell or Share My Personal Information, OK.

Trackers Next.js sites usually load

These are the tools that most often need consent on Next.js sites, and how Kiescy treats them. Run a cookie scan to see exactly what yours loads.

ToolCategoryWhat happens
@next/third-parties GoogleAnalytics / GoogleTagManagerAnalyticsLoad these only after consent, or keep Kiescy before them
Vercel Analytics, Vercel Speed InsightsAnalyticsCookieless by design; check your legal view
PostHog, Mixpanel, AmplitudeAnalyticsSet identifiers; gate them on analytics consent
Meta Pixel, LinkedIn Insight TagAdvertisingGate on advertising consent
Intercom, Crisp chatFunctionalSet cookies when they load

Troubleshooting

beforeInteractive doesn't seem to run first

beforeInteractive scripts must be in the root app/layout.tsx (or pages/_document.tsx). Placed in a nested layout or page, Next treats them differently.

The browser console shows CSP errors

Add the Kiescy API origin to script-src and connect-src in your Content-Security-Policy.

In development the banner seems to initialize twice

React Strict Mode double-runs effects in development. The banner script itself runs once per page load; this doesn't happen in production.

Compliance checklist for Next.js

  • A consent banner that blocks non-essential trackers until the visitor agrees (opt-in for the EU, UK and India).
  • A "Do Not Sell or Share" option for California visitors, and Global Privacy Control honored.
  • A privacy policy that names the tools above, linked from every page.
  • A cookie policy listing the cookies your scan finds.
  • A way for people to request, correct or delete their data.
  • Records of each consent choice, kept as proof.
  • Add a "Cookie settings" link to your footer that calls window.pira.open().
  • Render your privacy policy at /privacy with the embed, so it stays in your own layout and URL.

Guides for Next.js site owners

Questions

Does it cause hydration errors?

No. The banner renders in its own element outside React's tree, so React never sees it.

Does it work with static export and Vercel?

Yes. It's a plain script, so it works with static export, Vercel, Netlify, Cloudflare and self-hosting.

How do I give users a 'Cookie settings' link?

Call window.pira.open() from a button or link, for example in your footer.

Should I use a React cookie consent library instead?

Most React libraries only render a banner; they can't stop scripts that load before React hydrates. A script that runs before everything else can, which is what blocking trackers requires.

Does it work with the Pages Router and App Router?

Yes. Use the root layout for the App Router and _document.tsx for the Pages Router.

Official Next.js documentation

Menus move. If a step above doesn't match what you see, Next.js's own docs show the current way to add code.

Get compliant this afternoon, not this quarter.

Set up your policies, banner and request form in one place. Free while Kiescy is in early access.