--- url: "https://web.dev/articles/web-apps/periodic-background-sync" titulek: "How to periodically synchronize data in the background | Web Apps | web.dev" h1: "How to periodically synchronize data in the background Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/web-apps/periodic-background-sync" drobecky: ["Web Apps", "How to periodically synchronize data in the background"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 22:29:52" znaku: 6754 slov: 844 kodovani: "iso-8859-2" sha256: "ccf2f662d7077eb2c2b41475066b3dc67d354b5275d75eec1d12507f5a3dd2b0" --- How to periodically synchronize data in the background Stay organized with collections Save and categorize content based on your preferences. Cecilia Cong The modern way Periodic background sync lets you show fresh content when a progressive web app or service worker-backed page is launched. It does this by downloading data in the background when the app or page is not being used. Using the Periodic Background Sync API After the service worker is installed, use the [Permissions API](https://developer.mozilla.org/docs/Web/API/Permissions_API) to query for `periodic-background-sync`. You can do this from either a window or a service worker context. `const status = await navigator.permissions.query({ name: 'periodic-background-sync', }); if (status.state === 'granted') { // Periodic background sync can be used. } else { // Periodic background sync cannot be used. }` Registering a periodic sync requires both a tag and a minimum synchronization interval (`minInterval`). The tag identifies the registered sync so that multiple syncs can be registered. In the example below, the tag name is `'content-sync'` and the `minInterval` is one day. `navigator.serviceWorker.ready.then(async registration => { try { await registration.periodicSync.register('get-cats', { minInterval: 24 * 60 * 60 * 1000 }); console.log('Periodic background sync registered.'); } catch (err) { console.error(err.name, err.message); } });` Call `periodicSync.getTags()` to retrieve an array of registration tags. The example below uses tag names to confirm that cache updating is active to avoid updating again. `const registration = await navigator.serviceWorker.ready; if ('periodicSync' in registration) { const tags = await registration.periodicSync.getTags(); // Only update content if sync isn't set up. if (!tags.includes('content-sync')) { updateContentOnPageLoad(); } } else { // If periodic background sync isn't supported, always update. updateContentOnPageLoad(); }` To respond to a periodic background sync event add a `periodicsync` event handler to your service worker. The event object passed to it will contain a tag parameter matching the value used during registration. For example, if a periodic background sync was registered with the name `'content-sync'`, then `event.tag` will be `'content-sync'`. `self.addEventListener('periodicsync', (event) => { if (event.tag === 'content-sync') { event.waitUntil(syncContent()); } });` Browser compatibility Browser Support 80 80 x x [Source](https://developer.mozilla.org/docs/Web/API/PeriodicSyncManager) The classic way Rather than updating data in the background so it's ready when the user loads the app, the classic way simply consists of updating the data on load. Further reading [Richer offline experiences with the Periodic Background Sync API](/i18n/en/periodic-background-sync) [Web Periodic Background Synchronization API](https://developer.mozilla.org/docs/Web/API/Web_Periodic_Background_Synchronization_API) Demo HTML ` How to periodically synchronize data in the background

How to periodically synchronize data in the background

Periodic background sync can be used. Install the app first.

Periodic background sync cannot be used.

Last updated

Never

Registered tags

` CSS `html { box-sizing: border-box; font-family: system-ui, sans-serif; color-scheme: dark light; } *, *:before, *:after { box-sizing: inherit; } body { margin: 1rem; }` JS `const available = document.querySelector('.available'); const notAvailable = document.querySelector('.not-available'); const ul = document.querySelector('ul'); const lastUpdated = document.querySelector('.last-updated'); const updateContent = async () => { const data = await fetch( 'https://worldtimeapi.org/api/timezone/Europe/London.json' ).then((response) => response.json()); return new Date(data.unixtime * 1000); }; const registerPeriodicBackgroundSync = async (registration) => { const status = await navigator.permissions.query({ name: 'periodic-background-sync', }); if (status.state === 'granted' && 'periodicSync' in registration) { try { // Register the periodic background sync. await registration.periodicSync.register('content-sync', { // An interval of one day. minInterval: 24 * 60 * 60 * 1000, }); available.hidden = false; notAvailable.hidden = true; // List registered periodic background sync tags. const tags = await registration.periodicSync.getTags(); if (tags.length) { ul.innerHTML = ''; } tags.forEach((tag) => { const li = document.createElement('li'); li.textContent = tag; ul.append(li); }); // Update the user interface with the last periodic background sync data. const backgroundSyncCache = await caches.open('periodic-background-sync'); if (backgroundSyncCache) { const backgroundSyncResponse = backgroundSyncCache.match('/last-updated'); if (backgroundSyncResponse) { lastUpdated.textContent = `${await fetch('/last-updated').then( (response) => response.text() )} (periodic background-sync)`; } } // Listen for incoming periodic background sync messages. navigator.serviceWorker.addEventListener('message', async (event) => { if (event.data.tag === 'content-sync') { lastUpdated.textContent = `${await updateContent()} (periodic background sync)`; } }); } catch (err) { console.error(err.name, err.message); available.hidden = true; notAvailable.hidden = false; lastUpdated.textContent = 'Never'; } } else { available.hidden = true; notAvailable.hidden = false; lastUpdated.textContent = `${await updateContent()} (manual)`; } }; if ('serviceWorker' in navigator) { window.addEventListener('load', async () => { const registration = await navigator.serviceWorker.register('./sw.js'); console.log('Service worker registered for scope', registration.scope); await registerPeriodicBackgroundSync(registration); }); }` Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2026-09-04 UTC.