update
This commit is contained in:
parent
5d8562bda8
commit
99eaa986fe
37
html/sw.js
37
html/sw.js
|
@ -12,47 +12,38 @@ var urlsToCache = [
|
|||
'./font-awesome-4.7.0/fonts/fontawesome-webfont.woff2?v=4.7.0'
|
||||
];
|
||||
|
||||
self.addEventListener('install', function (event) {
|
||||
self.addEventListener('install', event => {
|
||||
// Perform install steps
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME)
|
||||
.then(function (cache) {
|
||||
.then(cache => {
|
||||
console.log('Opened cache');
|
||||
return cache.addAll(urlsToCache);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', function (event) {
|
||||
self.addEventListener('fetch', event => {
|
||||
event.respondWith(
|
||||
caches.match(event.request)
|
||||
.then(function (response) {
|
||||
// Cache hit - return response
|
||||
if (response) {
|
||||
fetch(event.request).then(response => {
|
||||
// Throw invalid responses away...
|
||||
if (!response || response.status !== 200 || response.type !== 'basic')
|
||||
return response;
|
||||
}
|
||||
|
||||
return fetch(event.request).then(
|
||||
function (response) {
|
||||
// Check if we received a valid response
|
||||
if (!response || response.status !== 200 || response.type !== 'basic') {
|
||||
return response;
|
||||
}
|
||||
|
||||
// IMPORTANT: Clone the response. A response is a stream
|
||||
// and because we want the browser to consume the response
|
||||
// as well as the cache consuming the response, we need
|
||||
// to clone it so we have two streams.
|
||||
// Cloning the valid reply
|
||||
var responseToCache = response.clone();
|
||||
|
||||
caches.open(CACHE_NAME)
|
||||
.then(function (cache) {
|
||||
// Storing the valid reply; an upsert into the cache
|
||||
caches.open(CACHE_NAME).then(cache => {
|
||||
cache.put(event.request, responseToCache);
|
||||
});
|
||||
|
||||
// Returning the response
|
||||
return response;
|
||||
}
|
||||
);
|
||||
}, () => {
|
||||
// Failed. It's cache or nothing.
|
||||
return caches.match(event.request);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user