v148 · JavaScript · SharedWorker

Offline Queue

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

Checking SharedWorker support…

Worker state

Connecting…
0
Messages in queue
0
Messages sent

Enqueue a message

Queue contents

  • Queue is empty — enqueue messages above.

How extended-lifetime enables this

// Worker created with extendedLifetime: true
const worker = new SharedWorker('/queue-worker.js', {
  name: 'analytics-queue',
  extendedLifetime: true, // keeps running when all tabs close
});

// Worker side — queue persists in worker memory between tab sessions
let queue = [];
let isOnline = true;

self.addEventListener('connect', e => {
  const port = e.ports[0];
  port.addEventListener('message', ({ data }) => {
    if (data.type === 'enqueue') queue.push(data.payload);
    if (data.type === 'flush') flushQueue();
  });
  port.start();
  port.postMessage({ type: 'queue', queue }); // sync state to new tab
});

function flushQueue() {
  if (!isOnline) return;
  while (queue.length) {
    const msg = queue.shift();
    fetch('/analytics', { method: 'POST', body: JSON.stringify(msg) });
  }
}

references

implementation reference

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