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]>
This commit is contained in:
Radosław Smoliński
2026-11-26 12:08:59 +01:00
co-authored by Claude Fable 5.2
parent 2f6a81acbf
commit 177620da57
28 changed files with 983 additions and 76 deletions
+11 -3
View File
@@ -23,6 +23,12 @@ export type SiteProvidersProps = {
/** The cookie banner. Default `<CookieBanner />`; pass your own configured one (`equalChoice`, classNames) or `false`. */
banner?: false | ReactNode
children: ReactNode
/**
* Optional server read of the consent cookie: `(await cookies()).get(CONSENT_COOKIE)?.value ?? null`. The HTML
* then has no banner for a visitor who already decided, and the page becomes dynamic (no static / ISR copy).
* Leave out (default) to keep pages static: the banner decides before the first paint with an inline script.
*/
consentCookie?: null | string
/** The "cookie settings" button. Default `<CookieButton />`; your own or `false`. */
cookieButton?: false | ReactNode
/** Extra cookies per category to delete when consent is withdrawn. */
@@ -41,8 +47,9 @@ export type SiteProvidersProps = {
logEndpoint?: string
/**
* CSP nonce for a nonce-based policy (createProxy `csp`): read it in the layout
* with `(await headers()).get('x-nonce')`. Goes on the theme script and on the
* analytics tags the plugin injects (GTM passes it on to its own tags).
* with `(await headers()).get('x-nonce')`. Goes on the theme script, the cookie
* banner's first-paint script and the analytics tags the plugin injects (GTM
* passes it on to its own tags).
*/
nonce?: null | string
/** <link rel=preconnect> to the media origin (R2_PUBLIC_URL). Default true. */
@@ -82,6 +89,7 @@ export function SiteProviders({
analytics,
banner,
children,
consentCookie,
cookieButton,
cookieMap,
jsonLd = [],
@@ -98,7 +106,7 @@ export function SiteProviders({
{theme ? <ThemeScript {...shell.theme} nonce={nonce ?? undefined} /> : null}
{preconnect ? <MediaPreconnect /> : null}
<JsonLdGraph schemas={[...shell.jsonLd, ...jsonLd]} />
<ConsentProvider cookieMap={cookieMap} logEndpoint={logEndpoint} texts={shell.consent}>
<ConsentProvider cookieMap={cookieMap} cookieValue={consentCookie} logEndpoint={logEndpoint} nonce={nonce} texts={shell.consent}>
<TurnstileProvider siteKey={shell.turnstileSiteKey}>
{lightbox ? (
<LightboxProvider texts={shell.lightboxTexts} {...(lightbox === true ? {} : lightbox)}>