-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
40 lines (37 loc) · 1.29 KB
/
sw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"use strict";
var cacheVersion = 4.9;
var currentCache = "offline" + cacheVersion;
const offlineUrl = "oof.html";
//sw install
self.addEventListener("install", function(event) {
// Put `offline.html` page into cache
var offlineRequest = new Request("oof.html");
event.waitUntil(
fetch(offlineRequest).then(function(response) {
return caches.open("offline").then(function(cache) {
return cache.put(offlineRequest, response);
});
})
);
});
//fetch event
self.addEventListener("fetch", function(event) {
// Only fall back for HTML documents.
var request = event.request;
// && request.headers.get('accept').includes('text/html')
if (request.method === "GET") {
// `fetch()` will use the cache when possible, to this examples
// depends on cache-busting URL parameter to avoid the cache.
event.respondWith(
fetch(request).catch(function(error) {
// `fetch()` throws an exception when the server is unreachable but not
// for valid HTTP responses, even `4xx` or `5xx` range.
return caches.open("offline").then(function(cache) {
return cache.match("oof.html");
});
})
);
}
// Any other handlers come here. Without calls to `event.respondWith()` the
// request will be handled without the ServiceWorker.
});