Comments (5)
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.
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.
@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.
Sorry, networkFirst
wasn't rejecting when it should have, meaning that the .catch()
wasn't triggering.
#84 should fix it.
from sw-toolbox.
@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)
- Issue with Multiple Tab open with one mobile view through chrome dev tools HOT 1
- Caches gets deleted on page reload HOT 1
- Allow for selection of more request properties like headers than just the URL HOT 2
- googlechrome.github.io returns 404
- Website for this repo has been 404 HOT 1
- Repo URL is broken HOT 1
- The fetchAndCache function has a bug on mobile phones HOT 1
- Release 3.6.1 to NPM HOT 1
- Bug: Toolbox does not handle quote exceeded errors HOT 13
- Consider bumping `path-to-regexp`. HOT 1
- Cache with maxAgeSeconds option not expiring for cacheFirst strategy
- how can I precache some url with cookie ?
- Notify people about Workbox ("The next version of sw-precache & sw-toolbox") HOT 1
- Usage and api links broken in Index.md
- What is the use of indexDb if sw-toolbox is validating freshness of cache via date header
- service worker error HOT 1
- toolbox.router.get function not working in safari ios
- Missing documentation for successResponses HOT 1
- Documentation pages have corrupted links to gh-pages!
- Why do I get TypeError failed to fetch on every page refresh HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sw-toolbox.