Время от времени сайт не может быть доступен из-за работника службы. Я отлажил сервисного работника, и ответ, который возвращается, является ответом 200 статуса, поэтому мой сервисный работник отправит ответ и затем клонирует ответ.
Это очень споради c, когда это происходит, но случается часто, когда я делаю релиз. Технический стек - это приложение Ruby rails, поэтому он использует плагин rails-serviceworker.
Вот мой код:
/*
@author Matt Claffey
*/
var cacheFiles = [
'/',
'/?utm_source=homescreen&utm_medium=pwa',
'/offline',
'/brands',
'/forum',
'/bars',
'/stream'
];
var staticCacheFiles = [
'<%= asset_path "application.js" %>',
'<%= asset_path "vendor.js" %>',
'<%= asset_path "application.css" %>',
'/assets/roboto-bold-webfont.woff2',
'/assets/roboto-regular-webfont.woff2',
'/assets/roughbg.jpg',
'/assets/logo.png',
'<%= asset_path "googleplay.png" %>',
'<%= asset_path "appstore.png" %>',
'<%= asset_path "explore-icon.png" %>',
'<%= asset_path "location.png" %>',
'<%= asset_path "discuss-icon-2.png" %>',
'<%= asset_path "stream-icon-1.png" %>',
'/assets/manifest.json',
'/assets/favicon.ico'
];
var staticDomain = '';
// Update the version when making changes and pushing up
var cacheName = 'rumratings-cache-v1.1.4';
function onInstall(event) {
self.skipWaiting();
event.waitUntil(
caches.open(cacheName).then(function (cache) {
const cacheUrls = cacheFiles.concat(staticCacheFiles);
return cache.addAll(cacheUrls);
})
.catch(function(error) {
console.error(error);
})
);
}
function onActivate(event) {
var cacheWhitelist = [cacheName];
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
return self.clients.claim();
}
function respondWithAssetCache(request) {
// open up the cache
return caches.open(cacheName).then(function(cache) {
// check if our request lives there
return cache.match(request).then(function(cacheResponse) {
return cacheResponse || fetch(request);
});
});
}
function respondWithPageCache(request) {
return caches.open(cacheName).then(function(cache) {
// Fetch for the request as normal
return fetch(request).then(function(response) {
// if the document responds with a 200 & its one of our requests in the array uptop
if (response.status === 200 && cacheFiles.includes(request)) {
// then we clone it with the latest response.
cache.put(request, response.clone());
}
return response;
}).catch(function() {
// oh no! We have gone offline so now we have been updating the cache on line 86
// we will now check the cache to see if we have that request in there!
return cache.match(request).then(function(response) {
// response is either the response object or undefined.
// If it fails then return offline page.
return response || cache.match('/offline');
});
});
});
}
function onFetch(event) {
const requestUrl = event.request.url.replace(/^.*\/\/[^\/]+/, '');
// If its a static asset respond with the cached version, if its not in the cache then fetch it.
if (staticCacheFiles.indexOf(requestUrl) !== -1) {
event.respondWith(
respondWithAssetCache(requestUrl)
);
return;
}
// if it is a page request then try and return the
if (event.request.mode === 'navigate' && event.request.method === 'GET') {
if (cacheFiles.includes(requestUrl)) {
// fetch for the url and then if it returns we replace the cached one with that
event.respondWith(respondWithPageCache(requestUrl));
} else {
event.respondWith(fetch(event.request).catch(function() {
return caches.open(cacheName).then(function(cache) {
return cache.match('/offline');
});
}));
}
return;
}
}
self.addEventListener('install', onInstall);
self.addEventListener('activate', onActivate);
self.addEventListener('fetch', onFetch);