question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Background Sync replay request

See original GitHub issue

Hi,

I am trying to make a background-sync request with WorkBox, in a very simple example application with Vue. I revised my codes for a long time but I did not find the solution.

The service worker is well registered and when I make the ajax request, background-sync works properly.

This is my code in the sw.js:

const queue = new workbox.backgroundSync.Queue('asierrrna');
workbox.routing.registerRoute(
  RegExp('^MY_URL'),
  workbox.strategies.networkOnly({
    plugins: [{
      fetchDidFail: async ({request}) => {
        await queue.addRequest(request);
      },
    }],
  }),
  'GET'
);

Background-sync is registered in indexedDB tab:

Captura de pantalla 2019-04-24 a las 11 25 00 .

Finally the network tab. There we see that the API call is well, because when the network returns it makes the call, but nothing else happens in the app, and the old data is not replaced by the new one 😕

Captura de pantalla 2019-04-24 a las 11 25 50

Thanks 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
asiermusacommented, Apr 26, 2019

Hi @philipwalton,

The sync event works properly. As you can see in the picture above, when ajax petition fail and internet connection is restored, the sync event is well triggered and in the network tab I can see the API response.

My problem is related with how I can replace the old query with the new one in my app. I was looking to do with callbacks in the Queue like that (it doesn’t work for me with the new onSync callback):

queueDidReplay: async (storableRequestArray) => {
     let message = {
          'data' : storableRequestArray
     };
     let clients = await self.clients.matchAll();
     for (const client of clients) {
         client.postMessage(message)
     }
}

Then in my SPA app I listen for a “message”, and I use my app logic to restore the failed request.

navigator.serviceWorker.addEventListener('message', (e) => {
  const message = e.data
  doSomething() //my app logic here
});

Is this the right way?

Thanks

0reactions
subashdbccommented, Nov 6, 2019

@asiermusa I was not sure about how Background Sync work on workbox. After doing some research and experiments I was able to catch how things were working. Sorry for that comment

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manually replaying requests queued by workbox-background ...
Queue class (instead of workbox.backgroundSync.Plugin ) directly. The Plugin class takes care of setting up a fetchDidFail callback for you, so ...
Read more >
workbox-background-sync - Chrome Developers
The easiest way to use Background Sync is to use the Plugin that will automatically Queue up failed requests and retry them when...
Read more >
workbox-background-sync - npm package - Snyk
Queues failed requests and uses the Background Sync API to replay them when the network is available. Visit Snyk Advisor to see a...
Read more >
workbox-background-sync examples - CodeSandbox
Learn how to use workbox-background-sync by viewing and forking ... AboutQueues failed requests and uses the Background Sync API to replay them when...
Read more >
workbox-background-sync - UNPKG
18, * A class to manage storing failed requests in IndexedDB and retrying them ; 19, * later. All parts of the storing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found