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
+2 -1
View File
@@ -178,7 +178,7 @@ export const proxy = createProxy(i18nConfig, {
// app/(frontend)/[locale]/layout.tsx
import { headers } from 'next/headers'
const nonce = (await headers()).get('x-nonce') // CSP_NONCE_HEADER
<SiteProviders shell={shell} theme nonce={nonce}>… // ThemeScript + GA4/GTM tag get the nonce
<SiteProviders shell={shell} theme nonce={nonce}>… // ThemeScript, the banner's first-paint script + GA4/GTM tag get the nonce
```
Costs and rules:
@@ -190,6 +190,7 @@ Costs and rules:
- Your own proxy: `createCspNonce()` + `buildCsp({ nonce })`, set the policy on the request headers
(`NextResponse.next({ request: { headers } })`) and on the response, like `createProxy` does.
- `ConsentScript nonce`, `ThemeScript nonce`, `Analytics nonce` exist for policies without `'strict-dynamic'`.
- The cookie banner's first-paint script (consent.md → "First paint") is inline, so under a nonce policy it needs the nonce with or without `'strict-dynamic'`: `SiteProviders nonce` passes it (`ConsentProvider nonce` / `CookieBanner nonce` by hand). Refused, the banner appears after hydration.
### Payload admin — its own, looser policy