@luwiostack/cookie-consent 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +415 -2
- package/dist/index.cjs +569 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +118 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +118 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +558 -0
- package/dist/index.mjs.map +1 -0
- package/dist/machine-names-DAJioLt5.mjs +102 -0
- package/dist/machine-names-DAJioLt5.mjs.map +1 -0
- package/dist/machine-names-DCujHBVM.cjs +137 -0
- package/dist/machine-names-DCujHBVM.cjs.map +1 -0
- package/dist/react.cjs +124 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +109 -0
- package/dist/react.d.cts.map +1 -0
- package/dist/react.d.mts +109 -0
- package/dist/react.d.mts.map +1 -0
- package/dist/react.mjs +120 -0
- package/dist/react.mjs.map +1 -0
- package/dist/types-ByyibBUg.d.cts +272 -0
- package/dist/types-ByyibBUg.d.cts.map +1 -0
- package/dist/types-ByyibBUg.d.mts +272 -0
- package/dist/types-ByyibBUg.d.mts.map +1 -0
- package/dist/used-vendors-C8uSNIkd.cjs +14 -0
- package/dist/used-vendors-C8uSNIkd.cjs.map +1 -0
- package/dist/used-vendors-Dxk4PZtY.mjs +9 -0
- package/dist/used-vendors-Dxk4PZtY.mjs.map +1 -0
- package/dist/vendors.cjs +619 -0
- package/dist/vendors.cjs.map +1 -0
- package/dist/vendors.d.cts +171 -0
- package/dist/vendors.d.cts.map +1 -0
- package/dist/vendors.d.mts +171 -0
- package/dist/vendors.d.mts.map +1 -0
- package/dist/vendors.mjs +606 -0
- package/dist/vendors.mjs.map +1 -0
- package/package.json +89 -3
package/README.md
CHANGED
|
@@ -1,3 +1,416 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @luwiostack/cookie-consent
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Cookie consent for React apps. The four categories are fixed: necessary, preferences, statistics
|
|
4
|
+
and marketing. Per category, you list the vendors your site uses there; every cookie a vendor sets
|
|
5
|
+
carries its category, so a vendor brings only its cookies of the categories you use it in. The
|
|
6
|
+
decision is kept in `localStorage`, and the visitor is asked again when the policy changes or a
|
|
7
|
+
year passes. The package also has a gate for embeds, script unblocking, removal of a withdrawn
|
|
8
|
+
category's cookies and ready-made vendor presets. There is no UI: every app
|
|
9
|
+
builds its own banner and preferences on `useConsent()`.
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm i @luwiostack/cookie-consent
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Create the instance
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// consent.ts
|
|
19
|
+
import {
|
|
20
|
+
type ConsentTransferDeclaration,
|
|
21
|
+
type ConsentVendorDeclaration,
|
|
22
|
+
createConsent,
|
|
23
|
+
} from '@luwiostack/cookie-consent'
|
|
24
|
+
|
|
25
|
+
const DAY = 24 * 60 * 60 * 1000
|
|
26
|
+
|
|
27
|
+
// Every vendor in full: the same shape your backend sends (see "Config from your backend").
|
|
28
|
+
// A cookie without a domain is first-party; one with a domain is set on the vendor's.
|
|
29
|
+
const googleTransfers: ConsentTransferDeclaration[] = [
|
|
30
|
+
{ country: 'GB', safeguards: ['adequacy'] },
|
|
31
|
+
{ country: 'JP', safeguards: ['adequacy'] },
|
|
32
|
+
{ country: 'US', safeguards: ['dpf', 'scc'] },
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
const googleAnalytics: ConsentVendorDeclaration = {
|
|
36
|
+
id: 'google_analytics',
|
|
37
|
+
privacy_policy_url: 'https://policies.google.com/privacy',
|
|
38
|
+
transfers: googleTransfers,
|
|
39
|
+
cookies: [
|
|
40
|
+
{ name: '_ga', category: 'statistics', max_age: 730 * DAY },
|
|
41
|
+
{ name: '_ga_*', category: 'statistics', max_age: 730 * DAY },
|
|
42
|
+
],
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const youtube: ConsentVendorDeclaration = {
|
|
46
|
+
id: 'youtube',
|
|
47
|
+
privacy_policy_url: 'https://policies.google.com/privacy',
|
|
48
|
+
transfers: googleTransfers,
|
|
49
|
+
cookies: [
|
|
50
|
+
{ name: 'PREF', category: 'preferences', max_age: 240 * DAY, domain: '.youtube.com' },
|
|
51
|
+
{ name: 'YSC', category: 'marketing', max_age: 'session', domain: '.youtube.com' },
|
|
52
|
+
{ name: 'VISITOR_INFO1_LIVE', category: 'marketing', max_age: 180 * DAY, domain: '.youtube.com' },
|
|
53
|
+
],
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const metaPixel: ConsentVendorDeclaration = {
|
|
57
|
+
id: 'meta_pixel',
|
|
58
|
+
privacy_policy_url: 'https://www.facebook.com/privacy/policy/',
|
|
59
|
+
transfers: [{ country: 'US', safeguards: ['dpf'] }],
|
|
60
|
+
cookies: [
|
|
61
|
+
{ name: '_fbp', category: 'marketing', max_age: 90 * DAY },
|
|
62
|
+
{ name: 'fr', category: 'marketing', max_age: 90 * DAY, domain: '.facebook.com' },
|
|
63
|
+
],
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Your own strictly necessary cookies — always allowed, never asked.
|
|
67
|
+
const mySite: ConsentVendorDeclaration = {
|
|
68
|
+
id: 'my_site',
|
|
69
|
+
cookies: [
|
|
70
|
+
{ name: 'session', category: 'necessary', max_age: 'session' },
|
|
71
|
+
{ name: 'luwio.consent', category: 'necessary', type: 'local_storage', max_age: 'persistent' },
|
|
72
|
+
],
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const consent = createConsent({
|
|
76
|
+
// Per category, the vendors your site uses there. A vendor brings only its cookies of that category.
|
|
77
|
+
categories: {
|
|
78
|
+
necessary: [mySite],
|
|
79
|
+
statistics: [googleAnalytics],
|
|
80
|
+
preferences: [youtube], // the YouTube embed (PREF) — without YouTube's marketing cookies
|
|
81
|
+
marketing: [metaPixel],
|
|
82
|
+
},
|
|
83
|
+
policyVersion: 1, // bump when the vendors change → everyone is asked again
|
|
84
|
+
// maxAge: 365 days by default
|
|
85
|
+
onChange: (next, previous) => {
|
|
86
|
+
// log the decision server-side: next?.consent_id, next?.granted(), next?.decided_at
|
|
87
|
+
},
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
consent.isGranted('statistics') // a category ('stats' is a type error)
|
|
91
|
+
consent.isGranted('preferences', 'youtube') // + YouTube is used in preferences: the embed
|
|
92
|
+
consent.isGranted('marketing', 'youtube') // false — not used there, so no tracking
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
A vendor listed under a category brings only its cookies of that category, plus its necessary
|
|
96
|
+
cookies, which are always allowed. So `preferences: [youtube]` is the embed alone (`PREF`). Add
|
|
97
|
+
`youtube` to `marketing` as well to declare YouTube's tracking cookies, and the vendor then needs
|
|
98
|
+
both categories. Listing a vendor under a category it has no cookies in throws.
|
|
99
|
+
|
|
100
|
+
Each listing may also declare the vendor differently, for example a preset narrowed per category
|
|
101
|
+
(`youtube({ cookies: ['PREF'] })` under preferences, `youtube({ cookies: ['YSC'] })` under
|
|
102
|
+
marketing). The vendor then gets the cookies of every listing. Its `privacy_policy_url` and
|
|
103
|
+
`transfers`, and a cookie listed twice, must be declared the same way each time, or it throws.
|
|
104
|
+
|
|
105
|
+
In production the declarations come from your backend, in this same shape: see
|
|
106
|
+
[Config from your backend](#config-from-your-backend).
|
|
107
|
+
|
|
108
|
+
**Where the decision is kept.** By default in `localStorage` (`luwio.consent`), synced across
|
|
109
|
+
tabs. A stored decision stops counting when its policy version differs, when it's older than
|
|
110
|
+
`maxAge`, or when it's dated in the future (it could never expire; a few minutes of clock drift are
|
|
111
|
+
tolerated). Where storage is blocked or full, the visitor's choice still applies **for the rest of
|
|
112
|
+
the page's life** — the banner closes, gates open — and they're asked again on the next page load;
|
|
113
|
+
`onChange` fires as usual.
|
|
114
|
+
|
|
115
|
+
`consent.categories` is always the four categories, in order (`CONSENT_CATEGORIES`). Each one
|
|
116
|
+
carries the vendors used in it, and only those cookies. `necessary` is required: it is
|
|
117
|
+
always granted and never part of the question. Categories carry no text: like every LuwioStack
|
|
118
|
+
domain object they have a `machine_name`, so render `t(category.machine_name)` (and
|
|
119
|
+
`t(category.description_machine_name)`) from the catalogs below.
|
|
120
|
+
|
|
121
|
+
## React
|
|
122
|
+
|
|
123
|
+
```tsx
|
|
124
|
+
import { Consent, ConsentGate, useConsent } from '@luwiostack/cookie-consent/react'
|
|
125
|
+
|
|
126
|
+
<Consent consent={consent}>
|
|
127
|
+
<App />
|
|
128
|
+
<CookieBanner />
|
|
129
|
+
</Consent>
|
|
130
|
+
|
|
131
|
+
// Your own banner — the package ships no UI, only the state and actions.
|
|
132
|
+
function CookieBanner() {
|
|
133
|
+
const { needsDecision, isPreferencesOpen, acceptAll, rejectAll, openPreferences } = useConsent()
|
|
134
|
+
if (!needsDecision || isPreferencesOpen) return null
|
|
135
|
+
return (
|
|
136
|
+
<section aria-label="Cookies" className="cookie-banner">
|
|
137
|
+
<h2>We use cookies</h2>
|
|
138
|
+
<button type="button" onClick={rejectAll}>Reject all</button>
|
|
139
|
+
<button type="button" onClick={openPreferences}>Customize</button>
|
|
140
|
+
<button type="button" onClick={acceptAll}>Accept all</button>
|
|
141
|
+
</section>
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Hold an embed back until its category is granted — and, with a vendor, only while that vendor is
|
|
146
|
+
// used in the category (it renders nothing when YouTube isn't used in preferences):
|
|
147
|
+
<ConsentGate
|
|
148
|
+
category="preferences"
|
|
149
|
+
vendor="youtube"
|
|
150
|
+
fallback={(gate) => <button onClick={gate.grant}>Load video</button>}
|
|
151
|
+
>
|
|
152
|
+
<iframe src="https://www.youtube-nocookie.com/embed/…" />
|
|
153
|
+
</ConsentGate>
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
The package ships **no UI**. Every app builds its own banner and preferences, so they match the
|
|
157
|
+
product. `useConsent()` gives you what they need:
|
|
158
|
+
|
|
159
|
+
- `needsDecision`
|
|
160
|
+
- `categories`, each with its vendors and cookies
|
|
161
|
+
- `isGranted(category, vendor?)`
|
|
162
|
+
- `acceptAll`, `rejectAll` and `save`
|
|
163
|
+
- `isPreferencesOpen`, `openPreferences` and `closePreferences`: shared state, so a link anywhere
|
|
164
|
+
can open your preferences. The three decision actions close them.
|
|
165
|
+
|
|
166
|
+
Keep your preferences' unsaved ticks in your own state, starting from `isGranted`, so nothing is
|
|
167
|
+
ever pre-ticked. `apps/showcase` (`src/app/CookieBanner.tsx`) is a full example.
|
|
168
|
+
|
|
169
|
+
### Pages without the banner
|
|
170
|
+
|
|
171
|
+
Don't let the banner cover the page that explains it: the privacy and cookie policy, the legal
|
|
172
|
+
notice. Where the banner shows is your app's call, so decide it per route. With
|
|
173
|
+
`@luwiostack/router`, flag the route with `staticData` and read the flag in your banner:
|
|
174
|
+
|
|
175
|
+
```tsx
|
|
176
|
+
// privacy.route.tsx
|
|
177
|
+
createRoute({ id: 'privacy', staticData: { hideCookieBanner: true }, … })
|
|
178
|
+
|
|
179
|
+
// CookieBanner.tsx
|
|
180
|
+
const { needsDecision, isPreferencesOpen } = useConsent()
|
|
181
|
+
const hideBanner = useRouteStaticData((d) => d.hideCookieBanner === true)
|
|
182
|
+
if (isPreferencesOpen) return <Preferences />
|
|
183
|
+
if (needsDecision && !hideBanner) return <Banner />
|
|
184
|
+
return null
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Type the flag once by augmenting `RouteStaticData` (see the router README). Hide only the
|
|
188
|
+
**banner**, not the preferences: a visitor who opens "Cookie settings" on the privacy page gets them.
|
|
189
|
+
Nothing is unblocked meanwhile: the status stays `pending`, so only `necessary` is granted, and the
|
|
190
|
+
banner shows again on the next page. Prefer a route flag over checking the pathname, which breaks
|
|
191
|
+
as soon as the slug is translated.
|
|
192
|
+
|
|
193
|
+
## Vendors and their cookies
|
|
194
|
+
|
|
195
|
+
Most cookies come from the same handful of services, so the package ships their declarations at
|
|
196
|
+
`@luwiostack/cookie-consent/vendors`. They are data only, like a country's `machine_name`, and every
|
|
197
|
+
cookie is already tagged with its category; you choose which categories you use a vendor in. Each preset is a separate export, so your bundle only
|
|
198
|
+
holds the ones you import. Call a preset as is, keep only the cookies your setup sets, or add your
|
|
199
|
+
own:
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
import { defineVendor, googleAds, metaPixel } from '@luwiostack/cookie-consent/vendors'
|
|
203
|
+
|
|
204
|
+
googleAds({ cookies: ['_gcl_au', 'IDE'] }) // only these
|
|
205
|
+
metaPixel({ extra: [{ name: 'my_flag', category: 'marketing', max_age: 'session' }] }) // plus yours
|
|
206
|
+
|
|
207
|
+
// Your own site's necessary cookies: never asked about, but they must be declared. List them under
|
|
208
|
+
// `necessary` — the session, CSRF token and this choice's own storage item.
|
|
209
|
+
const mySite = {
|
|
210
|
+
id: 'my_site', // t('my_site'), t('my_site_session') … in your own catalog
|
|
211
|
+
cookies: [
|
|
212
|
+
{ name: 'session', category: 'necessary', max_age: 'session' },
|
|
213
|
+
{ name: 'luwio.consent', category: 'necessary', type: 'local_storage', max_age: 'persistent' },
|
|
214
|
+
],
|
|
215
|
+
} as const
|
|
216
|
+
// createConsent({ categories: { necessary: [mySite], … } })
|
|
217
|
+
|
|
218
|
+
// A service without a preset — declare it, or package it with defineVendor to reuse it, then list
|
|
219
|
+
// it under the categories you use it in:
|
|
220
|
+
const DAY = 24 * 60 * 60 * 1000
|
|
221
|
+
export const acmeChat = defineVendor({
|
|
222
|
+
id: 'acme_chat', // t('acme_chat') → the name
|
|
223
|
+
privacy_policy_url: 'https://acme.example/privacy',
|
|
224
|
+
transfers: [{ country: 'US', safeguards: ['dpf', 'scc'] }], // [] when the data stays in the EEA
|
|
225
|
+
cookies: [
|
|
226
|
+
{ name: 'acme_id', category: 'preferences', max_age: 365 * DAY }, // type defaults to 'http'
|
|
227
|
+
{ name: 'acme_open', category: 'necessary', type: 'session_storage', max_age: 'session' },
|
|
228
|
+
{ name: 'acme_ad', category: 'marketing', max_age: 90 * DAY, domain: '.acme.example' },
|
|
229
|
+
],
|
|
230
|
+
})
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Each cookie has a `category`, a `max_age` and an optional `type` and `domain`:
|
|
234
|
+
|
|
235
|
+
- `max_age` is milliseconds, `'session'` or `'persistent'`.
|
|
236
|
+
- `type` is `http` (the default), `local_storage`, `session_storage`, `indexed_db` or `pixel`.
|
|
237
|
+
- `domain` is the vendor's domain a third-party cookie is set on (`.youtube.com`); leave it out for
|
|
238
|
+
one on your own domain. The resolved cookie carries `party`: `first` or `third`.
|
|
239
|
+
|
|
240
|
+
A vendor's `transfers` lists the countries outside the EEA it sends personal data to, each with its
|
|
241
|
+
safeguards: `adequacy` (an EU adequacy decision), `dpf` (EU-US Data Privacy Framework), `scc`
|
|
242
|
+
(standard contractual clauses) or `bcr` (binding corporate rules). `[]` says the data stays in the
|
|
243
|
+
EEA; leaving it out says nothing. Every preset declares it.
|
|
244
|
+
|
|
245
|
+
`ConsentCookieFormatter.formatLocale(cookie, locale)` renders a lifetime as "2 years" or "3
|
|
246
|
+
months". It returns `null` for `session` and `persistent`: translate those with
|
|
247
|
+
`t(cookie.max_age_machine_name)`.
|
|
248
|
+
|
|
249
|
+
Every text has one translation key, a snake_case `machine_name` the consent instance hands out —
|
|
250
|
+
nothing builds a key itself. The text lives in the downloadable catalogs
|
|
251
|
+
(`translations/cookie-consent.<locale>.json`, in en, nl, fr, de and es):
|
|
252
|
+
|
|
253
|
+
| Render | Key | Example |
|
|
254
|
+
| --- | --- | --- |
|
|
255
|
+
| `t(category.machine_name)` | `consent_category_<id>` | `consent_category_statistics` |
|
|
256
|
+
| `t(category.description_machine_name)` | `consent_category_<id>_description` | `consent_category_statistics_description` |
|
|
257
|
+
| `t(vendor.machine_name)` | the vendor id as a machine name | `google_analytics` |
|
|
258
|
+
| `t(cookie.machine_name)` (its purpose) | the vendor's plus the cookie name, `*` as `wildcard` | `google_analytics_ga`, `google_analytics_ga_wildcard` (`_ga_*`) |
|
|
259
|
+
| `t(cookie.type_machine_name)` | `consent_cookie_type_<type>` | `consent_cookie_type_local_storage` |
|
|
260
|
+
| `t(cookie.max_age_machine_name)` | `consent_max_age_<word>` | `consent_max_age_session` |
|
|
261
|
+
| `t(cookie.party_machine_name)` | `consent_cookie_party_<party>` | `consent_cookie_party_third` |
|
|
262
|
+
| `t(key)` for each of `transfer.safeguard_machine_names` | `consent_transfer_safeguard_<safeguard>` | `consent_transfer_safeguard_dpf` |
|
|
263
|
+
|
|
264
|
+
A transfer's `country` is an ISO code: name it with `new Intl.DisplayNames([locale], { type: 'region' })`.
|
|
265
|
+
|
|
266
|
+
```tsx
|
|
267
|
+
<h3>{t(vendor.machine_name)}</h3>
|
|
268
|
+
{vendor.cookies.map((cookie) => (
|
|
269
|
+
<tr key={cookie.name}>
|
|
270
|
+
<td><code>{cookie.name}</code></td>
|
|
271
|
+
<td>{t(cookie.machine_name)}</td>
|
|
272
|
+
<td>{ConsentCookieFormatter.formatLocale(cookie, locale) ?? t(cookie.max_age_machine_name ?? '')}</td>
|
|
273
|
+
<td>{t(cookie.type_machine_name)}</td>
|
|
274
|
+
</tr>
|
|
275
|
+
))}
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
You declare vendors (`ConsentVendorDeclaration`: `id` and `cookies`, each a
|
|
279
|
+
`ConsentCookieDeclaration`); the machine names are derived. Two cookie names — or two vendor ids —
|
|
280
|
+
that come out as the same machine name throw a `ConsentError`, as does one without letters or digits.
|
|
281
|
+
|
|
282
|
+
### Config from your backend
|
|
283
|
+
|
|
284
|
+
When a back office decides which integrations are on, the config is data. The backend sends JSON
|
|
285
|
+
(`ConsentConfigData`): per category, the vendors used in it (preset ids or full declarations), and
|
|
286
|
+
a policy version.
|
|
287
|
+
`consentConfigFromData` turns it into `createConsent` options:
|
|
288
|
+
|
|
289
|
+
```ts
|
|
290
|
+
import { consentConfigFromData } from '@luwiostack/cookie-consent/vendors'
|
|
291
|
+
|
|
292
|
+
// config.consent from your startup config:
|
|
293
|
+
// { "policy_version": 7, "categories": {
|
|
294
|
+
// "statistics": ["google_analytics"],
|
|
295
|
+
// "preferences": ["youtube"],
|
|
296
|
+
// "marketing": [
|
|
297
|
+
// { "preset": "google_ads", "cookies": ["_gcl_au"] },
|
|
298
|
+
// { "id": "acme_chat", "cookies": [{ "name": "acme_id", "category": "marketing", "max_age": 31536000000 }] }
|
|
299
|
+
// ]
|
|
300
|
+
// } }
|
|
301
|
+
const consent = createConsent({ ...consentConfigFromData(config.consent), onChange: logConsent })
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
**Strictly necessary cookies stay in the app.** The backend decides the optional categories; the
|
|
305
|
+
app declares its own `necessary` vendors in code and passes them as `necessary`. They come first,
|
|
306
|
+
followed by the data's categories; `policy_version` and `max_age` still come from the data:
|
|
307
|
+
|
|
308
|
+
```ts
|
|
309
|
+
// The app's own strictly necessary cookies — never configured in the back office.
|
|
310
|
+
const mySite = {
|
|
311
|
+
id: 'my_site',
|
|
312
|
+
cookies: [
|
|
313
|
+
{ name: 'session', category: 'necessary', max_age: 'session' },
|
|
314
|
+
{ name: 'luwio.consent', category: 'necessary', type: 'local_storage', max_age: 'persistent' },
|
|
315
|
+
],
|
|
316
|
+
} as const
|
|
317
|
+
|
|
318
|
+
const consent = createConsent({
|
|
319
|
+
...consentConfigFromData(config.consent, { necessary: [mySite] }), // + preferences, statistics, marketing
|
|
320
|
+
onChange: logConsent,
|
|
321
|
+
})
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
Once the app passes `necessary`, data that lists a `necessary` category is refused with
|
|
325
|
+
`ConsentError`: strictly necessary cookies are the app's to declare, never the backend's. A backend
|
|
326
|
+
vendor still brings its own necessary cookies (reCAPTCHA's, say) under the category it's listed in.
|
|
327
|
+
|
|
328
|
+
- **Preset lookup:** presets resolve through `vendorPresets`, the id → preset map. It bundles every
|
|
329
|
+
preset, since the backend picks at runtime.
|
|
330
|
+
- **Policy version:** bump `policy_version` on the backend whenever the vendor list changes, so
|
|
331
|
+
visitors are asked again.
|
|
332
|
+
- **Errors:** an unknown category, preset or cookie throws `ConsentError`, and so does a vendor
|
|
333
|
+
listed under a category it has no cookies in, or a `necessary` category in the data when the app
|
|
334
|
+
passes its own.
|
|
335
|
+
|
|
336
|
+
## Outside React
|
|
337
|
+
|
|
338
|
+
```ts
|
|
339
|
+
import { clearRevokedCookies, unblockScripts } from '@luwiostack/cookie-consent'
|
|
340
|
+
|
|
341
|
+
unblockScripts(consent) // activates <script type="text/plain" data-consent="statistics">,
|
|
342
|
+
// plus data-consent-vendor="meta_pixel" to also need that vendor there
|
|
343
|
+
clearRevokedCookies(consent) // removes the cookies / storage of categories that aren't granted
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
`unblockScripts` keeps an activated script's CSP `nonce`, so it works on a site with a nonce-based
|
|
347
|
+
`Content-Security-Policy`: put the nonce on the blocked `<script type="text/plain">` as usual.
|
|
348
|
+
Activation is one-way — a withdrawn category can't un-run a script; reload from `onChange`.
|
|
349
|
+
|
|
350
|
+
### Removing what a withdrawn category left
|
|
351
|
+
|
|
352
|
+
Withdrawing consent should stop the tracking *and* remove what it stored. `clearRevokedCookies`
|
|
353
|
+
does the second: at start and on every change, it deletes the declared cookies and storage items of
|
|
354
|
+
every category that isn't granted — after "withdraw statistics", `_ga` and every `_ga_*` are gone.
|
|
355
|
+
|
|
356
|
+
```ts
|
|
357
|
+
import { clearRevokedCookies } from '@luwiostack/cookie-consent'
|
|
358
|
+
|
|
359
|
+
clearRevokedCookies(consent, {
|
|
360
|
+
// optional: per item removed — log it, or reload so scripts that already run stop
|
|
361
|
+
onClear: (cookie, actualName) => console.info('removed', actualName),
|
|
362
|
+
})
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
- **Declared items only**, matched by the declared name (`*` is a wildcard: `_ga_*` matches
|
|
366
|
+
`_ga_ABC123`). Undeclared cookies are never touched, and neither is `necessary`.
|
|
367
|
+
- **Shared names are kept**: if a granted category also declares a name, it stays.
|
|
368
|
+
- Per `type`: `http` cookies are expired — tried with every parent domain and path, since tags set
|
|
369
|
+
them broadly (GA sets `_ga` on your registrable domain); `local_storage` / `session_storage`
|
|
370
|
+
items are removed; `indexed_db` databases are deleted; a `pixel` stores nothing to remove.
|
|
371
|
+
- **First-party only.** A third-party cookie (`IDE` on doubleclick.net, YouTube's on youtube.com)
|
|
372
|
+
lives on another site — a page can't remove it. `HttpOnly` cookies only your server can.
|
|
373
|
+
- It removes data, it doesn't stop scripts that are already running: reload the page after a
|
|
374
|
+
withdrawal (e.g. from `onClear`) for that.
|
|
375
|
+
- `onClear` reports only what's really gone: a cookie no domain/path variant could reach isn't
|
|
376
|
+
reported, nor an IndexedDB database in a browser that can't list them (it's deleted by exact name).
|
|
377
|
+
|
|
378
|
+
## Compliance checklist
|
|
379
|
+
|
|
380
|
+
The four categories are granular enough for GDPR / ePrivacy; what regulators check is how the
|
|
381
|
+
choice is collected. The package covers prior blocking (`ConsentGate`, `unblockScripts`), removal on withdrawal (`clearRevokedCookies`), the cookie declaration,
|
|
382
|
+
re-asking (`policyVersion`, `maxAge`) and the data for a consent log (`onChange`). Your app covers
|
|
383
|
+
the rest:
|
|
384
|
+
|
|
385
|
+
- Reject all next to Accept all on the first layer, with the same weight.
|
|
386
|
+
- No pre-ticked boxes: start the preferences from `isGranted`.
|
|
387
|
+
- A "Cookie settings" link on every page, and a reload when something is withdrawn.
|
|
388
|
+
- Store each decision server-side (`consent_id`, version, time, granted categories).
|
|
389
|
+
- Declare what your setup really sets, checked against a cookie scan; only truly necessary cookies
|
|
390
|
+
under `necessary`. Bump `policyVersion` whenever a vendor is added or moved.
|
|
391
|
+
|
|
392
|
+
Watch out for: Google-served ads (AdSense, Ad Manager, AdMob) in the EEA / UK need a
|
|
393
|
+
Google-certified TCF CMP, which this isn't; the CNIL recommends re-asking after about 6 months
|
|
394
|
+
(set `maxAge`); California expects a Global Privacy Control signal to count as refusing marketing
|
|
395
|
+
(not read by the package); name the countries and safeguards of each vendor's `transfers` in your
|
|
396
|
+
cookie declaration and privacy policy. Guidance, not legal advice.
|
|
397
|
+
|
|
398
|
+
## API
|
|
399
|
+
|
|
400
|
+
| | |
|
|
401
|
+
| --- | --- |
|
|
402
|
+
| `createConsent({ categories?, policyVersion?, maxAge?, store?, now?, onChange? })` | The consent instance; `categories` maps each fixed category to the vendors used in it. |
|
|
403
|
+
| `CONSENT_CATEGORIES` · `ConsentCategoryId` | `necessary`, `preferences`, `statistics`, `marketing`. |
|
|
404
|
+
| `consent.categories` · `consent.vendors` | The four categories with their vendors (cookies narrowed per category); every vendor in use once. Each with its `machine_name`s. |
|
|
405
|
+
| `ConsentVendorDeclaration` · `ConsentCookieDeclaration` · `ConsentTransferDeclaration` | What you declare: `{ id, privacy_policy_url?, transfers?, cookies }`, `{ name, category, type?, max_age, domain? }`, `{ country, safeguards }`. |
|
|
406
|
+
| `ConsentCategory` · `ConsentVendor` · `ConsentCookie` · `ConsentTransfer` | What you render: the same plus `machine_name` (and `description_machine_name`, `type_machine_name`, `party`, `party_machine_name`, `max_age_machine_name?`, `safeguard_machine_names`). |
|
|
407
|
+
| `consent.getSnapshot()` | `{ status: 'pending' \| 'decided', decision }`. |
|
|
408
|
+
| `consent.isGranted(category, vendorId?)` / `isRequired(category)` | `necessary` is always granted. With a vendor, also whether it's used in that category. |
|
|
409
|
+
| `consent.acceptAll()` / `rejectAll()` / `save(ids)` / `grant(...ids)` / `revoke(...ids)` / `reset()` | Make (or forget) a decision. |
|
|
410
|
+
| `ConsentDecision` | `consent_id`, `policy_version`, `decided_at`, `granted()`, `isGranted(id)`, `toJSON()`. |
|
|
411
|
+
| `unblockScripts(consent, options?)` | Activate blocked scripts / `data-src` embeds. |
|
|
412
|
+
| `clearRevokedCookies(consent, { onClear? })` | Remove declared first-party cookies and storage of every category that isn't granted. |
|
|
413
|
+
| `ConsentCookieFormatter.formatLocale(cookie, locale)` | A lifetime as "2 years" / "3 months"; `null` for session / persistent. |
|
|
414
|
+
| `@luwiostack/cookie-consent/vendors` | 10 presets, `defineVendor`, `vendorPresets`, `consentConfigFromData(data, { necessary? })`. |
|
|
415
|
+
| `<Consent consent>` · `useConsent()` | Provider and reactive hook. |
|
|
416
|
+
| `<ConsentGate category vendor? fallback>` | Children only once granted; `fallback(api)` gets `missing`, `grant()`, `openPreferences()`. With a vendor, renders nothing when it isn't used in the category. |
|