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.

Add networkTimeoutSeconds to NetworkOnly strategy

See original GitHub issue

At present, it is possible to set timeout only for NetworkFirst strategy:

workbox.strategies.networkFirst({networkTimeoutSeconds: 10});

When NetworkOnly strategy is used to retrieve non-cacheable data, it keeps waiting indefinitely in case of a network issue.

Suggested implementation: https://github.com/github/fetch/issues/175#issuecomment-216791333

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jeffposnickcommented, Apr 12, 2019

keep some progress indicator spinning forever 😃

Just to be clear, though, there really should be a reasonable timeout imposed by the user agent by default. If the current per-browser defaults aren’t reasonable, that’s good feedback to pass along to browsers that wait too long.

I’m happy to have folks turn to Workbox to give them more control when they want to override those defaults for specific types of requests, but obviously not everyone is going to use Workbox, and a service worker will likely not be in control during the user’s first visit to a site.

1reaction
PetrTomancommented, Apr 12, 2019

@jeffposnick Yes. Imho it’s better to let the user know something went wrong in a reasonable time than just keep some progress indicator spinning forever 😃 Example:

function timeoutPromise(seconds, promise) {
    return new Promise(function (resolve, reject) {
        var timeoutId = setTimeout(function() {
            reject(new Error('timeout'))
        }, seconds * 1000);

        promise.then(
            function(res) {
                clearTimeout(timeoutId);
                resolve(res);
            },
            function(err) {
                clearTimeout(timeoutId);
                reject(err);
            }
        );
    })
}

// Service worker setup
self.addEventListener('fetch', function(e) {
    // don't use Workbox for POST/AJAX requests
    if (e.request.method === 'POST' || /\/someAjaxPath\//.test(e.request.url)) {
        e.respondWith(timeoutPromise(30, fetch(e.request)));
    }
});

// in Ajax handler: a common bit of guesswork to identify timeout
// (I haven't found a way to pass timeout info to the error handler)
error: function(xhr, status) {
    if (xhr.readyState === 4 && xhr.status < 200) alert('Network timeout');
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

workbox-strategies - Chrome Developers
NetworkOnly. An implementation of a network-only request strategy. This class is useful if you want to take advantage of any Workbox plugins. If ......
Read more >
Workbox: PWA with steroids - Kruk Matias - Medium
For this, you can use a NetworkFirst strategy with the networkTimeoutSeconds option configured. Example: workbox.routing.registerRoute(
Read more >
API - sw-toolbox
A collection of service worker tools for offlining runtime requests.
Read more >
What's the use case for Workbox NetworkOnly strategy
... you might want to explicitly create a route that matches some criteria and applies a NetworkFirst strategy: import {NetworkOnly ...
Read more >
injectManifest | Workbox - Vite PWA - Netlify
You can use the following code to create your custom service worker to be used with network first strategy. We also include how...
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