How do I fix the PWA bug?
-
site
sw.js: 1 Uncaught (in promise) TypeError: Request failed
const static = 'site-static-v1'; const assets = [ '/', '/index.html', '/assets/js/ui.js', '/assets/css/main.css', '/assets/images/background-home.jpg', 'https://fonts.googleapis.com/css?family=Lato:300,400,700', ]; // install event self.addEventListener('install', evt => { evt.waitUntil( caches.open(static).then((cache) => { console.log('caching shell assets'); cache.addAll(assets); }) ); }); // activate event self.addEventListener('activate', evt => { evt.waitUntil( caches.keys().then(keys => { return Promise.all(keys .filter(key => key !== static) .map(key => caches.delete(key)) ); }) ); }); // When we change the name we could have multiple cache, to avoid that we need to delet the old cache, so with this function we check the key that is our cache naming, if it is different from the actual naming we delete it, in this way we will always have only the last updated cache. // fetch event self.addEventListener('fetch', evt => { evt.respondWith( caches.match(evt.request).then(cacheRes => { return cacheRes || fetch(evt.request); }) ); });
I use it all for a PWA applicationJavaScript Penelope Brewer, May 4, 2020 -
the error has changed
Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'WorkerGlobalScope': Cannot construct a Request with a Request object that has already been used
self.addEventListener('install', function(event) {
var indexPage = new Request('index.php');
event.waitUntil(
fetch(indexPage).then(function(response) {
return caches.open('pwabuilder-offline').then(function(cache) {
console.log('[PWA Builder] Cached index page during Install'+ response.url);
return cache.put(indexPage, response);
});
}));
});
self.addEventListener('fetch', function(event) {
var updateCache = function(request){
return caches.open('pwabuilder-offline').then(function (cache) {
return fetch(request).then(function (response) {
console.log('[PWA Builder] add page to offline'+response.url)
return cache.put(request, response);
});
});
};
event.waitUntil(updateCache(event.request));
event.respondWith(
fetch(event.request).catch(function(error) {
console.log( '[PWA Builder] Network request Failed. Serving content from cache: ' + error );
return caches.open('pwabuilder-offline').then(function (cache) {
return cache.match(event.request).then(function (matching) {
var report = !matching || matching.status == 404?Promise.reject('no-match'): matching;
return report
});
});
})
);
})Anonymous
1 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!