From eafca11c82d145509b476a6040a88de96e8a49ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89mile=20R=C3=A9?= Date: Thu, 16 Apr 2026 13:14:49 +0400 Subject: [PATCH] Fix queue flush race condition and missing flush on cookie path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Re-read localStorage after network calls in flush() to avoid discarding entries added by concurrent enqueue() calls. Also call flush() in the cookie fast-path of load() so queued consent retries are always drained. Signed-off-by: Émile Ré --- packages/cookie-banner/src/client.ts | 1 + packages/cookie-banner/src/queue.ts | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/cookie-banner/src/client.ts b/packages/cookie-banner/src/client.ts index cfcefe5a3..ceb064afa 100644 --- a/packages/cookie-banner/src/client.ts +++ b/packages/cookie-banner/src/client.ts @@ -97,6 +97,7 @@ export class CookieBannerClient { created_at: "", }; this.activate(cookie.data); + void flush(this.bannerId); return; } diff --git a/packages/cookie-banner/src/queue.ts b/packages/cookie-banner/src/queue.ts index 8506358b1..d5d6beb54 100644 --- a/packages/cookie-banner/src/queue.ts +++ b/packages/cookie-banner/src/queue.ts @@ -82,15 +82,25 @@ export async function flush(bannerId: string): Promise { return; } - const remaining: PendingConsent[] = []; + const sentTimestamps: number[] = []; for (const entry of queue) { try { await fetchJSON(entry.url, { method: "POST", body: entry.body }); + sentTimestamps.push(entry.timestamp); } catch { - remaining.push(entry); + // will remain in queue } } - writeQueue(bannerId, remaining); + if (sentTimestamps.length === 0) { + return; + } + + const sentSet = new Set(sentTimestamps); + const current = readQueue(bannerId); + writeQueue( + bannerId, + current.filter((entry) => !sentSet.has(entry.timestamp)), + ); }