Skip to content

Lesson 6 of 9 · 25m

Background Sync and Periodic Sync Reality

Use sync events as opportunistic wake-ups with strict fallbacks, and understand platform limits before designing product promises around them.

Background Sync has one of the most misleading names in the web platform. Developers hear it and imagine a reliable daemon that wakes the app every fifteen minutes. What they get is an opportunistic one-shot event whose timing belongs to the browser, whose support varies, and whose failure mode is silence. Used correctly, it improves sync latency. Used naively, it becomes a promise you cannot keep.

One-shot sync is a retry accelerator

Register a sync when the network is unavailable or the app is closing with pending work:

await saveMutation(db, mutation);
const reg = await navigator.serviceWorker.ready;
if ('sync' in reg) {
  try {
    await reg.sync.register('outbox-flush');
  } catch (err) {
    console.warn('sync registration failed', err);
  }
}

The worker receives the event with sync listeners and calls event.waitUntil(flushOutbox()) for the matching tag. The browser may fire it when connectivity returns. It may also decide not to. Therefore the page must also flush on startup, visibility, online events, manual retry, and ordinary interaction.

Durable state outlives the worker

Service workers can be killed between tasks. Durable state belongs in IndexedDB, not worker memory. flushOutbox() should load pending mutations, process them with idempotency keys, and write outcomes transactionally:

async function flushOutbox() {
  const db = await openOutboxDb();
  for (const mutation of await pendingMutations(db)) {
    try {
      await sendMutation(mutation);
      await markAccepted(db, mutation.mutationId);
    } catch (err) {
      await markRetryableFailure(db, mutation.mutationId, err);
    }
  }
}

Keep the event short. Browsers impose execution limits, and a queue of hundreds of mutations will not finish in one heroic worker lifetime.

Periodic sync is even more constrained

Periodic sync exists primarily on Android Chromium for installed, highly engaged PWAs:

const status = await navigator.permissions.query({ name: 'periodic-background-sync' });
if (status.state === 'granted') {
  await reg.periodicSync.register('refresh-digest', { minInterval: 12 * 60 * 60 * 1000 });
}

minInterval is a floor, not a schedule. The browser weighs engagement, battery, network, and platform policy. Use it for freshness improvements, never for alarms, payroll deadlines, or security-critical delivery.

Design the trigger matrix honestly

Foreground online flushes immediately. Foreground offline queues locally and shows pending state. Browser online events retry. Service worker sync is an opportunistic retry. Periodic sync is an optional freshness boost. Server push can wake or invalidate when supported. If a note is absent on another device, "the browser did not grant periodic sync" is implementation detail; the honest state is that the change is still pending on this device.

Field notes

  • Feature-detect SyncManager; Safari support has historically lagged Chromium. Build fallbacks first.
  • navigator.onLine means some network path exists, not that your API is reachable.
  • Do not notify users for every background success. Notification spam makes a useful app feel broken.
  • Use distinct sync tags only when they represent distinct work units.
  • Devtools can trigger sync manually, but that proves plumbing only — not real platform timing.

Was this lesson helpful?

Sign in to rate