astro-consent is a privacy-first cookie consent integration for Astro. It adds a polished consent banner or overlay, generates an editable stylesheet in your project, and exposes a tiny runtime API for managing stored consent.
- A banner or overlay consent UI
- A generated
src/cookiebanner/styles.cssfile you can edit directly - Idle-based display with a configurable fade-in delay
- Focus trapping,
Escapeto close, and focus return for the overlay - CLI setup, removal, and diagnostics commands
- TypeScript support
- Compatibility with Astro 4, 5, and 6
npm install astro-consentUse the CLI to wire everything into your Astro project:
npx astro-consent initThat will:
- add
astro-consenttoastro.config.* - create
src/cookiebanner/styles.cssif it is missing - add a starter configuration block with placeholder values
Open src/cookiebanner/styles.css in your project and customize the banner or modal visually. The package does not inject CSS through JavaScript, so this file is the single source of truth for styling.
npm run devThe default install is designed to be easy to understand and safe to customise. A typical configuration looks like this:
import astroConsent from "astro-consent";
export default {
integrations: [
astroConsent({
siteName: "My Website",
headline: "Manage cookie preferences for My Website",
description:
"We use cookies to improve site performance, measure traffic, and support marketing.",
acceptLabel: "Accept all",
rejectLabel: "Reject all",
manageLabel: "Manage preferences",
cookiePolicyUrl: "/cookie-policy",
privacyPolicyUrl: "/privacy",
displayUntilIdle: true,
displayIdleDelayMs: 1000,
presentation: "banner",
consent: {
days: 30,
storageKey: "astro-consent"
}
})
]
};npx astro-consent initAdds the integration import and a marked configuration block to astro.config.*, then creates src/cookiebanner/styles.css if the file does not already exist.
npx astro-consent removeRemoves the marked astro-consent block from astro.config.*, removes the import, and deletes src/cookiebanner/styles.css if it exists.
npx astro-consent doctorChecks the following:
- Astro config import wiring
astroConsent(...)integration wiringsrc/cookiebanner/styles.cssexistencesrc/layouts/BaseLayout.astroimporting the stylesheet
Use JSON output for automation:
npx astro-consent doctor --jsonnpx astro-consent statusReports the current install state:
- whether the Astro config is wired
- whether the stylesheet exists
- whether the package is linked into
node_modules - which consent storage key is configured
- a note explaining that actual consent state is browser-side
JSON output is also available:
npx astro-consent status --jsonPreview changes without writing files:
npx astro-consent init --dry-run
npx astro-consent remove --dry-runYou can choose how the consent UI appears:
bannerfor a standard bottom-of-page consent banneroverlayfor a centered modal-first experience
Example:
astroConsent({
presentation: "overlay"
});The consent UI can wait until the browser is idle before appearing, then fade in after a short pause.
When true, the banner or overlay waits for browser idle before showing.
Adds a delay after idle before the UI becomes visible.
Example:
astroConsent({
displayUntilIdle: true,
displayIdleDelayMs: 1000
});These options let you tune the wording without editing the runtime:
siteNamesets the site name used in fallback copyheadlinesets the banner or dialog titledescriptionsets the explanatory textacceptLabelsets the primary actionrejectLabelsets the reject actionmanageLabelsets the preferences action
Example:
astroConsent({
siteName: "Escape Zone",
headline: "Manage cookie preferences for Escape Zone",
description:
"We use cookies to improve site performance, measure traffic, and support marketing.",
acceptLabel: "Accept all",
rejectLabel: "Reject all",
manageLabel: "Manage preferences"
});The consent UI supports separate links for cookie and privacy policy pages.
cookiePolicyUrlcontrols the cookie policy linkprivacyPolicyUrlcontrols the privacy policy link
If you still pass policyUrl, it will be used for both links as a backwards-compatible fallback.
The integration exposes a small browser API on window.astroConsent:
window.astroConsent.get();
window.astroConsent.set({ essential: true, analytics: true, marketing: false });
window.astroConsent.reset();get()returns the stored consent object if one exists and is still validset(...)stores a new consent statereset()clears consent and reloads the page
The generated src/cookiebanner/styles.css file is meant to be edited directly.
It uses cb-* custom properties so you can change the visual style without rewriting selectors. The most useful tokens are:
--cb-bg--cb-text--cb-muted--cb-border--cb-accent--cb-accent-strong--cb-success--cb-shadow--cb-banner-radius--cb-modal-width--cb-button-bg--cb-button-secondary-bg
The installer creates the stylesheet here:
src/cookiebanner/styles.cssThat file is intentionally local to your project. It is not injected from the package at runtime, which keeps styling under your control and avoids overwriting custom work.
- Consent is stored in
localStorage - The stored value expires after the configured TTL
- Optional categories default to checked in the UI, but users can still reject them
- The overlay uses accessible dialog behavior, including focus trapping,
Escapeto close, and focus return to the trigger
Check the following:
npx astro-consent inithas been run in the Astro project rootastro.config.*includes theastro-consentintegrationsrc/layouts/BaseLayout.astroimports../cookiebanner/styles.css- You have not already stored consent in
localStorage
Run:
npx astro-consent initRun:
npx astro-consent doctorand follow the fix instructions it prints.
The design is intentionally simple:
- the runtime handles consent logic and UI behavior
- the generated stylesheet handles appearance
- the CLI wires setup and removal cleanly
That separation keeps the plugin predictable, easier to debug, and safer to customise.