Periodic sync not fired in service worker

Viewed 232

I'm trying to use background periodic sync for my website. I'm using localhost and registering the periodicsync event at 1*1000 ms, but that doesn't fire at all.

I had a look at this demo, but even if I install the website as an app, it won't fire at all.

Using chrome 87.0.4280.66.

It works however if I manually trigger the periodic sync event from Application developer window.

1 Answers

The periodicsync event will only register correctly when the app is installed as a PWA in a 'most' webkit based browsers only

https://developer.mozilla.org/en-US/docs/Web/API/Web_Periodic_Background_Synchronization_API

The conditions as to whe this will actually fire is unclear and is dependent on some wooly factors such as the users engagement with the website.

That is why the perameter that can be set when registering for the periodic sync is minInterval

This block will help you to register for it successfully, I am, unfortunatly unclear on what the 'real world' scenarios in which the peridic sync will fire:

const status = await navigator.permissions.query({
        // @ts-ignore
        name: 'periodic-background-sync',
      });

      if (status.state === 'granted') {
        navigator.serviceWorker.ready.then(async (sw: any) => {
          await sw.periodicSync.register('periodicsync', {
            minInterval: 1000,
          });
        })
          .catch(error => {
            console.error('[BackgroundSync] Error: ' + JSON.stringify(error, null, 2));
          });
      }
      else {
        console.error('[BackgroundSync] Does not have permission');
      }
    }
Related