Locale cookie honours consent saved under a bumped policy version

The proxy parsed the consent cookie as version 1, so after an editor
bumped the policy version NEXT_LOCALE was never saved. It now accepts the
stored record's own version; policyVersion pins one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-10-22 17:35:08 +02:00
co-authored by Claude Opus 5.5
parent 0fd57c1d6a
commit 7d835bdc49
3 changed files with 57 additions and 1 deletions
+2
View File
@@ -49,6 +49,8 @@ type CreateProxyOptions = {
* requested with `Accept: text/markdown` is rewritten to its Markdown version.
*/
markdown?: boolean | string
/** Consent policy version to require for saving the locale cookie (createLocaleMiddleware). */
policyVersion?: number
}
/**
+21 -1
View File
@@ -37,6 +37,13 @@ type CreateLocaleMiddlewareArgs = {
consentCookieName?: string
/** Cookie name for the locale choice. Defaults to LOCALE_COOKIE_NAME. */
cookieName?: string
/**
* Cookie Settings → Policy version to require. The proxy can't read the
* panel, so by default a stored consent counts under its OWN version —
* reading it as version 1 meant that after an editor bumped the policy the
* locale cookie was never saved again. Pin it to require that version.
*/
policyVersion?: number
}
/**
@@ -74,17 +81,30 @@ function firstSegment(pathname: string): string {
* return res
* }
*/
/** The policy version a consent cookie was saved under (undefined when unreadable). */
function storedVersion(raw: string | undefined): number | undefined {
if (!raw) {return undefined}
try {
const version = (JSON.parse(decodeURIComponent(raw)) as { version?: unknown }).version
return typeof version === 'number' ? version : undefined
} catch {
return undefined
}
}
export function createLocaleMiddleware({
config,
consentCategory = 'functional',
consentCookieName = CONSENT_COOKIE,
cookieName = LOCALE_COOKIE_NAME,
policyVersion,
}: CreateLocaleMiddlewareArgs) {
// Whether the locale cookie may be written: 'necessary' is always granted;
// 'functional' (default) requires the visitor to have consented.
function mayPersistLocale(request: MiddlewareRequest): boolean {
if (consentCategory === 'necessary') {return true}
const consent = parseConsent(request.cookies.get(consentCookieName)?.value)
const raw = request.cookies.get(consentCookieName)?.value
const consent = parseConsent(raw, policyVersion ?? storedVersion(raw))
return consent?.[consentCategory] === true
}