v148 · JavaScript · Web Workers

Extended lifetime shared workers

Pass extendedLifetime: true to the SharedWorker constructor and the worker continues running after all client pages close — enabling true background processing without a Service Worker.

concepts

  1. Persistent Counter

    A shared worker that ticks a counter every second. Simulate connecting and disconnecting all client pages — with a standard worker the counter resets; with extendedLifetime: true it keeps running.

  2. Background Channel

    A worker used as a persistent message queue. Clients post messages, disconnect, then reconnect — the worker held onto the queue while no clients were connected and delivers on reconnect.

  3. Offline Queue

    An analytics message queue backed by an extended-lifetime worker. Messages enqueued while "offline" persist in the worker — which keeps running after all tabs close — and flush automatically when connectivity returns. Simulates real queue drain and connectivity toggle.

  4. Worker Lifecycle Demo

    A visual state machine for the Shared Worker lifecycle: starting → running → keepalive → terminating → terminated. Simulate clients connecting and disconnecting, trigger keepalive fetch events, and watch a live timeline of worker uptime vs. client count. Shows all four lifetime extension triggers.

  5. Keepalive Timer

    Two side-by-side live timers — one backed by a standard SharedWorker, one by an extendedLifetime: true worker. Connect clients on each side, then disconnect them all. The standard worker's timer resets immediately. The extended worker's timer keeps running through a simulated grace period, demonstrating the key behavioural difference at a glance.

why it shipped

Shared Workers are great for cross-tab coordination — a single worker instance shared by all open tabs. But standard Shared Workers are terminated as soon as the last client tab closes. If you need the worker to finish an asynchronous operation (flushing a write buffer, completing an analytics ping, finishing a background sync) after the last tab navigates away, you previously had to reach for a Service Worker — a heavier primitive with its own lifecycle complexity. The new extendedLifetime: true option tells the browser to keep the shared worker alive even with zero connected clients, for a grace period long enough to complete in-flight work.

references

implementation reference

Need the exact API surface, compatibility boundaries, errors, lifecycle, and source links? Read the matching gendn reference ↗