Vue and Nuxt
Add the CookieBeam loader to a Vue single-page app's index.html or to Nuxt's app.head config, and read consent with a composable.
Last updated:
In a Vue single-page app the loader goes in index.html. In Nuxt it goes in the app.head setting of nuxt.config.ts, which Nuxt renders into the <head> of every page. Read Install in a web framework first for the rules every framework shares.
Vue with Vite
Put the loader first in <head> of index.html in the project root:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- 1. CookieBeam first, synchronous -->
<script src="https://cdn.cookiebeam.com/banner/PUBLIC_BANNER_ID/default/loader.js" data-cfasync="false" data-no-optimize="1" data-no-defer="1" data-no-minify="1" data-noptimize="1" nowprocket="1" data-pagespeed-no-defer="1"></script>
<!-- 2. Then Google Tag Manager, gtag.js and other tags -->
<title>My app</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
Nuxt
Add the loader to app.head.script in nuxt.config.ts. tagPriority: 'critical' asks Nuxt to render it ahead of other head tags:
// nuxt.config.ts
export default defineNuxtConfig({
app: {
head: {
script: [
{
src: 'https://cdn.cookiebeam.com/banner/PUBLIC_BANNER_ID/default/loader.js',
tagPriority: 'critical',
},
],
},
},
});
Don't set async or defer on the entry. Keep it in nuxt.config.ts rather than adding it with useHead or useScript in a page or component: the tag needs to be the same on every page and load synchronously. Check the rendered HTML (see below) to confirm the loader is the first script in <head>, above anything a module or plugin adds.
Route changes
Vue Router and Nuxt navigation don't reload the document. The loader runs once per full page load, and the runtime, banner and consent stay in place across routes.
Read consent with a composable
Wrap the hasConsent and onConsentChange helpers from Read consent in your code:
// composables/useConsent.ts
import { onBeforeUnmount, onMounted, ref } from 'vue';
import { hasConsent, onConsentChange } from '../utils/consent';
export function useConsent(category: string) {
// false on the server and until mounted, so hydration matches.
const granted = ref(false);
let stop = () => {};
onMounted(() => {
const update = () => {
granted.value = hasConsent(category);
};
update();
stop = onConsentChange(update);
});
onBeforeUnmount(() => stop());
return granted;
}
Gate a component on consent
<script setup lang="ts">
import { useConsent } from '../composables/useConsent';
const marketing = useConsent('marketing');
</script>
<template>
<VideoEmbed v-if="marketing" />
</template>
To keep a vendor's code out of the initial bundle, load the gated component with defineAsyncComponent (or a Lazy component prefix in Nuxt) and render it only behind the v-if.
For the cookie settings link, use a plain button with data-cb="show-preferencesModal", not <RouterLink> or <NuxtLink>. See Add a cookie settings link.
Content security policy
If your site sends a Content-Security-Policy header, see Content security policy. When your server adds a per-request nonce to Nuxt's own scripts, add the same nonce to the CookieBeam tag and pair it with 'strict-dynamic'.
Check the installation
- Build the app (
vite build, ornuxt buildand run the output) or check the deployed site. View the page source and confirm the loader is the first script in<head>, with noasyncordefer. - Add
localhostor your preview hostname under Banner domains and publish, or the banner won't appear there. - Navigate between routes. The Network panel shows a single
loader.jsrequest. - The console shows no CSP error and no hydration mismatch warning.
- Reject, then accept, a category and confirm gated components follow the choice without a reload.
Then work through Verify your installation.