Periodic Background API

Published on April 15, 2021

← Back to tips

Lets you run specific tasks at custom intervals, like a cronjob but in your browser.

Useful to provide a seamless and fast web experience

Use Cases:

  • Prefetch changed contents on the page without any action from user, for faster reloading/refresh; Can also use to update offline cache
  • Retrieve and batch notifications for UI

Some Limitations:

  • The API is only usable in a progressive web app (PWA)
  • Can only run the code in your service worker. You might need to use a postMessage to communicate with client/window

How to use:

Register Periodic Sync

  1. Request permissions
  2. Once granted, register a sync with an interval
  3. Add an event listener for the custom sync event to trigger the action

Sync Listener

More reading

▶️ https://wicg.github.io/periodic-background-sync/

▶️ https://web.dev/periodic-background-sync/

🎉 Interested in Frontend or Indie-hacking?

I talk about the latest in frontend, along with my experience in building various (Indie) side-projects