Files
ipal-kit/src/modules/consent/consentStore.ts
T
Radosław SmolińskiandClaude Fable 5.2 177620da57 Cookie banner is part of the first render: in the server HTML, decided before the first paint; pages stay static
Until now CookieBanner rendered nothing until the page had hydrated and an effect had read the cookie, so on a first
visit it appeared late and became the LCP element on text pages (request from a client project, 2026-11-26).

- The banner is rendered on the server with `hidden`, followed by one inline script (consentBannerScript) that,
  before the browser paints, shows it (no current decision) or keeps it hidden (current decision). It decides as
  readConsentRecord does. A click on a banner button before hydration is kept and carried out once the page is live.
- The decision is read through a store (useSyncExternalStore): no hydration mismatch, the browser's value before
  the next paint. One decision per document.
- Optional server read (ConsentProvider cookieValue, SiteProviders consentCookie): exact HTML, dynamic pages.
- CookieBanner firstPaint={false} keeps the old behaviour; nonce from SiteProviders / ConsentProvider / the prop.
- Nothing about consent changes: no tag earlier, Consent Mode default first, cookie, consent log, withdrawal.

Tests: test/consentFirstPaint.test.ts (server render, script against readConsentRecord, hydration with no warning,
decisions, tags), reference e2e (static HTML, painted before hydration, returning visitor, early click).
Docs: consent.md "First paint", frontend.md, security.md, AGENT.md, skills, changelog draft under 1.17.0.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-26 12:08:59 +01:00

56 lines
2.2 KiB
TypeScript

import { CONSENT_COOKIE, readConsentRecord } from './storage.js'
/**
* The stored consent decision as an external store (React `useSyncExternalStore`).
*
* Why a store and not state filled in an effect: with a store React hydrates with the server's value and switches to
* the browser's own before the browser paints, so the banner and the button can be part of the server HTML with no
* hydration mismatch and no frame of the wrong one. One decision per document: every <ConsentProvider> on a page
* reads the same value.
*/
export type ConsentRecord = NonNullable<ReturnType<typeof readConsentRecord>>
const listeners = new Set<() => void>()
/** The value last written in this document — still the decision when the browser refuses to keep the cookie. */
let written: string | undefined
let cache: { key: string; record: ConsentRecord | null } | undefined
/** Raw value of the consent cookie in this browser (first cookie of that name), undefined when there is none. */
export function readConsentCookie(): string | undefined {
if (typeof document === 'undefined') {return undefined}
return document.cookie
.split('; ')
.find((c) => c.startsWith(`${CONSENT_COOKIE}=`))
?.split('=')[1]
}
/** The current decision under `version`, or null. The same object until the stored value changes. */
export function getConsentSnapshot(version: number): ConsentRecord | null {
const raw = readConsentCookie()
const key = `${version}\n${raw ?? ''}\n${written ?? ''}`
if (cache?.key !== key) {
cache = { key, record: readConsentRecord(raw, version) ?? readConsentRecord(written, version) }
}
return cache.record
}
export function subscribeConsent(listener: () => void): () => void {
listeners.add(listener)
return () => {
listeners.delete(listener)
}
}
/** Writes the cookie (`value` already serialized, `attributes` like `;path=/;max-age=…`) and tells every reader. */
export function writeConsentCookie(value: string, attributes: string): void {
document.cookie = `${CONSENT_COOKIE}=${value}${attributes}`
written = value
for (const listener of listeners) {listener()}
}
/** Tests only: forget what this document wrote. */
export function resetConsentStore(): void {
written = undefined
cache = undefined
}