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
+81 -4
View File
@@ -82,9 +82,11 @@ export default async function Layout({ children, params }) {
`ConsentProvider` — props: `texts` (required, `ConsentTexts` from `getConsentTexts`),
`cookieMap` (extra cookies to delete on withdrawal), `logEndpoint`
(consent log). `CookieBanner` does not render after a decision, `CookieButton` —
the opposite (it shows only after a decision and reopens the banner); both
render only after mounting on the client.
(consent log), `nonce` (CSP nonce for the banner's first-paint script), `cookieValue` (optional
server-read cookie — [First paint](#first-paint-the-banner-is-part-of-the-first-render-1170)).
`CookieBanner` does not render after a decision, `CookieButton` —
the opposite (it shows only after a decision and reopens the banner). Since 1.17.0 the banner is in
the server HTML and painted with the page, not after hydration (next section).
Built-in banner texts follow the page language: `consentFallback(locale)` → `CONSENT_FALLBACK_PL` on `pl`,
`CONSENT_FALLBACK` (English) otherwise; panel values always win. Category names for your own selects:
@@ -99,6 +101,8 @@ takes `locale` and optional `labels`; defaults: `COOKIE_TABLE_LABELS` (`@intecio
|---|---|---|
| `classNames` | — | slots (table below) — a given one replaces the default |
| `equalChoice` | `false` | "Reject" styled as the primary button |
| `firstPaint` | `true` | banner in the server HTML, decided before the first paint; `false` = nothing until the page has hydrated (as before 1.17.0) |
| `nonce` | provider's | CSP nonce for the first-paint script; default: the one given to `ConsentProvider` / `SiteProviders` |
| `icon` | `true` | cookie icon |
| `order` | `['settings', 'reject', 'accept']` | banner buttons |
| `settings` | `'inline'` | `'inline' \| 'modal' \| 'detailed'` |
@@ -112,6 +116,78 @@ moves up by its visible part (`bottomOverlap`), so it never covers footer links.
`--ipal-cookie-button-offset` (from the bottom, default `1rem`), `--ipal-cookie-button-inset` (from the
side, default `1rem`); z-index `--ipal-z-header`. Type `CookieButtonProps`.
## First paint: the banner is part of the first render (1.17.0)
Before 1.17.0 the banner rendered nothing until React had hydrated the page and an effect had read the
cookie. On a first visit its text appeared seconds after the page (measured on the reference site,
mobile profile with 4x CPU and slow 4G: page painted at 1.0 s, banner at 2.7 s) and on a text page it
became the **LCP element**. Now it is in the HTML and painted with the page (banner at 1.0 s, LCP 1.0 s).
An existing site gets this by updating: no edit, pages stay static.
**How (default, pages stay static / ISR / CDN-cached).** The HTML is the same for every visitor, so it
cannot know the visitor's cookie. The server renders the banner with the `hidden` attribute and, right
after it, one small inline script (`consentBannerScript`, about 0.6 kB). The script runs while the HTML is
parsed, before the browser paints:
| Visitor | Before the first paint | After hydration |
|---|---|---|
| no decision; decision under an older **Policy version**; unreadable or foreign `cookie-consent` | `hidden` removed → banner painted with the page | the same element becomes live (no re-render, no shift: the bar is `position: fixed`) |
| current decision | `display: none` on the banner → never painted | React removes it; `CookieButton` appears |
| JavaScript off, or a CSP refused the script | banner stays hidden | with JS: appears after hydration, as before 1.17.0; without JS: no banner (and no tag: every tag is loaded by JS) |
The script decides exactly as `readConsentRecord` does, reads only the consent cookie, stores nothing and
loads nothing. A click on a banner button **before** hydration is not lost: the script notes the button
(`data-ipal-pending`), and the banner carries the click out when it becomes live (the decision, the cookie
and the consent log entry are made then, by the same code as any click). There is no hydration warning:
React hydrates with "no decision" (what the server rendered) and switches to the browser's own value
before the next paint (`useSyncExternalStore`).
What does **not** change: what counts as consent, when tags load (nothing earlier), the Consent Mode
default (still pushed first), the cookie (`cookie-consent`, `{ id, state, timestamp, version }`), the
consent log, withdrawal, `CookieButton`, the settings window in all three modes, texts per language.
**CSP.** With the header-only policy (`buildSecurityHeaders`, `'unsafe-inline'` for scripts — what
static pages need anyway) nothing is to be done. With a nonce policy (`createProxy({ csp })`) the script
needs the nonce: `<SiteProviders nonce={nonce}>` passes it on (a site that already passes `nonce` for the
theme script is done); by hand: `<ConsentProvider nonce={nonce}>` or `<CookieBanner nonce={nonce}>`.
Without the nonce the script is refused and the banner appears after hydration, as before.
**Your own `classNames.root`.** The banner is hidden with the `hidden` attribute. Tailwind 4 makes
`[hidden]` win over every utility; with Tailwind 3 (or other CSS) a root class that sets `display`
(`flex`, `grid`) overrides it: add `[data-ipal="banner"][hidden] { display: none !important }` to the
site's CSS.
**Turn it off:** `<CookieBanner firstPaint={false} />` — nothing in the HTML, the banner appears after
hydration (before 1.17.0).
**Option: read the cookie on the server (exact HTML, dynamic pages).** Pass the request's cookie and the
server render already knows the decision — no banner in the HTML for a visitor who decided (the
`CookieButton` instead), the banner without `hidden` and without a script for one who has not:
```tsx
import { CONSENT_COOKIE } from '@intecion/ipal-kit'
import { cookies } from 'next/headers'
const consentCookie = (await cookies()).get(CONSENT_COOKIE)?.value ?? null // null = read, none sent
<SiteProviders shell={shell} consentCookie={consentCookie}>… // by hand: <ConsentProvider cookieValue={consentCookie}>
```
The price: `cookies()` makes every page under that layout **dynamic** — rendered per request, no static
or ISR copy, and a CDN must not share one HTML between visitors. Choose it only for a site that is
dynamic anyway (a nonce CSP, signed-in pages). `undefined` (the prop left out) = static mode.
| | Static (default) | Server read (`consentCookie`) |
|---|---|---|
| Public pages | static / ISR, one HTML for all, CDN-cacheable | dynamic, per request |
| Banner in the HTML of a visitor who decided | yes, hidden before paint | no |
| Inline script | one (needs the nonce under a nonce CSP) | none |
| Banner at first paint for a new visitor | yes | yes |
**Your own banner** (AGENT.md §7 says: don't; when you must): `consentBannerScript({ version })` from
`@intecion/ipal-kit` (options: `ConsentBannerScriptOptions` — `version`, `cookieName`) is the same script — put `hidden` and `suppressHydrationWarning` on your banner's root and
the script in a `<script>` as its next sibling; `useConsentContext()` gives `hydrated` (false in the
server render and while hydrating) and `serverRead`.
## Overriding the look (Level 2)
The default Tailwind classes can be overridden with `classNames`:
@@ -292,7 +368,8 @@ Texts from the panel (**Cookies**, per language) — empty field = the element i
New fields in the global = a migration in projects on Postgres/SQLite (`payload migrate:create`); MongoDB unchanged.
The elements also have stable `data-ipal` handles for CSS and e2e tests (used by
`iapl-consent-check`): `banner`, `cookie-button` and the buttons `settings`, `reject`,
`iapl-consent-check`): `banner` (while it waits for the first-paint script it carries `hidden`; a button clicked before hydration:
`data-ipal-pending`; live: `data-ipal-ready`), `banner-script`, `cookie-button` and the buttons `settings`, `reject`,
`accept`, `back`, `save`; in modal mode also `consent-overlay`, `consent-dialog`, `close`; in detailed mode
additionally `category-<category>` (card) and `cookies` (expandable list).