Service workers & promises—keeping async work alive

Avoid lost work with lifecycle-aware patterns, explicit event.waitUntil, and idempotent jobs.

TL;DR

  • Use event.waitUntil(promise) for every async path started by an event.
  • Design idempotent jobs—assume retries.
  • Keep heavy/long jobs off transient events; use queues/alarms.

Core patterns

  • Warm start (lazy imports), bounded retries with backoff, consistent error shapes.
  • Message orchestration: command envelopes with requestId.

Don’ts

  • Long-lived state in globals; the service worker can terminate at any time.
  • Fire-and-forget promises from listeners.

Testing

  • Simulate termination and replay; inject artificial delays; verify retry limits.

See also: Messaging Patterns · Talk to an Engineer

Ship lifecycle-safe MV3 code

We design idempotent jobs, safe queues, and reviewer-friendly listings.