Fix queue flush race condition and missing flush on cookie path

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é <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-04-16 13:14:49 +04:00
parent 7d05d43f85
commit eafca11c82
2 changed files with 14 additions and 3 deletions

View File

@@ -97,6 +97,7 @@ export class CookieBannerClient {
created_at: "", created_at: "",
}; };
this.activate(cookie.data); this.activate(cookie.data);
void flush(this.bannerId);
return; return;
} }

View File

@@ -82,15 +82,25 @@ export async function flush(bannerId: string): Promise<void> {
return; return;
} }
const remaining: PendingConsent[] = []; const sentTimestamps: number[] = [];
for (const entry of queue) { for (const entry of queue) {
try { try {
await fetchJSON(entry.url, { method: "POST", body: entry.body }); await fetchJSON(entry.url, { method: "POST", body: entry.body });
sentTimestamps.push(entry.timestamp);
} catch { } 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)),
);
} }