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

Check support:

Service Worker Implementation

Basic Task Registration

Create periodic tasks in your service worker:

circle-check

Multiple Tasks Under Same Tag

Register multiple related tasks:

Complete Example: Blog Post Sync

Client-Side Registration

Basic Registration

Request periodic sync from your application:

With Error Handling

Listening for Sync Updates

Receive notifications when sync completes:

Common Use Cases

1. News/Content Updates

Keep articles fresh in the background:

2. Social Media Feed

Update social feed periodically:

3. Weather Data

Keep weather information up-to-date:

4. Cache Cleanup

Periodically clean old cached data:

5. User Data Prefetch

Preload user-specific data:

Browser Execution Control

circle-exclamation

Factors Affecting Execution

1. Site Engagement Score

  • High engagement = more frequent sync

  • Low engagement = less frequent or no sync

  • Based on user's interaction with your site

2. PWA Installation

  • Installed PWAs get higher priority

  • Non-installed sites may not sync at all

3. Device State

  • Battery level (low battery = no sync)

  • Charging status (more likely when charging)

  • Data saver mode (disabled when enabled)

  • Network type (WiFi preferred over cellular)

4. User Activity

  • More likely during typical usage times

  • Skipped during extended inactivity

5. Interval Duration

  • Very short intervals (< 12 hours) often ignored

  • Longer intervals more likely to be respected

  • Practical minimum: 12-24 hours

Managing Periodic Sync

Check Registered Tags

Query which tags are registered:

Unregister Periodic Sync

Stop periodic sync for a tag:

Re-register After Updates

Update periodic sync interval:

Testing Periodic Sync

Force Sync in DevTools

Chrome DevTools allows manual triggering:

Test in Code

Manually trigger for testing:

Debugging Tips

Error Handling

Retry on Failure

The browser will retry if your sync task throws an error:

Graceful Degradation

Handle unsupported browsers:

Best Practices

1. Use Reasonable Intervals

2. Minimize Network Requests

3. Check Network Conditions

4. Provide User Control

Allow users to configure sync:

5. Monitor Performance

Track sync performance:

Limitations

Browser Support

  • Supported: Chrome, Edge (Chromium-based)

  • Not Supported: Safari, Firefox, older browsers

  • Platform: Android and Desktop only (not iOS)

Execution Constraints

  • Browser decides actual execution time

  • May not run at all if engagement is low

  • Requires PWA installation for best results

  • Respects battery and data saver settings

Resource Limits

  • Limited execution time (typically < 30 seconds)

  • Must complete quickly or risk termination

  • Network-only (no intensive computations)

Permissions

Periodic sync requires the same permissions as service workers:

Resources

  • MDN Periodic Background Sync: https://developer.mozilla.org/en-US/docs/Web/API/Web_Periodic_Background_Synchronization_API

  • Web.dev Article: https://web.dev/periodic-background-sync/

  • Chrome Developers: https://developer.chrome.com/docs/capabilities/periodic-background-sync

  • Can I Use: https://caniuse.com/periodic-background-sync

Last updated

Was this helpful?