Search “cookie consent implementation” and you’ll find dozens of React tutorials. But what if you’re building with Vue.js, Angular, or Svelte? These frameworks power millions of production applications, yet developers struggle to find framework-specific guidance for implementing vue angular svelte cookie consent solutions that actually work with SSR, state management, and modern build tools.
This guide provides production-ready implementations for all three frameworks, complete with TypeScript support, SSR/SSG handling, and integration patterns that respect each framework’s architecture. Whether you’re using Nuxt.js, Angular Universal, or SvelteKit, you’ll have working code by the end of this tutorial.
Copying a vanilla JavaScript cookie consent solution into your Vue, Angular, or Svelte application creates more problems than it solves. Each framework has unique characteristics that require tailored approaches.
Vue’s reactivity system uses Proxy-based tracking, Angular relies on Zone.js for change detection, and Svelte compiles reactivity at build time. A cookie consent implementation must integrate with these systems to ensure your UI updates correctly when users accept or reject cookies.
For example, if you store consent state in a plain JavaScript object, Vue components won’t re-render when consent changes. Angular components might trigger unnecessary change detection cycles. Svelte components won’t react at all unless you use stores or reactive declarations.
Server-side rendering introduces a critical problem: cookies don’t exist on the server. When Nuxt.js, Angular Universal, or SvelteKit pre-renders your pages, any code that checks document.cookie will crash. Even if you guard against this with typeof window !== 'undefined', you’ll face hydration mismatches when the server renders one thing and the client renders another.
Your vue cookie consent implementation needs to handle this gracefully, rendering a neutral state on the server and checking actual consent only after hydration completes.
Modern applications use centralized state management—Pinia or Vuex for Vue, services with RxJS for Angular, and stores for Svelte. Cookie consent state belongs in these systems because multiple components need to read it: analytics scripts, marketing pixels, embedded videos, and cookie banners themselves.
A proper angular gdpr compliance implementation exposes consent state through Angular’s dependency injection system, making it available wherever you need it without prop drilling or global variables.
Whether you’re using Vue 3 with the Composition API, Nuxt.js for SSR, or Vue 2 with the Options API, adding cookie consent is the same simple process.
Add these two lines to your index.html (or app.vue for Nuxt):
<!-- In your index.html or nuxt.config head -->
<script src="https://cmp.cookietrust.io/gdpr/autoblocker.umd.js"></script>
<script id="cookietrust-cmp" src="https://cmp.cookietrust.io/gdpr/[YOUR-SITE-ID]/latest/v2consent.js" async></script>
That’s it. The SDK automatically:
For Nuxt.js, add the scripts to your nuxt.config.ts:
// nuxt.config.ts
export default defineNuxtConfig({
app: {
head: {
script: [
{ src: 'https://cmp.cookietrust.io/gdpr/autoblocker.umd.js' },
{
id: 'cookietrust-cmp',
src: 'https://cmp.cookietrust.io/gdpr/[YOUR-SITE-ID]/latest/v2consent.js',
async: true
}
]
}
}
})
Angular applications—whether using standalone components, modules, or Angular Universal for SSR—integrate with CookieTrust the same way.
Add the scripts to your index.html:
<!-- In src/index.html, before closing </head> -->
<script src="https://cmp.cookietrust.io/gdpr/autoblocker.umd.js"></script>
<script id="cookietrust-cmp" src="https://cmp.cookietrust.io/gdpr/[YOUR-SITE-ID]/latest/v2consent.js" async></script>
No Angular services, no RxJS observables, no Zone.js considerations. The SDK handles everything at the DOM level, which means:
Unlike custom Angular services that need isPlatformBrowser guards, CookieTrust’s SDK automatically detects the environment. The banner only renders client-side, preventing any SSR hydration mismatches.
Svelte and SvelteKit applications integrate with CookieTrust without needing custom stores or reactive declarations.
For vanilla Svelte, add to your index.html. For SvelteKit, add to app.html:
<!-- In app.html (SvelteKit) or index.html (Svelte) -->
<script src="https://cmp.cookietrust.io/gdpr/autoblocker.umd.js"></script>
<script id="cookietrust-cmp" src="https://cmp.cookietrust.io/gdpr/[YOUR-SITE-ID]/latest/v2consent.js" async></script>
The SDK integrates seamlessly with Svelte’s compile-time reactivity:
If you need to conditionally load scripts based on consent in your server hooks, CookieTrust’s consent cookie is standard and readable:
// src/hooks.server.ts
export async function handle({ event, resolve }) {
const consent = event.cookies.get('cookie_consent')
// consent contains user preferences if set
return resolve(event)
}
All three frameworks require careful handling of server-side rendering to avoid crashes and hydration mismatches.
Client-Only Rendering Strategies
The safest approach is to render the cookie banner only after the client hydrates. All three implementations above use this strategy:
onMounted hook ensures client-only executiontypeof window checks and isPlatformBrowser guardbrowser check from $app/environment and onMountCookie Detection on Server
If you need to read consent on the server (for example, to conditionally render analytics scripts), access cookies through framework-specific APIs:
| Framework | Server Cookie Access |
|---|---|
| Nuxt.js | useCookie() composable or event.node.req.headers.cookie |
| Angular Universal | REQUEST token with cookie-parser middleware |
| SvelteKit | event.cookies.get() in load functions |
Hydration Mismatch Prevention
Never render different content on server vs. client based on consent state. Instead:
<div class="hide-until-hydrated">Framework Comparison
| Aspect | Vue/Nuxt | Angular | Svelte/SvelteKit |
|---|---|---|---|
| State Management | Composables/Pinia | Services + RxJS | Stores |
| SSR Guard | onMounted |
isPlatformBrowser |
browser + onMount |
| Reactivity | Proxy-based | Zone.js | Compile-time |
| TypeScript | Optional | Required | Optional |
| Bundle Impact | ~3KB | ~5KB | ~2KB |
The implementations above work well—if you have time to build, test, and maintain framework-specific consent logic. But there’s a simpler path.
CookieTrust works with Vue, Angular, Svelte, and every other JavaScript framework with the same two lines of code:
<!-- CookieTrust with Auto-Blocking -->
<script src="https://cmp.cookietrust.io/gdpr/autoblocker.umd.js"></script>
<script id="cookietrust-cmp" src="https://cmp.cookietrust.io/gdpr/[SITE_ID]/latest/v2consent.js" async></script>
That’s it. No composables, no services, no stores. The SDK handles:
| Aspect | DIY Implementation | CookieTrust |
|---|---|---|
| Lines of code | 80-150 per framework | 2 (universal) |
| SSR handling | Manual guards needed | Built-in |
| Script blocking | Write yourself | Auto-blocker included |
| Cookie scanning | Manual tracking | AI-powered detection |
| Compliance updates | You maintain | Automatic |
| Setup time | Hours | Minutes |
Build your own solution if you:
Use CookieTrust if you:
The code examples in this article give you full understanding of what’s happening under the hood. But for most production applications, a managed CMP eliminates the ongoing maintenance burden.
Cookie consent implementations should have minimal impact on your application’s performance.
Lazy Loading Consent Components
Load the banner component only when needed:
// Vue
const CookieBanner = defineAsyncComponent(() =>
import('./components/CookieBanner.vue')
)
// Angular
loadComponent: () => import('./cookie-banner.component')
.then(m => m.CookieBannerComponent)
// Svelte
const CookieBanner = lazy(() => import('./CookieBanner.svelte'))
Bundle Size Optimization
Avoid heavy cookie libraries. The implementations above use native document.cookie APIs, adding less than 2KB to your bundle. If you need a library, choose lightweight options:
js-cookie: 2.2KB minifiedCore Web Vitals Impact
Cookie banners affect Cumulative Layout Shift (CLS) if they cause content to jump. Prevent this by:
position: fixed to overlay contentMeasure impact with Lighthouse and ensure your banner doesn’t delay First Contentful Paint (FCP) by more than 100ms.
Implementing framework cookie consent requires more than copying vanilla JavaScript code. Vue.js, Angular, and Svelte each have unique patterns for state management, SSR handling, and reactivity that your implementation must respect.
What you learned:
onMounted for SSRisPlatformBrowser for Universalbrowser for SvelteKitThese patterns give you complete control—and complete responsibility for maintenance, updates, and compliance changes.
The alternative: CookieTrust’s SDK delivers the same functionality across all three frameworks with a universal two-line embed. The auto-blocker handles script management automatically, and AI-powered scanning keeps your cookie list current without manual updates.
For teams that want compliance without the framework-specific code, start a free CookieTrust trial and be compliant in minutes.
Related guides: