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
- Create a free accountAdd your website. Kiescy scans it and finds the trackers that need consent.
- Paste one script into Next.jsYour banner appears and starts holding back trackers until visitors choose.
- 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
| Approach | Good for | Watch out for |
|---|---|---|
| A React cookie consent library | UI-only needs | Mounts after hydration, so it can't stop scripts that loaded earlier |
| A self-built banner | Teams with legal and engineering time | You build regional rules, records and Consent Mode yourself |
| Kiescy with beforeInteractive | Production apps | Start SDKs from window.pira.onChange |
Add your privacy policy
Render your hosted policy inside your own layout:
<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.
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.
| Tool | Category | What happens |
|---|---|---|
| @next/third-parties GoogleAnalytics / GoogleTagManager | Analytics | Load these only after consent, or keep Kiescy before them |
| Vercel Analytics, Vercel Speed Insights | Analytics | Cookieless by design; check your legal view |
| PostHog, Mixpanel, Amplitude | Analytics | Set identifiers; gate them on analytics consent |
| Meta Pixel, LinkedIn Insight Tag | Advertising | Gate on advertising consent |
| Intercom, Crisp chat | Functional | Set cookies when they load |
Google Consent Mode v2 on Next.js
With strategy="beforeInteractive", Kiescy runs before your app's scripts, so its Consent Mode v2 defaults are in place before @next/third-parties or a gtag <Script> loads.
For analytics SDKs initialized in code (PostHog, Mixpanel), start them inside window.pira.onChange once choices.analytics is true, as in the snippet above.
New to Consent Mode? Read basic vs advanced Consent Mode v2.
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.