---
title: "SvelteKit"
description: "Add the CookieBeam loader to src/app.html in a SvelteKit site, with the CSP nonce placeholder, and read consent with a store."
url: "https://cookiebeam.com/docs/install/frameworks/sveltekit"
lang: "en"
last_updated: "2026-11-16"
---

# SvelteKit

Add the CookieBeam loader to src/app.html in a SvelteKit site, with the CSP nonce placeholder, and read consent with a store.

In SvelteKit the loader goes in `src/app.html`, the page template every route renders into. Read [Install in a web framework](https://cookiebeam.com/docs/install/frameworks) first for the rules every framework shares.

## Add the loader

Put the loader first in `<head>`, above `%sveltekit.head%`:

```html
<!-- src/app.html -->
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <!-- 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 -->
    %sveltekit.head%
  </head>
  <body data-sveltekit-preload-data="hover">
    <div style="display: contents">%sveltekit.body%</div>
  </body>
</html>
```

Don't add the loader with `<svelte:head>` in a layout or page. Those tags belong to the component and are rendered at `%sveltekit.head%`, so they can't be guaranteed to come first.

## With a nonce-based CSP

If you set `kit.csp` with a nonce in `svelte.config.js`, SvelteKit replaces `%sveltekit.nonce%` in `app.html` with the nonce of the current response. Add it to the loader:

```html
<script
  nonce="%sveltekit.nonce%"
  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>
```

Add `'strict-dynamic'` to `script-src` so the scripts CookieBeam adds are trusted too. See [Content security policy](https://cookiebeam.com/docs/install/content-security-policy) for the hosts and the `'unsafe-inline'` requirement for styles.

## Route changes

SvelteKit's client-side navigation doesn'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 store

Wrap the `hasConsent` and `onConsentChange` helpers from [Read consent in your code](https://cookiebeam.com/docs/install/frameworks#read-consent-in-your-code) in a readable store:

```ts
// src/lib/consent-store.ts
import { readable } from 'svelte/store';
import { browser } from '$app/environment';
import { hasConsent, onConsentChange } from './consent';

export function consent(category: string) {
  // false during server rendering; the browser fills in the real value.
  return readable(false, (set) => {
    if (!browser) return;
    const update = () => set(hasConsent(category));
    update();
    return onConsentChange(update);
  });
}
```

## Gate a component on consent

```svelte
<script lang="ts">
  import { consent } from '$lib/consent-store';
  import VideoEmbed from '$lib/VideoEmbed.svelte';

  const marketing = consent('marketing');
</script>

{#if $marketing}
  <VideoEmbed />
{/if}
```

For the cookie settings link, use a plain `<button data-cb="show-preferencesModal">`. See [Add a cookie settings link](https://cookiebeam.com/docs/install/frameworks#add-a-cookie-settings-link).

## Check the installation

1. Build and preview the site (`npm run build` and `npm run preview`) or check the deployed site. View the page source and confirm the loader is the first script in `<head>`, with no `async` or `defer`.
2. With a CSP, the tag carries a real nonce, not the literal `%sveltekit.nonce%`, and the console shows no CSP error.
3. Add `localhost` or your preview hostname under **Banner domains** and publish, or the banner won't appear there.
4. Navigate between routes. The Network panel shows a single `loader.js` request.
5. Reject, then accept, a category and confirm gated components follow the choice without a reload.

Then work through [Verify your installation](https://cookiebeam.com/docs/getting-started/verify-installation).
