API JS
import { getConsent, getConsentRecord, setConsent, onConsentChange, whenAccepted, gateScript,} from 'astro-cookie-consent';getConsent(category?, options?)
Section intitulée « getConsent(category?, options?) »getConsent(); // 'accept' | 'decline' | null — reads the 'all' categorygetConsent('analytics'); // 'accept' | 'decline' | null for a specific categoryRenvoie null si rien n’est enregistré ou si l’enregistrement stocké a expiré.
getConsentRecord(options?)
Section intitulée « getConsentRecord(options?) »getConsentRecord(); // { categories: {...}, timestamp } | nullL’enregistrement brut stocké – la valeur actuelle de chaque catégorie et le moment où elle a été
définie. Renvoie null s’il a expiré ou n’existe pas. Utile comme piste d’audit (principe de
responsabilité, article 7, paragraphe 1 du RGPD) – voir Fonctionnalités liées au RGPD.
setConsent(value)
Section intitulée « setConsent(value) »setConsent('accept'); // sets it programmatically and broadcasts the changesetConsent({ analytics: true, marketing: false }); // per-category formonConsentChange(callback)
Section intitulée « onConsentChange(callback) »onConsentChange((record) => { // record.categories is the full map, e.g. { all: true } // or { analytics: true, marketing: false }});Se déclenche à chaque changement futur, avec l’enregistrement complet (pas seulement une catégorie).
whenAccepted(callback, category?)
Section intitulée « whenAccepted(callback, category?) »whenAccepted(() => { /* fires immediately if already accepted, or once accepted */});whenAccepted(() => { /* same, scoped to one category */}, 'analytics');N’est jamais appelé en cas de refus.
gateScript(options)
Section intitulée « gateScript(options) »gateScript({ src: '...', onLoad: () => {} }); // load a script only after consentgateScript({ src: '...', category: 'analytics' }); // same, scoped to one category| Option | Type | Par défaut | Description |
|---|---|---|---|
src |
string |
— | Obligatoire. L’URL du script à charger une fois accepté. |
async |
boolean |
true |
Définit script.async. |
onLoad |
function |
— | Appelée une fois le script chargé. |
category |
string |
'all' |
Catégorie qui doit être acceptée pour l’insérer. |
N’insère jamais le script avant que le visiteur accepte, et ne l’insère jamais deux fois.
Afficher du contenu de façon conditionnelle
Section intitulée « Afficher du contenu de façon conditionnelle »gateScript couvre le chargement d’un script tiers. Pour afficher ou masquer votre propre
interface selon le consentement – un widget, un lien « gérer les cookies », un espace réservé pour un
contenu intégré –, combinez directement getConsent() et onConsentChange(). C’est exactement le
modèle utilisé par le « widget d’audience » de la page d’accueil, qui réagit en direct quand
vous modifiez la bannière :
<div id="analytics-widget">Waiting for consent…</div>
<script> import { getConsent, onConsentChange } from 'astro-cookie-consent';
const widget = document.getElementById('analytics-widget');
const render = (value: 'accept' | 'decline' | null) => { if (!widget) return; widget.textContent = value === 'accept' ? 'Analytics widget — visible' : value === 'decline' ? 'Hidden until you accept analytics' : 'Waiting for consent…'; };
render(getConsent('analytics')); // set the initial state on load onConsentChange(() => render(getConsent('analytics'))); // update on every change</script>getConsent() au chargement gère le visiteur qui revient et a déjà choisi ; onConsentChange() gère
le cas où il change d’avis plus tard via le bouton de réouverture – les deux sont nécessaires pour que
le contenu corresponde toujours au choix actuel.
Pour une version plus complète – un espace réservé avec un bouton « Activer » pour un contenu tiers intégré –, consultez Désactiver des widgets individuels.
Expiration
Section intitulée « Expiration »Un choix enregistré expire après expiryDays (365 par défaut, configurable via la prop expiryDays de
<ConsentBanner /> ou l’option expiryDays de getConsent/getConsentRecord) – ensuite, les deux
renvoient de nouveau null et la bannière réapparaît au chargement suivant.