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 fallback in Safari not working

See original GitHub issue

Library Affected: workbox-background-sync

Browser & Platform: Safari 13

Issue or Feature Request Description: The fallback for browsers that don’t support background sync doesn’t appear to be working as expected in a PWA I am working on.

When offline, the request is added to the queue and I can see this in IndexedDB. However no event other than installing a new service worker (a solution that’s not workable in practice) will trigger the requests in the queue to be retried.

Looking in Queue.ts in workbox/packages/workbox-background-sync/src I can see on lines 413-415:

      // If the browser doesn't support background sync, retry
      // every time the service worker starts up as a fallback.
      this._onSync({queue: this});

this._onSync() only appears to be called the first time this._addSyncListener() is called but as it’s not attached to an event listener it’s never called again which would explain why it doesn’t work as I expected.

As a workaround I could make use of the replayRequests() function but before doing so I would like to confirm whether or not this behaviour is expected.

Here’s the service worker code (more or less a straight copy and paste from the Workbox docs):

  // Clone the request to ensure it's safe to read when
  // adding to the Queue.  
    const promiseChain = fetch(event.request.clone())
      .catch((err) => {
        self.clients.matchAll().then(all => all.map(client => client.postMessage("offline")));
  
        if(typeof BroadcastChannel !== "undefined") {
            const broadcastChannel = new BroadcastChannel('wb_channel');
            broadcastChannel.postMessage('offline');
        }
        return queue.pushRequest({ request: event.request });
      });
    event.waitUntil(promiseChain);
});

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jeffposnickcommented, Oct 25, 2021

https://github.com/GoogleChrome/workbox/pull/2955 exposes the QueueStore and StorableRequest classes publicly, and https://github.com/GoogleChrome/workbox/pull/2941 exposes the size() of the QueueStore used by a Queue. Both of those PRs should be part of the upcoming Workbox v6.4.0 release.

I’m going to be honest in that I have never had to work with those pieces of workbox-background-syncmanually, so I’m not complete sure if it addresses the use case you detail here. @tropicadri has worked with this portion of the Workbox codebase more recently and might have some insight to share.

1reaction
Adam-Reevescommented, Mar 4, 2020

@philipwalton so will the fix of making the reply logic conditional on the registration succeeding fix the issues I’m having with Safari?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Force a POST to Background sync regardless of current ...
Now, on Safari and Firefox - I understand the fallback will be to attempt a background sync every time the service worker starts....
Read more >
browser.storage.onChanged doesn't appear to work on Safari
I'm not able to reproduce this problem when using browser.storage.onChanged from a background page, it works as expected for me there. I copied...
Read more >
Background Sync with Service Workers - David Walsh Blog
Because a service worker isn't continually running -- it "wakes up" to do a task and then "goes back to sleep" -- we...
Read more >
Web Push Troubleshooting - OneSignal Documentation
Common setup issues with Web Push (Chrome, Firefox, Safari, etc.) ... Your current site origin does not match your settings in the OneSignal...
Read more >
Cross Browser Compatibility of Background Sync API in Safari 9
Background Sync API is Not Supported on Safari 9, which means that any user who'd be accessing your page through Safari 9 can...
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