forked from dexie/dexie-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
144 lines (132 loc) · 4.77 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
---
layout: null
---
// Cache all css
//
const raw = [
"/",
"https://ghbtns.com/github-btn.html?user=dfahlander&repo=Dexie.js&type=star&count=true",
"https://buttons.github.io/buttons.js",
"https://unpkg.com/dexie",
"https://www.google-analytics.com/analytics.js",
{% for page in site.html_pages %}
"{{ page.url }}",
{% endfor %}
{%for file in site.static_files %}
"{{ file.path }}",
{% endfor %}
].filter(file => !file.startsWith("/test") && !file.includes("analytics.js"));
const CACHE_NAME = 'dexiejs-offline-cache';
async function waitForAll(promises){
return new Promise( resolve => {
let rc = { success: 0, failure:0 };
for (let i=0; i < promises.length; i++) {
promises[i]
.then(()=>{
rc.success++;
if (rc.success+rc.failure === promises.length) resolve(rc);
}).catch(err => {
rc.failure++;
if (rc.success+rc.failure === promises.length) resolve(rc);
console.error(`error occurred ${err}`);
});
}
});
}
/*
function isCurrentSite(url){
const u = new URL(url);
if (self.location.origin === u.origin){
return true;
}
return false;
}
function shortIfLocal(url){
const u = new URL(url);
if (isCurrentSite(u)) {
return u.pathname;
}
return u.href;
}
function correctUrlForLocal(url){
// try to construct url
let temp;
try{
temp = new URL(url);
} catch (err) {
try {
temp = new URL(self.location.origin+url);
}
catch(err2){
temp = undefined;
}
}
if (!temp) return url;
if (!isCurrentSite(temp)) return temp;
temp.pathname = temp.pathname.replace(/\/$/,'');
return temp;
}*/
self.addEventListener('install', function (event) {
event.waitUntil(
caches.delete(CACHE_NAME)
.then(() => caches.open(CACHE_NAME))
.then(async cache => {
console.log('Opened cache');
const leadingSlashRemoved = raw.map(url => url);
do {
// cache in batches of 10
const sliceDice = leadingSlashRemoved.splice(0, 50);
await waitForAll(sliceDice.map(async url => {
const response = await fetch(url);
//console.log("URL", url, response);
await cache.put(url, response);
}));
} while(leadingSlashRemoved.length > 0);
return true;
})
);
});
const MAX_WAIT = 300; // If network responds slower than 100 ms, respond with cache instead
self.addEventListener('fetch', function (event) {
// Start reading from cache
const cachedResponsePromise = caches.match(event.request);
const fetchPromise = fetch(event.request);
const timeoutPromise = new Promise(resolve => {
setTimeout(()=>resolve("timedout"), MAX_WAIT);
});
// Start fetching in parallell
event.respondWith(Promise.race([fetchPromise, timeoutPromise]).then(async res => {
// Fetch successful, probably online. See if we also have the cached response:
const cachedResponse = await cachedResponsePromise.catch(err => null);
if (res === "timedout" || !res.ok) {
// Fetch didn't throw but the result wasn't ok either.
// Could be timeout, a 404, 500 or maybe offline?
// In case we have an OK response in the cache, respond with that one instead:
if (cachedResponse && cachedResponse.ok) {
if (res === "timedout") console.log("URL", event.request.url, "timedout. Serving it from cache to speed up site");
return cachedResponse;
} else if (res === "timedout") {
// We don't have anything in cache. Wait for fetch even if it takes time.
res = await fetchPromise;
} else {
return res;
}
}
if (res.ok) {
// Should we update the cache with this fresh version?
if (!cachedResponse || (cachedResponse.headers.get("last-modified") !== res.headers.get("last-modified"))) {
// There were no cached response, or "last-modified" headers was changed - keep the cache up-to-date,
// so that, when the user goes offline, it will have the latest and greatest, and not revert to old versions
const cache = await caches.open(CACHE_NAME);
await cache.put(event.request, res.clone());
}
}
return res;
}, async error => {
const cachedResponse = await cachedResponsePromise.catch(err => null);
if (cachedResponse && cachedResponse.ok) {
return cachedResponse;
}
throw error;
}));
});