Shows the site's cookie consent banner and preferences panel, stores the
visitor's answer in localStorage for 365 days, logs it, and injects the
site's third-party scripts for the accepted categories. Available as
lynkow.consent. In a browser, createClient() shows the banner for you
until the visitor answers. UI methods are no-ops on the server.
Access via: lynkow.consent
Methods
12 methods
acceptAll
acceptAll(): voidAccepts every category the site defines: stores and logs the choice as
accept_all, injects the scripts of every category, emits
'consent-changed' and hides the banner. No-op on the server.
Returns: void
// Wire up a one-click "Accept All" button in your own UI
document.getElementById('accept-cookies').addEventListener('click', () => {
lynkow.consent.acceptAll()
})destroy
destroy(): voidRemoves the banner, the preferences panel and the injected script
elements, and stops their listeners. A show() or showPreferences()
still waiting for the configuration is abandoned. Both work again
afterwards, as a remounted component needs. Stored answers are kept.
Returns: void
useEffect(() => {
lynkow.consent.show()
return () => lynkow.consent.destroy()
}, [])getCategories
getCategories(): ConsentCategoriesReturns the visitor's current consent categories, including any category the site defines beyond the four this type names.
When no consent has been stored yet, the answer is what the site's
consentMode says is granted before anyone answers: only the required
categories under 'opt-in', everything under 'opt-out' and
'notice-only'. On the server, always returns the 'opt-in' defaults,
because no stored choice is readable there.
Returns: ConsentCategories
// Read current preferences to conditionally load a third-party widget
const prefs = lynkow.consent.getCategories()
if (prefs.marketing) {
loadChatWidget()
}getConfig
getConfig(options?: BaseRequestOptions): Promise<CookieConfig>Fetches the site's cookie consent configuration, the one the banner
renders, on the server or in the browser. Kept in memory per locale for
the life of the client, whatever the cache option, and concurrent calls
share one request. A call with fetchOptions always fetches and stores
nothing. The banner reuses what this returns.
Parameter | Type | Description |
|---|---|---|
|
|
|
Returns: Promise<CookieConfig>
const config = await lynkow.consent.getConfig({
fetchOptions: { next: { revalidate: 3600 } },
})
if (config.enabled) {
console.log('Categories:', config.categories.map((c) => c.id))
}hasConsented
hasConsented(): booleanChecks whether the user has already made a consent choice (accepted,
rejected, or customized). Returns false if no consent is stored or
if the stored consent has expired (after 365 days).
Returns: boolean
// Only show the consent banner if the user hasn't already decided
if (!lynkow.consent.hasConsented()) {
lynkow.consent.show()
}hide
hide(): voidHide and remove the consent banner from the DOM. Does not affect
stored consent preferences (the user will not be re-prompted on the
next page load if they already chose). Removes only the banner this
client mounted, and cancels a show() of this client still waiting for
the configuration. No-op on server.
Returns: void
// Close the banner after the user clicked "Accept all" via custom UI:
lynkow.consent.hide()logConsent
logConsent(preferences: CookiePreferences, action?: ConsentAction): Promise<void>Records a consent choice in the site's consent log, with the visitor id
stored under _lkw_vid in localStorage, the page URL and the locale the
banner config was fetched in. The banner methods call it for you. Never
rejects: every error is ignored. Outside a browser the choice is logged
under the visitor id server; use cookies.logConsent() with your own
visitorId there.
Parameter | Type | Description |
|---|---|---|
|
| Category id to accepted flag, for example |
|
| Action to record. Inferred when omitted; pass |
Returns: Promise<void>
await lynkow.consent.logConsent(
{ necessary: true, analytics: true, marketing: false },
'customize'
)rejectAll
rejectAll(): voidRejects every optional cookie category. Categories the site marked
required stay true. Saves the choice to localStorage, logs consent to
the server, and hides the banner. No-op on server.
When this takes back a category that was granted, it is logged as
'withdraw' rather than 'reject_all' and the scripts injected for that
category are removed from the page. Removing them is not an undo: see
setCategories.
Returns: void
// Reject all non-essential cookies from a custom banner
document.getElementById('reject-cookies').addEventListener('click', () => {
lynkow.consent.rejectAll()
})reset
reset(): voidForgets the visitor's stored answer, removes the injected script elements,
emits 'consent-changed' with the optional categories off, and shows the
banner again, even under showOnFirstVisit: false. Nothing is logged.
No-op on the server.
Returns: void
// Add a "Manage cookies" link in the footer to let users re-choose
document.getElementById('manage-cookies').addEventListener('click', () => {
lynkow.consent.reset()
})setCategories
setCategories(categories: Partial<ConsentCategories>): voidUpdates the given categories and keeps the others as they are.
necessary and every category the site marks required stay true.
Stores and logs the choice as customize, injects the scripts of accepted
categories and emits 'consent-changed'. Taking back a granted category
logs withdraw and removes its script elements, but code those scripts
already ran keeps running: reload the page to stop it. No-op on the server.
Parameter | Type | Description |
|---|---|---|
|
| Categories to change, for example |
Returns: void
// Save custom preferences from a preferences form
lynkow.consent.setCategories({
analytics: true,
marketing: false,
preferences: true,
})show
show(options?: ConsentShowOptions): voidShows the consent banner, styled from the site's configuration.
createClient() already calls it in a browser. Does nothing when the
site's banner is disabled. When the visitor already answered, it injects
the accepted categories' scripts instead. Before an answer, 'opt-in'
injects nothing, while 'opt-out' and 'notice-only' inject every
category at once; 'notice-only' shows a single acknowledge button.
showOnFirstVisit: false hides the banner unless force is set. Only one
banner is shown per page: nothing is added while one from this or another
client is on screen, and it is removed once the visitor answers through
any client. Never throws; a failed config fetch shows nothing. No-op on
the server.
Parameter | Type | Description |
|---|---|---|
|
|
|
Returns: void
// A "Cookie settings" link on a site that does not show the banner by itself
lynkow.consent.show({ force: true })showPreferences
showPreferences(): voidOpens the preferences panel, where the visitor switches each optional
category on or off, starting from their current answer. Saving applies the
choice like setCategories and closes the banner. Under 'notice-only' the panel
only lists the categories and closes without recording anything. No-op on
the server or while a panel from this or another client is open.
Returns: void
<button onClick={() => lynkow.consent.showPreferences()}>
Cookie settings
</button>