# Periodic background sync for content updates

> When periodic background sync is the right tool for refreshing content, how Chrome gates it on installation and engagement, and how to test it.

import Figure from '@components/Figure.astro';
import periodicSyncDiagram from '@assets/diagrams/periodic-background-sync.svg';

Periodic background sync lets an installed PWA refresh its cached content on a schedule the
browser chooses, by firing a `periodicsync` event in the service worker while no page is open.
A news or weather app can use it to open with current data without waiting for a refresh.
It is Chromium-only and requires installation and regular use of the app.

<Figure src={periodicSyncDiagram} alt="Flow diagram of periodic background sync: an installed PWA with enough site engagement checks the periodic-background-sync permission and calls periodicSync.register() with a tag and minInterval; the browser schedules the sync no more often than minInterval, fires the periodicsync event in the service worker when the time arrives and the device is online, and the worker fetches and caches new data inside event.waitUntil(). The cycle repeats until the app is uninstalled, engagement decays or periodicSync.unregister() is called." caption="Periodic background sync: from permission and registration to the repeating periodicsync event." />

## How it works

The page registers a tag with `registration.periodicSync.register(tag, { minInterval })`; the
browser then fires `periodicsync` in the service worker no more often than `minInterval`, when
the device is online, and only while the app's engagement permits it. The API surface
(`PeriodicSyncManager`, `register()`, `getTags()`, `unregister()`, the `periodicsync` event) is in
Chrome 80 and Edge 80, other Chromium browsers inherit it, and Firefox and Safari have none (BCD `api.PeriodicSyncManager`); the method-level reference
is the [service worker entry](/reference/service-worker/periodic-background-sync/).

### Chrome's gates

Chrome fires the event only for a web app the user has installed and launched as a standalone
app, not for a site in a tab, and only when the device is on a network it has used before
([Periodic background sync](https://developer.chrome.com/docs/capabilities/periodic-background-sync),
developer.chrome.com). The frequency is tied to the site engagement score Chrome keeps per origin,
visible at `about://site-engagement/`: a score of zero means no events at all, and a higher score
allows syncs closer to `minInterval`. If the user stops opening the app, the score decays and
the events stop. `minInterval` is therefore a floor rather than a schedule, and a registration with
`minInterval: 0` leaves the cadence entirely to Chrome.

### Permission

The feature is gated by the `periodic-background-sync` permission. Chrome derives it from installation and shows no prompt: its [permission context](https://chromium.googlesource.com/chromium/src/+/refs/heads/main/chrome/browser/background_sync/periodic_background_sync_permission_context.cc) allows the origin once an app is installed, and the user can still change the setting. Check it with `navigator.permissions.query({ name:
'periodic-background-sync' })` from a window or a service worker; only `granted` means you can use the API. `register()` rejects with `NotAllowedError` when the permission is not granted, `InvalidStateError` when the
registration has no active worker, and `InvalidAccessError` when called from a window that is neither a
top-level nor an auxiliary browsing context ([PeriodicSyncManager: register()](https://developer.mozilla.org/en-US/docs/Web/API/PeriodicSyncManager/register),
developer.mozilla.org).

### What it is for, and what it is not

Background sync, despite the similar name, retries a failed request once connectivity returns;
periodic background sync pulls fresh data on a cadence. Web Push also wakes the worker but
interrupts the user with a notification, and Chrome and Edge reject a subscription that is not `userVisibleOnly` ([PushManager.subscribe()](https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe)). Periodic sync refreshes
on a repeating basis without a notification, which makes it right for a daily article cache and wrong for
anything that must happen at a particular time; the
[notification triggers](/reference/notifications/notification-triggers/) entry covers why no shipped API fills that gap.

## Examples

Keep a page-load refresh as the fallback, and skip it when a periodic sync is registered.

### Registering a daily article refresh when the app can

Register after the service worker is active and the permission is granted. The feature and permission checks send engines without the API and Chrome tabs to refreshing on load; the `catch` covers a `register()` that still rejects.

```js
async function enableDailyRefresh() {
  const registration = await navigator.serviceWorker.ready;
  if (!('periodicSync' in registration)) {
    return refreshOnLoad(); // Firefox, Safari, or an uninstalled Chrome tab
  }
  const status = await navigator.permissions.query({ name: 'periodic-background-sync' });
  if (status.state !== 'granted') return refreshOnLoad();
  try {
    await registration.periodicSync.register('update-articles', { minInterval: 24 * 60 * 60 * 1000 });
  } catch {
    refreshOnLoad(); // permission denied or no active worker: behave as before
  }
}
```

`refreshOnLoad()` is the pre-existing behaviour, so an engine without the API loses nothing; periodic sync can avoid the load-time refresh where it is available.

### Handling the event and skipping the load-time refresh

The worker caches the response inside `event.waitUntil()`; the page checks `getTags()` and skips
its own refresh when the sync is registered, on the bet that the worker has kept the cache fresh. A registered tag does not prove a sync has run yet.

```js
// service worker
self.addEventListener('periodicsync', (event) => {
  if (event.tag === 'update-articles') {
    event.waitUntil(caches.open('articles').then((cache) => cache.add('/api/articles')));
  }
});

// page
async function maybeRefresh(registration) {
  const tags = 'periodicSync' in registration ? await registration.periodicSync.getTags() : [];
  if (!tags.includes('update-articles')) refreshOnLoad();
}
```

Keep the handler small because Chrome gives the worker a limited window. Chrome gates the feature on engagement so that apps do not
gratuitously consume battery or data, so keep downloads small.

:::note
Chrome's periodic background sync guide describes an experimental **Periodic Background Sync**
section in the DevTools Application panel. Clicking **Start recording** logs the register,
background sync, and unregister events, and a recording stays enabled for up to three days, which
shows when Chrome actually chose to fire the event. The same guide says Chrome uses the site
engagement score, listed at `about://site-engagement/`, to decide whether and how often periodic
syncs happen, and that a `periodicsync` event is not fired unless the score is greater than zero
([Periodic background sync](https://developer.chrome.com/docs/capabilities/periodic-background-sync),
developer.chrome.com).
:::

## See also

- [Web Periodic Background Synchronization](https://wicg.github.io/periodic-background-sync/) (wicg.github.io)
- [Periodic background sync](https://developer.chrome.com/docs/capabilities/periodic-background-sync) (developer.chrome.com)
- [PeriodicSyncManager.register()](/reference/service-worker/periodic-background-sync/)
- [Background sync](/reference/service-worker/background-sync/)
- [Web Push and PushManager.subscribe()](/reference/notifications/web-push/)
- [Notification triggers (showTrigger)](/reference/notifications/notification-triggers/)