Service worker sync event fires immediately

256 Views Asked by At

I am using Chrome 66.0.3359.181 (64-bit). I am running the following code:

navigator.serviceWorker.ready
  .then(sw=>{
    addData('sync-posts',post)
    .then(()=>{
      return sw.sync.register('sync-new-posts');
    })
    .then(()=>{
      var snackbarContainer = document.querySelector('#confirmation-toast');
      const data = {message: 'Your post was saved for syncing!'};
      snackbarContainer.MaterialSnackbar.showSnackbar(data);
    })
    .catch(err=>{
      console.log(err);
    });
  })

However even when I have disconnected my wifi it still triggers the sync event immediately.

ANSWER: I actually figured this out while I was writing it but since I didn't find anyone else answer this type of question AND it took an hour or so out of my time I thought I'd post it anyway. I had a VM network adapter enabled (for Docker) and that was causing it to try and sync even though that connection didn't go anywhere useful.

Then I also discovered that provided I was throwing an error from that sync event when it failed, it would retry syncing. Originally I was catching errors and just logging to console but this meant the sync thought it was complete.

0

There are 0 best solutions below