Skip to content
Getting StartedInstallation

Installation for SvelteKit

Markdown
Loading…

Install the CookieYes SDK and get a working banner in three steps.

Three steps, about five minutes: install the package, add the handle and the layout files, and render <CookieYesRoot> once around your pages. If you would rather have the files written for you, run npx @cookieyes/cli init. See Quick start.

Prerequisites

RequirementMinimum
Node.js20
SvelteKit2.62 (SvelteKit 3 is supported)
Svelte5.20

1. Install

npm install @cookieyes/sveltekit

This is the only package you install. @sveltejs/kit and svelte are peer dependencies, so the copies your app already has are the ones used. The components and hooks of @cookieyes/svelte are exported from @cookieyes/sveltekit too: import everything from there.

2. Add the handle

Put the configuration in a module of its own. The server and the browser both import it, so they run the same options:

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

export const config: CookieYesConfig = {
  mode: "cookie-only", // no backend needed
  regulation: "GDPR", // "GDPR" | "CCPA"
};

cookieyesHandle and <CookieYesRoot> take the same configuration as initCookieYes() in the other packages, so a configuration copies across unchanged.

Add the handle, which reads each request once:

src/hooks.server.ts
import { cookieyesHandle } from "@cookieyes/sveltekit";
import { config } from "./lib/cookieyes";

export const handle = cookieyesHandle(config);

Serve the stylesheet the handle links to:

src/routes/cookieyes/styles.css/+server.ts
export { GET } from "@cookieyes/sveltekit/styles-route";

And let TypeScript know about event.locals.cookieyes:

src/app.d.ts
/// <reference types="@cookieyes/sveltekit" />
export {};

There is no stylesheet to import. The handle writes the banner's critical CSS into the head of every page and loads the full stylesheet from the route above.

3. Render it once

Pass what the handle read to the page from the root layout's server load:

src/routes/+layout.server.ts
import { loadConsent } from "@cookieyes/sveltekit";

export const load = loadConsent;

Render <CookieYesRoot> in the root layout, with the page inside it:

src/routes/+layout.svelte
<script lang="ts">
  import { CookieYesRoot } from "@cookieyes/sveltekit";
  import { config } from "../lib/cookieyes";

  let { data, children } = $props();
</script>

<CookieYesRoot {config} state={data.consent}>
  {@render children()}
</CookieYesRoot>

<CookieYesRoot> renders the banner, the preferences dialog and the recall button, plus the opt-out dialog when a CCPA regulation can apply, and then your page, inside the same provider, so the hooks work in every page and component. The root layout stays mounted across navigation, and the banner comes before your page content in the DOM: screen readers announce it first, and it paints before the rest of the page.

Done. Reload the page and the banner appears. It is already in the server HTML, and a visitor who has chosen before gets no banner at all. If it does not appear, see Common mistakes.

Common mistakes

loadConsent() found no event.locals.cookieyes in the server log. The handle is missing. Export handle = cookieyesHandle(config) from src/hooks.server.ts, as in step 2. If you have a handle of your own, combine the two with sequence() from @sveltejs/kit/hooks.

<CookieYesRoot> needs a config prop. Pass the object from src/lib/cookieyes.ts: <CookieYesRoot {config} state={data.consent}>, as in step 3.

The banner renders as unstyled text. The stylesheet route is missing, or is mounted somewhere other than /cookieyes/styles.css. Add src/routes/cookieyes/styles.css/+server.ts, as in step 2, or set stylesHref on the handle to where you mounted it.

Your page renders, but the hooks throw "could not find CookieYes". The component is outside <CookieYesRoot>. Render the page through {@render children()} inside it, as in step 3.

Next steps

  • Configuration: every option of src/lib/cookieyes.ts, including CCPA and languages
  • Integrations: load Google Analytics, Meta Pixel and other tags only after consent
  • Styling: match the banner to your brand

On this page