---
title: "Astro"
description: "Add the CookieBeam loader to the head of your Astro base layout with is:inline, and read consent in client scripts and islands."
url: "https://cookiebeam.com/docs/install/frameworks/astro"
lang: "en"
last_updated: "2026-11-16"
---

# Astro

Add the CookieBeam loader to the head of your Astro base layout with is:inline, and read consent in client scripts and islands.

In Astro the loader goes in the `<head>` of the base layout that every page uses, with the `is:inline` directive so Astro leaves the tag as it is. Read [Install in a web framework](https://cookiebeam.com/docs/install/frameworks) first for the rules every framework shares.

## Add the loader

```astro
---
// src/layouts/BaseLayout.astro
const { title } = Astro.props;
---
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <!-- 1. CookieBeam first, synchronous -->
    <script
      is:inline
      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 -->
    <title>{title}</title>
  </head>
  <body>
    <slot />
  </body>
</html>
```

`is:inline` matters. Without it, Astro processes the `<script>`: it bundles it as a module, which runs deferred, after the page has been parsed. Every page must use this layout, or include the same tag in its own `<head>`.

## Page navigation

A plain Astro site loads every page in full, and the loader runs on each one, as on any multi-page site.

If you turn on view transitions with Astro's `<ClientRouter />`, navigation swaps pages without a full reload. CookieBeam is built to start once per page load, so after enabling it, navigate between pages and check in the Network panel that `loader.js` isn't requested again and that the console shows no CookieBeam warning. If you see either, leave `<ClientRouter />` off for now.

## Read consent in client code

Use the `hasConsent` and `onConsentChange` helpers from [Read consent in your code](https://cookiebeam.com/docs/install/frameworks#read-consent-in-your-code) in a processed (non-inline) `<script>` in a component:

```astro
<div id="video-slot" hidden>
  <!-- marketing content -->
</div>

<script>
  import { hasConsent, onConsentChange } from '../lib/consent';

  document.addEventListener('astro:page-load', () => {
    const slot = document.getElementById('video-slot');
    if (!slot) return;
    const update = () => { slot.hidden = !hasConsent('marketing'); };
    update();
    onConsentChange(update);
  });
</script>
```

The `astro:page-load` listener runs on every navigation, including the first load, so the snippet above works with `<ClientRouter />` (formerly `<ViewTransitions />`). Without it, a soft document swap replaces the DOM without re-running module scripts, and stored element references go stale.

Hiding markup only hides it. If the content loads a third-party embed or script, don't put it in the HTML; create it from the script once `hasConsent` is true, or mark it up with `data-category` as described in [Script blocking](https://cookiebeam.com/docs/configure/blocking).

## Gate an island on consent

In a framework island, such as a React, Vue or Svelte component with a `client:` directive, use the hook, composable or store from that framework's page ([React](https://cookiebeam.com/docs/install/frameworks/react#read-consent-with-a-hook), [Vue](https://cookiebeam.com/docs/install/frameworks/vue-nuxt#read-consent-with-a-composable), [Svelte](https://cookiebeam.com/docs/install/frameworks/sveltekit#read-consent-with-a-store)) inside the island.

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).

## Content security policy

If your site sends a `Content-Security-Policy` header, see [Content security policy](https://cookiebeam.com/docs/install/content-security-policy). A statically built Astro site has no per-request nonce, so list the CookieBeam hosts in the policy, or add the nonce to the tag from your server.

## Check the installation

1. Build and preview the site (`astro build` and `astro preview`) or check the deployed site. View the page source and confirm the loader is the first script in `<head>`, with no `type="module"`, `async` or `defer`.
2. Check a few pages built from different layouts, so none is missing the tag.
3. Add `localhost` or your preview hostname under **Banner domains** and publish, or the banner won't appear there.
4. Reject, then accept, a category and confirm gated content follows the choice without a reload.

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