Skip to content

Runtime and server helpers for Svelte

Markdown
Loading…

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 hooks 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

<CookieYesProvider> starts one runtime for the page. getCookieYes() returns it from any code, such as a store or a plain module, and useConsentRuntime() returns the same object inside a component.

src/lib/consent-log.ts
import { getCookieYes } from "@cookieyes/svelte";

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.

getCookieYes() throws with [cookieyes] getCookieYes() found no runtime until a <CookieYesProvider> has run, and useConsentRuntime() throws like every other hook without a provider above it. On the server one runtime serves every request and holds no visitor's choice, so call getCookieYes() from an event handler, onMount or other code that runs in the browser.

resetCookieYes() clears the runtime, so the next provider starts fresh. Use it between tests.

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():

src/server/consent.ts
import { readServerConsent } from "@cookieyes/svelte";

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. config is the module-level object from src/lib/cookieyes.ts: the provider reuses the running runtime when it gets the same object, so a server that renders per request does not build a new one each time.

src/lib/cookieyes.ts
import type { CookieYesConfig } from "@cookieyes/svelte";

export const config: CookieYesConfig = { mode: "cookie-only", regulation: "GDPR" };
src/App.svelte
<script lang="ts">
  import {
    type ConsentSnapshot,
    CookieBanner,
    CookiePreferences,
    CookieYesProvider,
    RecallButton,
  } from "@cookieyes/svelte";
  import { config } from "./lib/cookieyes";

  let { initialConsent }: { initialConsent: ConsentSnapshot | null } = $props();
</script>

<CookieYesProvider {config} regulation="GDPR" {initialConsent}>
  <CookieBanner />
  <CookiePreferences />
  <RecallButton />
  <h1>Your app</h1>
</CookieYesProvider>

Render it per request with the consent passed in:

src/entry-server.ts
import { render } from "svelte/server";
import App from "./App.svelte";
import { getInitialConsent } from "./server/consent";

export function renderPage(request: Request) {
  return render(App, { props: { initialConsent: getInitialConsent(request) } });
}

Hydrate with the same value, so the first client render matches the server HTML. The consent cookie is readable in the browser, so the same function gives the same answer there:

src/entry-client.ts
import "@cookieyes/svelte/styles.css";
import { hydrate } from "svelte";
import { readServerConsent } from "@cookieyes/svelte";
import App from "./App.svelte";

hydrate(App, {
  target: document.body,
  props: { initialConsent: readServerConsent(document.cookie, { regulation: "GDPR" }) },
});

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 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/svelte 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. No <CookieYesProvider> has run yet, or a hook was called outside component initialisation. Render the provider around your app, and call hooks while a component initialises.

The banner flashes on every load for a returning visitor. No initialConsent reached CookieYesProvider, on the server or when hydrating. Pass it in both, as shown above.

The runtime is rebuilt on every server render. The config is written inline in a component, so each render passes a new object. Import one module-level config.

One visitor's consent appears for another in production. A per-visitor value was written into config or onto the runtime on the server. Per-visitor values go on CookieYesProvider only.

Next steps

On this page