Astro Scripts and embeds
Embeds
Why an embed needs gating
An iframe sends requests to its host as soon as it is in the page with a
src, before any script can stop it. ConsentBanner does not block iframes
you already have. Render an embed only while its category is allowed, and
remove it when the visitor withdraws permission.
Astro has no consent gate component. Use one of these:
| Approach | Use it when |
|---|---|
| A custom element that renders the iframe | You want a placeholder with a button in place of the embed |
The iframe blocker, with data-category and data-src | You have iframe markup to gate as it is |
Gate an embed with a custom element
This component shows a placeholder with a preferences button until measurement is allowed. It adds the iframe once the visitor allows measurement, and removes it when permission is withdrawn. It also keeps the iframe out while the visitor has switched YouTube off in vendor consent.
The component reads client.isVendorAllowed('youtube'), so declare youtube
in the vendors option of c15t() with category: 'measurement'. An
undeclared vendor reads as not allowed, and the video never loads:
Use it like any Astro component. How it works:
- The iframe does not exist in the server HTML, so nothing loads before consent, even before the consent runtime starts.
client.subscribe(render)renders again on every consent change.connectedCallbackruns again whenClientRouterswaps in a page that contains the element, so the embed works across navigation.- The first
connect()can run before c15t has started, so the element tries again onDOMContentLoaded.
Change the category, the iframe src and the placeholder text for other
embeds. The YouTube and
Google Maps guides use the same pattern with
a reusable browser helper. Integrations lists
the other vendors.
Gate existing iframe markup
The consent runtime includes an iframe blocker, on by default. Mark an iframe
with a category and move its URL from src to data-src:
When the category is allowed, the blocker copies data-src to src. When it
is withdrawn, the blocker removes src again. data-vendor gates the iframe
on one vendor as well. See vendor consent.
Always use data-src, never src, for a gated iframe. The browser starts
loading a src from the HTML before the blocker runs, so an iframe with src
in the markup loads before consent.
The blocker watches the whole document, so it also gates iframes on pages you
reach with ClientRouter, which replaces <body> on each navigation.
Gate an embed on the server
On a server-rendered page, Astro.locals.c15t.snapshot.effectivePermissions
tells you whether the visitor had allowed the category when the request
arrived. Rendering the iframe on the server from it works for returning
visitors. A visitor who allows the category on the page sees the embed only
after the next navigation, so pair it with the custom element, or use the
custom element alone. See Server API.
Check the embeds
- Open the page in a private window with DevTools Network open. There is no request to the embed's host, and no iframe from it in the Elements panel.
- Allow the embed's category from Privacy settings. The iframe appears and loads.
- Reload. The iframe loads again without a new choice.
- Withdraw the category and save. The page reloads, and the embed's host gets no request.
See Verify consent for the full checklist.