# Async Clipboard API and User Activation vs Beacon API and visibilitychange Lifecycle

- **Canonical URL:** https://frontend.rocks/vs/async-clipboard-activation-vs-beacon-api-visibilitychange/
- **Relation:** Contrasts With
- **Last Verified:** 2026-11-15
- **Verdict:** sendBeacon dispatches background requests without user gesture, whereas Async Clipboard requires transient user activation.

## Comparison Matrix

| Dimension | Async Clipboard API and User Activation | Beacon API and visibilitychange Lifecycle |
| :--- | :--- | :--- |
| **Maturity** | Baseline · Widely | Baseline · Widely |
| **Domain** | Web APIs | Web APIs |
| **Mental Model** | Promise-based OS clipboard access gated by HTTPS, document focus, and transient user activation tokens, supporting deferred Blob resolution via ClipboardItem. | Prevent dropped analytics and broken bfcache on tab close by handing <= 64 KB POST payloads to the browser network process on visibilitychange. |
| **Prerequisites** | DOM Event Propagation (Capture, Target, Bubble) (Transient user activation is minted by trusted input events dispatching through the DOM.) | Back/Forward Cache (bfcache) (Using visibilitychange and pagehide instead of unload keeps pages eligible for bfcache.) |
| **When Not to Use** | When not to use navigator.clipboard.readText(): Never poll clipboard contents on focus; it triggers intrusive browser permission prompts and erodes user trust. | When not to use sendBeacon(): If your code needs the HTTP response status, custom Authorization headers, or payloads > 64 KB, use fetch(..., { keepalive: true }) (for headers/status within 64 KB) or flush eagerly before exit; unlike Async Clipboard, sendBeacon() requires no transient user activation. |

## Async Clipboard API and User Activation — Under the Hood
Transient user activation behaves like a stamped museum entry ticket that dissolves after a few seconds: if you call `navigator.clipboard.write()` while holding the ticket, the browser keeps the turnstile open while your `Promise<Blob>` resolves.

1. **Activation Check**: Trusted pointer or keyboard events propagating through [DOM Event Propagation](/concepts/dom-event-propagation) set the frame's `transientActivation` timestamp.
2. **Deferred `ClipboardItem`**: Passing a `Promise<Blob>` inside `new ClipboardItem({ 'text/plain': promise })` lets `write()` claim the gesture synchronously before network I/O completes.
3. **Payload Sanitization**: Engines re-encode `image/png` and strip `<script>` tags from `text/html` on paste to neutralize OS-level parser exploits.

```js
button.addEventListener('click', () => {
  const textBlob = fetch('/api/share-link')
    .then((res) => res.text())
    .then((url) => new Blob([url], { type: 'text/plain' }));

  navigator.clipboard.write([new ClipboardItem({ 'text/plain': textBlob })]);
});
```

## Beacon API and visibilitychange Lifecycle — Under the Hood
`navigator.sendBeacon()` hands the serialized payload directly to the browser's network process over IPC, decoupling socket transmission from the renderer lifecycle. Unlike `fetch()` without `keepalive`, which aborts on tab close, the network process finishes delivery after the renderer tears down.

1. **Reliable Trigger**: Mobile browsers freeze or kill backgrounded tabs without firing `unload`; `document.visibilityState === 'hidden'` fires reliably when switching tabs or locking the screen.
2. **64 KB Keepalive Pool**: All pending beacons and `fetch(..., { keepalive: true })` requests share a strict 64 KB per-origin buffer; `sendBeacon()` returns `false` if full.
3. **CORS Constraints**: Passing a `Blob` with `application/json` triggers a [CORS preflight](/concepts/cors-preflight-samesite) on cross-origin endpoints; `text/plain` or `FormData` avoids preflight overhead.

```js
document.addEventListener('visibilitychange', () => {
  if (document.visibilityState !== 'hidden') return;
  const body = JSON.stringify({ route: location.pathname, ts: Date.now() });
  const queued = navigator.sendBeacon('/api/telemetry', body);
  if (!queued) {
    localStorage.setItem('pending-telemetry', body);
  }
});
```

## Interactive Labs & Dissections
- [Async Clipboard API and User Activation Lab](https://frontend.rocks/concepts/async-clipboard-activation/#lab) ([Markdown](https://frontend.rocks/concepts/async-clipboard-activation.md))
- [Beacon API and visibilitychange Lifecycle Lab](https://frontend.rocks/concepts/beacon-api-visibilitychange/#lab) ([Markdown](https://frontend.rocks/concepts/beacon-api-visibilitychange.md))

## Primary Sources
- https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API
- https://w3c.github.io/clipboard-apis/
- https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon
- https://w3c.github.io/beacon/
