Add a cookie consent banner to a SvelteKit app

Cookbook · platform Last verified View raw markdown (for agents)

In short: paste the live snippet into <head> of src/app.html, above %sveltekit.head%, as classic <script> tags. That shell is the first HTML for every route, including when server rendering is off. Use app.html rather than onMount or <svelte:head>, which load later.

You need #

Where it goes #

Put the live snippet in src/app.html, above %sveltekit.head%.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <!-- Paste LIVE snippet from install.getSnippet / Integrations — first scripts -->
    <script>
      var huOptions = { "appID": "YOUR_APP_ID", "currentLanguage": "en", "blocking": true, "globalCookie": false };
    </script>
    <script
      src="https://cdn.hu-manity.co/v2/hu-banner.min.js"
      type="text/javascript"
      charset="utf-8"
    ></script>
    <!-- A new app. If the live snippet has no /v2/, the app is on v1 — keep that URL. -->
    %sveltekit.head%
  </head>
  <body data-sveltekit-preload-data="hover">
    <div style="display: contents">%sveltekit.body%</div>
  </body>
</html>

Prefer pasting the exact HTML from MCP/dashboard rather than retyping huOptions keys — the live snippet may carry Consent Mode defaults, blockingEngine, or custom providers.

Confirmed on a SvelteKit app, on / and on an inner route, in the dev server and in the production build: those tags are the first <script> elements, with no async, defer or type="module" added. The same holds with ssr set to false.

If the snippet is currently in <svelte:head> or created from onMount, move it to src/app.html, above %sveltekit.head%.

What the order controls #

The banner holds recognised trackers that load after it until the visitor chooses, in regions whose rule requires consent first. A script placed above the snippet runs straight away, and a script you mark data-hu-category="1" is treated as essential. What runs before consent is your decision, so keep analytics and pixels below the snippet.

Confirm the result #

Run the browser checks in a private window (banner shows, no tracking cookies before the visitor chooses in a region whose rule requires consent first, trackers start after Allow) with verify-install (skills/verify-install/SKILL.md) against the running site. Test from a region your strictest rule covers (for example the EU), or confirm region rules are off. SvelteKit-specific extra:

# Check Pass
S1 View source (not devtools' rendered DOM) on / and an inner route huOptions and the hu-banner.min.js (or /v2/…) tag are the first <script> tags, in that order, with no async, defer, or type="module". modulepreload links may follow them.
S2 src/app.html The two tags sit above %sveltekit.head%.

Gotchas #

More help #