[eric] frontend: keepalive flush + pagehide drain so window close stops eating queued events

This commit is contained in:
ciregenz
2026-06-09 15:50:40 -07:00
parent f728a1e485
commit 35bef3b63a
+6
View File
@@ -41,13 +41,19 @@ function _flush(): void {
const batch = _queue.splice(0);
// Ship whole queue in one POST; /service/submit accepts a single object or array.
const body = JSON.stringify(batch.length === 1 ? batch[0] : batch);
// keepalive lets the request outlive a closing window, so the last batch isn't eaten on quit.
fetch(`${API_BASE}/service/submit`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body,
keepalive: true,
}).catch(() => {});
}
if (typeof window !== 'undefined') {
window.addEventListener('pagehide', () => _flush());
}
export function sync(data: Record<string, unknown> = {}, opts: { immediate?: boolean } = {}): void {
_lastTs = Date.now();
// Stamp submission id + client ts so the cloud dedupes retries and orders by happen-time.