PWA - Fetch GET URL when online

I am a bit confused about approaching deferred fetch requests.

I have a button in my app. when clicked, I want it to perform a fetch using the GET method. If the device is offline, I want that fetch deferred until the the device is online again. The fetch can complete successfully now or anytime in the future.

What approach should I be taking? Can you also clarify where the deferment should be take place? i.e. app script or service worker

1 answer

  • answered 2018-11-08 17:33 Jeff Posnick

    What you describe sounds like a user case for the Background Fetch API. It is not widely supported yet, but it is currently being tested in Chrome 71.

    You can learn more from articles like this one.

    You can't keep your service worker alive indefinitely, which would be necessary to accomplish this in the service worker without that API. (You could try putting this logic into your web app code via a retry-with-backoff timer, if you want.)