Add a cookie consent banner to an Astro site

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

In short: paste the live snippet as the first scripts in the shared layout's <head>, and add is:inline to every Cookie Compliance <script> tag so Astro keeps them as written.

You need #

Where it goes #

Astro processes a <script> tag by default: it bundles the tag and loads it as a deferred module. is:inline tells Astro to leave a tag exactly as written, which is what the banner needs, because huOptions is a synchronous inline script that hu-banner.min.js reads when it runs. Add is:inline to each Cookie Compliance tag in the shared layout:

---
// src/layouts/Layout.astro
---
<html lang="en">
  <head>
    <!-- Cookie Compliance: FIRST scripts on the page (paste live snippet here) -->
    <script is:inline>
      var huOptions = { "appID": "YOUR_APP_ID", "currentLanguage": "en", "blocking": true, "globalCookie": false };
    </script>
    <script is:inline 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. -->

    <slot name="head" />
  </head>
  <body><slot /></body>
</html>

Add is:inline to your existing trackers too, wherever they are declared (this layout, a page, a component), so each tag renders exactly as you wrote it and the placement you check is the placement visitors get.

If the snippet is currently in the <body>, in a component, or has no is:inline, move it to the top of the shared layout's <head> and add is:inline to each tag.

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 verify-install (skills/verify-install/SKILL.md) against the running site: banner shows, no tracking cookies before the visitor chooses in a region whose rule requires consent first, trackers start after Allow. Test from a region your strictest rule covers (for example the EU), or confirm region rules are off. Astro-specific extra:

# Check Result
A1 View source (not devtools' rendered DOM) on / and an inner route huOptions and the hu-banner.min.js (or /v2/…) tag are literal <script> elements, as the first two scripts, in order. A tag Astro had bundled would show as <script type="module" src="...?astro&type=script...">.
A2 Grep the layout for every Cookie Compliance <script> tag Each one has is:inline.

Gotchas #

More help #