nuxt-cookie-consent

Cookie and cookieless-tracking consent for Nuxt 4. Declare your categories in nuxt.config.ts and get a banner that
only appears when something actually needs consent, a preferences modal, a floating settings button, and composables
to gate scripts and cookies behind each category.
Disclaimer: This module provides tools to help you comply with privacy regulations like GDPR, but using it does not automatically guarantee compliance. You are solely responsible for your site's configuration, cookie descriptions, and overall privacy practices. Always consult a legal professional. The author assumes no liability for any legal issues arising from its use.
✨ Features
- Banner only when needed — it appears on the first visit only if a configured category requires it
- Cookies and cookieless tracking —
kind: 'cookie' categories list provider and duration; kind: 'tracking' ones (e.g. cookieless analytics) are a plain toggle, opt-out by default
- Reject as easily as accept — Reject All sits beside Accept All in the banner and the modal
- Asks again when it should — when your
consentVersion changes, or the choice is older than expiresInDays
- Withdrawal that takes effect — the category's cookies are deleted, and the page reloads if its scripts ran
- Events —
consent:changed, consent:granted and consent:revoked Nuxt runtime hooks
- Google Consent Mode v2 —
default and update commands from your categories
- Just-in-time consent —
useRequestConsent asks for one category at the moment a feature needs it
- Preferences without a banner — a theme or language switch asks for consent the first time it is used; until then no cookie is set and no banner is needed
- Consent-gated cookies and scripts —
useConsentCookie writes only after consent; $consent.triggers.<category> loads a script through Nuxt Scripts once granted
- No UI framework required — the default components are plain CSS themed from your
colors; override any of them with a same-named component in your own UI kit
- Headless — use
$consent and the composables as the consent store and render your own UI, or none
- Translatable — every text, including categories, with runtime locale switching; English, German, Spanish and French built in
🚀 Installation
Install the module and its peer dependency:
# npm
npm install @websideproject/nuxt-cookie-consent @nuxt/scripts
# pnpm
pnpm add @websideproject/nuxt-cookie-consent @nuxt/scripts
# bun
bun add @websideproject/nuxt-cookie-consent @nuxt/scripts
Add it to your nuxt.config.ts and declare your categories:
export default defineNuxtConfig({
modules: ['@websideproject/nuxt-cookie-consent'],
cookieConsent: {
categories: {
necessary: { readonly: true },
analytics: { kind: 'tracking', description: 'Cookieless, anonymous usage statistics.' },
},
},
})
The module registers @nuxt/scripts itself, so don't add it to modules. Then render the components once, in
app.vue or a layout:
<template>
<NuxtPage />
<CookieConsentBanner />
<CookieConsentModal />
<CookieConsentFab />
</template>
🧩 Usage
<script setup lang="ts">
const { $consent } = useNuxtApp()
// A script that is requested only once "analytics" is granted
useScript('https://example.com/analytics.js', { trigger: $consent.triggers.analytics })
// A cookie that is written only once "preferences" is granted
const theme = useConsentCookie<'light' | 'dark'>('theme_preference', 'preferences', { default: () => 'light' })
async function toggleTheme() {
// Opens the settings modal for "preferences" if it is not granted yet
if (await useRequestConsent('preferences')) theme.value = theme.value === 'light' ? 'dark' : 'light'
}
</script>
Configuration, the full $consent API, custom components and headless use are in the
module README and the documentation.
📸 Screenshots
The repo's playground renders the built-in components; the pictures are its CI screenshots.