Periodic Sync

The Periodic Background Sync API allows your PWA to fetch fresh content periodically in the background, even when the app is closed. This ensures users always have up-to-date data when they open your app.

Overview

Periodic sync enables:

  • Background updates: Fetch fresh content while app is closed

  • Improved UX: Users see latest content immediately

  • Reduced data usage: Sync only when on WiFi/charging

  • Battery efficiency: Browser controls execution based on engagement

circle-info

Important: Unlike other PWA Bundle features, Periodic Sync requires writing JavaScript code in both the service worker and your application.

How It Works

  1. Client registration: App requests periodic sync with a tag and interval

  2. Browser scheduling: Browser decides when to actually run sync (based on engagement, battery, etc.)

  3. Service worker execution: Service worker performs background task

  4. Client notification: Service worker notifies clients of updates

Browser Support

circle-exclamation

Service Worker Implementation

Basic Task Registration

Create periodic tasks in your service worker:

circle-check

Helper Functions Reference

The bundle generates the following helper functions in the service worker:

Function
Description

registerPeriodicSyncTask(tag, callback, priority)

Register a callback for a periodic sync tag. Optional priority (default: 100) controls execution order.

notifyPeriodicSyncClients(tag, payload)

Send a message to all clients via BroadcastChannel when sync completes.

openCache(name)

Open (or reuse) a named cache instance.

Multiple Tasks Under Same Tag

Register multiple related tasks with priorities:

Complete Example: Blog Post Sync

Client-Side Registration

Basic Registration

Request periodic sync from your application:

Listening for Sync Updates

Receive notifications when sync completes via BroadcastChannel:

Common Use Cases

News/Content Updates

User Data Prefetch

Browser Execution Control

circle-exclamation

Factors Affecting Execution

  • Site Engagement Score: High engagement = more frequent sync

  • PWA Installation: Installed PWAs get higher priority

  • Device State: Battery level, charging status, data saver mode

  • Network: WiFi preferred over cellular

  • Interval Duration: Very short intervals (< 12 hours) often ignored

Managing Periodic Sync

Check Registered Tags

Unregister Periodic Sync

Testing

Force Sync in DevTools

  1. Open DevTools (F12)

  2. Go to ApplicationService Workers

  3. Find your service worker

  4. Look for "Periodic Sync" section

  5. Enter tag name and click "Start"

Graceful Degradation

Handle unsupported browsers:

Limitations

  • Supported: Chrome, Edge (Chromium-based) only

  • Not Supported: Safari, Firefox, older browsers

  • Platform: Android and Desktop only (not iOS)

  • Limited execution time (typically < 30 seconds)

  • Browser decides actual execution time

Permissions

Best Practices

  1. Use reasonable intervals: 12+ hours recommended

  2. Minimize network requests: Batch operations into a single sync

  3. Provide user control: Allow users to configure sync frequency

  4. Handle failures gracefully: Return Promises, use try/catch

  5. Check network conditions: Skip heavy sync on poor connections

Last updated

Was this helpful?