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:
@@ -97,6 +97,7 @@ export class CookieBannerClient {
|
||||
created_at: "",
|
||||
};
|
||||
this.activate(cookie.data);
|
||||
void flush(this.bannerId);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -82,15 +82,25 @@ export async function flush(bannerId: string): Promise<void> {
|
||||
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)),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user