Coder Social home page Coder Social logo

Comments (5)

jeffposnick avatar jeffposnick commented on May 12, 2024

Untested, but the following should give you about what you're looking for:

toolbox.router.get('/(.*)', function(request, values, options) {
  // networkFirst will attempt to return a response from the network,
  // then attempt to return a response from the cache.
  return toolbox.networkFirst(request, values, options).catch(function(error) {
    // If both the network and the cache fail, then `.catch()` will be triggered,
    // and we get a chance to respond with our cached fallback page.
    // This would ideally check event.request.mode === 'navigate', but that isn't supported in
    // Chrome as of M48. See https://fetch.spec.whatwg.org/#concept-request-mode
    if (request.method === 'GET' && request.headers.get('accept').includes('text/html')) {
      return toolbox.cacheOnly(new Request('/Offline'), values, options);
    }
    throw error;
  });
});

(Adapted from the vanilla service worker version that doesn't use sw-toolbox.)

from sw-toolbox.

AaronLayton avatar AaronLayton commented on May 12, 2024

Hi @jeffposnick thanks for the quick response the other day. I have implemented the above but it doesn't seem to be getting to the catch callback

I am getting this error for an uncached page

The FetchEvent for "https://<my-url>" resulted in a network error response: an object that was not a Response was passed to respondWith().

from sw-toolbox.

AaronLayton avatar AaronLayton commented on May 12, 2024

@jeffposnick could really do with some help on this as still new sw - could the issue be reopened until fixed?

Could this be an issue with toolbox.networkFirst returning something even though we have a catch event? Do we need to clone the original request first maybe

from sw-toolbox.

jeffposnick avatar jeffposnick commented on May 12, 2024

Sorry, networkFirst wasn't rejecting when it should have, meaning that the .catch() wasn't triggering.

#84 should fix it.

from sw-toolbox.

 avatar commented on May 12, 2024

@jeffposnick Your code above works great, thank you very much. May I ask you one question how can I add this to your code please:

networkTimeoutSeconds: 3

from sw-toolbox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.