Runtime and server helpers for Vue
Direct access to the runtime from plain code, and reading a returning visitor's consent on the server.
What this page covers
Two things the regular composables do not do: reaching the runtime from code that is not a component, and reading a visitor's saved consent on the server so the banner is right on the first paint. Most apps need neither. If you want to read or change consent in a component, use useConsent() and useConsentActions().
Module-level registry
app.use(CookieYes, config) starts one runtime for the page. getCookieYes() returns it from any code, such as a router guard, a store or a plain module, and useConsentRuntime() returns the same object inside a component.
import { getCookieYes } from "@cookieyes/vue";
export function logCurrentConsent() {
const snapshot = getCookieYes().getSnapshot();
console.log(snapshot.committedCategories);
}The runtime gives you getSnapshot(), subscribe(listener), manager (every action) and getIntegrations() for a debug panel.
Both throw if the plugin has not run yet. Every other composable throws the same way, so call them only after app.use(CookieYes, config), from setup() or from code that runs later, such as an event handler.
resetCookieYes() clears the runtime. Use it between tests.
Read a returning visitor's consent on the server
By default the server does not know what a visitor chose, so it renders the banner for everyone and the browser removes it after loading, which a returning visitor sees as a flash. To avoid it, read the saved decision from the request cookie and pass it to CookieYesProvider.
Read the request's Cookie header with readServerConsent() and pass the result to CookieYesProvider:
import { readServerConsent } from "@cookieyes/vue";
export function getInitialConsent(request: Request) {
// null means the visitor has not chosen yet: the banner shows as usual.
return readServerConsent(request.headers.get("cookie") ?? "", { regulation: "GDPR" });
}The root component takes the result as a prop and hands it to the provider, which wraps the consent components:
<script setup lang="ts">
import {
type ConsentSnapshot,
CookieBanner,
CookiePreferences,
CookieYesProvider,
RecallButton,
} from "@cookieyes/vue";
defineProps<{ initialConsent: ConsentSnapshot | null }>();
</script>
<template>
<CookieYesProvider regulation="GDPR" :initial-consent="initialConsent">
<CookieBanner />
<CookiePreferences />
<RecallButton />
<h1>Your app</h1>
</CookieYesProvider>
</template>Create the app per request, install the plugin with one shared config object, and pass the consent in as a prop:
import { CookieYes, type CookieYesConfig } from "@cookieyes/vue";
import { createSSRApp } from "vue";
import { renderToString } from "vue/server-renderer";
import App from "./App.vue";
import { getInitialConsent } from "./server/consent";
// The same object for every request: the plugin reuses the runtime when the config is the same one.
const config: CookieYesConfig = { mode: "cookie-only", regulation: "GDPR" };
export async function render(request: Request): Promise<string> {
const app = createSSRApp(App, { initialConsent: getInitialConsent(request) });
app.use(CookieYes, config);
return renderToString(app);
}A client-only app (Vite with no server rendering) has no server render, so it needs none of this.
Pass the result to the provider only. Never write a visitor's consent into the config you give app.use(CookieYes, config) or onto the runtime: the server runs one process for every visitor at once, and per-visitor values there would leak between them.
To choose the regulation per visitor on the server, regionFromHeaders(headers) from @cookieyes/vue reads the visitor's region, such as "DE" or "US-CA", from the hosting provider's headers. Map it to "GDPR" or "CCPA" and pass that as the provider's regulation. See the region option on Configuration.
Common mistakes
[cookieyes] getCookieYes() found no runtime, or could not find CookieYes.
app.use(CookieYes, config) has not run on this app, or you called a composable outside setup(). Install the plugin before mounting, and call composables from setup().
The banner flashes on every load for a returning visitor.
No initialConsent reached CookieYesProvider. Read it on the server as shown above.
One visitor's consent appears for another in production.
A per-visitor value was written into the plugin config or onto the runtime on the server. Per-visitor values go on CookieYesProvider only.
Next steps
- Part and state contract: the selectors that are stable for CSS and tests
- useConsent: read consent in a component