๐ช nuxt-simple-cookie-consent

A simple, headless, and fully customizable cookie consent module for Nuxt 3.
Built for developers who want full control over styling and behavior, without relying on bloated third-party libraries.
See the example in the playground (uses Nuxt UI)
๐ Features
- โ Headless design โ Full control over all UI/UX. The module only provides logic and script handling.
- โ
Group scripts into multiple categories โ Organize by purpose (e.g.,
analytics,ads, or both). - โ
Required categories โ Mark categories as
required: trueto always enable them and prevent opt-out. - โ
Reactive
useCookieConsent()composable โ Easily track consent preferences, expiration, and control flow in your app. - โ Auto-injection and removal of scripts โ Scripts are dynamically injected or removed based on real-time user preferences.
- โ
Support for multi-category scripts โ A script can belong to multiple categories (e.g., both
adsandanalytics). - โ Consent expiration โ Automatically expire and re-prompt user consent after a configured number of days.
- โ Consent versioning โ Force a new prompt if your policy has changed (version mismatch).
- โ
SSR-safe script control โ Scripts are only injected client-side using
import.meta.client, avoiding SSR issues. - โ Post-load callbacks โ Easily initialize tools like Google Tag Manager or Facebook Pixel after consent is given.
- โ
Inline & pixel tag support โ Supports
<script>,<iframe>, or custom HTML as part of your cookie configuration. - โ
Event lifecycle hooks โ Listen to key events like:
onConsentAcceptedonConsentDeniedonCategoryAccepted(category)onScriptsInjected(category)onScriptsRemoved(category)
- โ
Built-in dev helpers โ Debug logging, timestamp tracking,
resetPreferences()and more.
๐ฆ Installation
Install the module to your Nuxt application with one command:
npx nuxi@latest module add nuxt-simple-cookie-consent
That's it! You can now use nuxt-simple-cookie-consent in your Nuxt app โจ
export default defineNuxtConfig({
modules: ['nuxt-simple-cookie-consent'],
cookieConsent: {
expiresInDays: 180,
consentVersion: '1.0.0',
cookieName: 'cookie_consent',
categories: {
analytics: {
label: 'Analytics',
description: 'Used to improve website performance.',
required: false
},
ads: {
label: 'Advertisement',
description: 'Used for ad personalization.'
}
},
scripts: [
{
id: 'ga2',
src: 'https://www.googletagmanager.com/gtag/js?id=GA_ID',
async: true,
defer: true,
categories: ['analytics', 'ads']
},
{
id: 'ads',
src: 'https://ads.example.com/script.js',
categories: ['ads']
},
{
id: 'ga',
src: 'https://www.googletagmanager.com/gtag/js?id=GA_ID',
customContent: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_ID');
`,
categories: ['analytics']
},
{
id: 'facebook',
customHTML: `
<iframe src="https://www.facebook.com/tr?id=FB_PIXEL_ID&ev=PageView&noscript=1"
height="1" width="1" style="display:none"></iframe>
`,
categories: ['ads'],
src: ''
}
]
}
});
Usage
Use the built-in composable to build your own cookie banner, modal, or settings panel:
const {
preferences,
categoryMeta,
updatePreferences,
acceptAll,
denyAll,
acceptCategories,
hasUserMadeChoice
} = useCookieConsent();
Build your UI using Nuxt UI, Tailwind, or anything else.
Preferences are fully reactive and changes are immediately reflected.
You can find example in the playground with Nuxt UI!
๐ Why Another Cookie Module?
Other modules are:
- โ Too opinionated on UI
- โ Too complex to configure
- โ Not reactive or dynamic enough
This one gives you just the logic โ you handle the rest with your own design system and UX. Well, of course you will get a full working examples, no need to pressure yourself!
๐ Planned Features
- Support multiple categories
- Script injection/removal based on category
- Post-load callbacks
- Required categories (
required: true) - Consent expiration / auto-renew prompt
- DevTools integration
- Built-in helpers for common script types
- SSR-safe inline script support
- Events (
onConsentAccepted, onConsentDenied, onCategoryAccepted, onScriptsInjected, onScriptsRemoved) - Consent Versioning
๐ Contributing
Pull requests, issues, and suggestions are welcome!
If you find a bug or want to propose a feature, open an issue or a PR.
Local development
# Install dependencies
npm install
# Generate type stubs
npm run dev:prepare
# Develop with the playground
npm run dev
# Build the playground
npm run dev:build
# Run ESLint
npm run lint
# Run Vitest
npm run test
npm run test:watch
# Release new version
npm run release