Runtime and server helpers for Nuxt
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 knowing a visitor's saved consent on the server so the banner is right on the first paint. The module does the second for you. If you want to read or change consent in a component, use useConsent() and useConsentActions().
Module-level registry
The module starts one runtime for the app. getCookieYes() returns it from any code, such as a route middleware, a store or a plain module, and useConsentRuntime() returns the same object inside a component. Nuxt imports both for you.
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.
Call it from browser code. On the server one runtime serves every request and holds no visitor's choice, so call getCookieYes() from an event handler, onMounted or other code that runs in the browser. To read consent while rendering, use useConsent().
Read a returning visitor's consent on the server
The module does this for you; there is nothing to call. On every server render it reads the consent cookie and the visitor's region from the Vercel (x-vercel-ip-country, x-vercel-ip-country-region) or Cloudflare (cf-ipcountry) headers, and <CookieYesRoot /> hands both to the CookieYesProvider it renders. Those headers are then the only region source, in the browser too: a region.detect in app/app.config.ts is not used on such a page. The result travels to the browser in the Nuxt payload, so the first client render matches the server HTML. A returning visitor gets no banner and no flash, and a visitor whose region maps to "CCPA" gets the CCPA banner from the server. Global Privacy Control is applied in the browser.
With ssr: false, with nuxt generate, and on routes with swr, isr, cache or prerender route rules, which Nuxt serves to every visitor, the module does not read the request. Those pages behave like a static page: the banner is in the HTML for everyone, and the browser hides it for a returning visitor after hydration. One visitor's choice or region never reaches another.
Composables read the server's answer only inside <CookieYesRoot>. Elsewhere they render a fresh visitor on the server and switch to the live state once mounted.
Common mistakes
getCookieYes is not defined.
@cookieyes/nuxt is missing from modules in nuxt.config.ts, so nothing is imported for you. See Installation.
The banner flashes on every load for a returning visitor.
On a server-rendered route, the banner is rendered outside <CookieYesRoot>, where no server-read consent reaches it. Render it inside the slot of <CookieYesRoot>. With ssr: false, nuxt generate or a cached route, the flash is expected: see above.
Next steps
- Part and state contract: the selectors that are stable for CSS and tests
- useConsent: read consent in a component