Add ServiceWorker
This commit is contained in:
parent
efe6250f38
commit
f7c213102f
8
app.js
8
app.js
@ -202,3 +202,11 @@ window.onpopstate = function(event) {
|
|||||||
return v.length > 0;
|
return v.length > 0;
|
||||||
}), true);
|
}), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ('serviceWorker' in navigator) {
|
||||||
|
navigator.serviceWorker.register('/sw.js').then(function(registration) {
|
||||||
|
console.log('Service worker registration succeeded:', registration);
|
||||||
|
}).catch(function(error) {
|
||||||
|
console.log('Service worker registration failed:', error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
70
sw.js
Normal file
70
sw.js
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
const cacheName = "v1";
|
||||||
|
|
||||||
|
self.addEventListener("install", function(e) {
|
||||||
|
e.waitUntil(
|
||||||
|
caches.open(cacheName).then(function(cache) {
|
||||||
|
return cache.addAll([
|
||||||
|
'/',
|
||||||
|
'/style.css',
|
||||||
|
'/app.js',
|
||||||
|
'/jquery-3.3.1.min.js',
|
||||||
|
'/favicon.ico',
|
||||||
|
'/fonts/Parisine-Bold.eot',
|
||||||
|
'/fonts/Parisine-BoldItalic.eot',
|
||||||
|
'/fonts/Parisine-BoldItalic.woff',
|
||||||
|
'/fonts/Parisine-Bold.woff',
|
||||||
|
'/fonts/Parisine-Italic.ttf',
|
||||||
|
'/fonts/Parisine-Italic.woff',
|
||||||
|
'/fonts/Parisine-Regular.eot',
|
||||||
|
'/fonts/Parisine-Regular.woff',
|
||||||
|
'/bootstrap.min.css',
|
||||||
|
'/logo.svg',
|
||||||
|
]);
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
self.addEventListener("activate", function(e) {
|
||||||
|
e.waitUntil(
|
||||||
|
caches.keys().then(function(cacheNames) {
|
||||||
|
return Promise.all(cacheNames.map(function(cName) {
|
||||||
|
if (cName != cacheName) {
|
||||||
|
return caches.delete(cName);
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
self.addEventListener("fetch", function(e) {
|
||||||
|
var url = e.request.url.split("/");
|
||||||
|
|
||||||
|
e.respondWith(
|
||||||
|
caches.match(e.request)
|
||||||
|
.then(function(response) {
|
||||||
|
if (response) {
|
||||||
|
// Return cached version
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
var requestClone = e.request.clone();
|
||||||
|
return fetch(requestClone)
|
||||||
|
.then(function(response) {
|
||||||
|
|
||||||
|
if(!response || response.type !== 'basic') {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(url[3] == "api" && (url[4] == "traffic" || url[4] == "schedules"))) {
|
||||||
|
var responseClone = response.clone();
|
||||||
|
caches.open(cacheName).then(function(cache) {
|
||||||
|
cache.put(e.request, responseClone);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the response
|
||||||
|
return response;
|
||||||
|
})
|
||||||
|
})
|
||||||
|
);
|
||||||
|
})
|
Loading…
x
Reference in New Issue
Block a user