Consent engine
Content from another party, like an embedded video, sends the IP address of the visitor to that party and can set cookies. The Consent engine remembers per party whether the visitor allows it.
- What it does
- Remembers per party whether the visitor allows content from that party, and loads it only after a clear yes.
- Exported from the package?
- Yes. Import
hasConsent,getConsents,giveConsent,revokeConsentandCONSENT_PROVIDERSfrom@davidnet-net/svelte-ui.
Always in the
consent_cache cookie on .davidnet.net (365 days), which every Davidnet app reads. That is all there is when no one is signed in. Signed in, the answer is also saved in the account, so it follows the person to every device, and when the session starts the account wins over the cookie. Never in local storage.Which one do I use?
| What I need | Use |
|---|---|
| Check whether I may load content | hasConsent |
| List what is allowed | getConsents |
| Record a yes | giveConsent |
| Record a no | revokeConsent |
hasConsent
const CONSENT_PROVIDERS = ["youtube"] as const;
type ConsentProvider = (typeof CONSENT_PROVIDERS)[number];
function getConsents(): ConsentProvider[]
function hasConsent(provider: ConsentProvider): booleanhasConsent(provider) says whether content of that party may be loaded. It is reactive: a template that reads it updates when the answer changes. The only party now is "youtube".
When to use
Use it before you load anything from another party. Show a place to ask first, and load the content when hasConsent is true. The YoutubeEmbed component does exactly this.
When not to use, and what to use instead
Content from our own servers needs no consent. Do not use it to remember other choices: it is for the loading of content from other parties only.
Live example
| Call | Result |
|---|---|
| hasConsent("youtube") | false |
| getConsents() | none |
import { giveConsent, hasConsent } from "@davidnet-net/svelte-ui";
{#if hasConsent("youtube")}
<iframe src={videoUrl} title={title}></iframe>
{:else}
<button onclick={() => giveConsent("youtube")}>Load the video</button>
{/if}giveConsent
function giveConsent(provider: ConsentProvider): Promise<void>
function revokeConsent(provider: ConsentProvider): Promise<void>giveConsent(provider) allows the party, now and the next time. revokeConsent(provider) takes it back, and the content asks again. Both update the state and the cookie at once, and when someone is signed in they also save the full list in the account. If saving fails, the local answer stays, and the account decides again at the next sync. getConsents() lists the allowed parties.
When to use
Call giveConsent only from a clear action of the visitor, like a button that says what will happen. revokeConsent is already offered in the Privacy tab of the settings in every app that uses AppShell.
When not to use, and what to use instead
Never call giveConsent yourself on load, or because someone kept scrolling. That is not consent.
Live example
consent_cache= You are signed out: the answer is only in the cookie of this browser.CONSENT_PROVIDERS in the engine and give it a label in the Privacy tab (the type check points to the place). The backend needs the id in its own list of allowed ids too.<Button onclick={() => giveConsent("youtube")}>Allow YouTube</Button>
<Button onclick={() => revokeConsent("youtube")}>Take it back</Button>