Skip to content

Client: unconditional pagehide flush for consent queue drain #16

@tyssejc

Description

@tyssejc

Problem

The client package's visibilitychange listener (which calls collector.flush() on page unload) is gated on config.beaconUrl being set. If no beacon URL is configured, the listener is never registered and buffered/queued events are lost on navigation.

Core's flush() now drains the consent queue for permitted events (#5 / PR #12), but the client needs to actually call it on unload for this to matter.

Proposed Change

Register a pagehide listener unconditionally in the client that calls collector.flush(). The beacon-specific logic can remain gated on beaconUrl, but the basic flush should always happen.

// Always flush on page hide — ensures buffer and consent queue are drained
if (typeof document !== "undefined") {
  const pagehideHandler = () => {
    collector.flush();
  };
  document.addEventListener("visibilitychange", () => {
    if (document.visibilityState === "hidden") pagehideHandler();
  });
  cleanupFns.push(() => document.removeEventListener("visibilitychange", ...));
}

Context

Follow-up from #5 (consent queue guardrails). Core-side flush() drain was implemented in PR #12, but the client doesn't always call it on unload. See also: agent-os/standards/core/consent-timing-heuristics.md (SPA vs MPA section).

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions