Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
75 lines (71 loc) · 2.62 KB
/
Copy pathsw.js
File metadata and controls
75 lines (71 loc) · 2.62 KB
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
// Every file this worker serves comes from the cache once it has been
// fetched, so a browser that has already visited keeps the old copies. Change
// VERSION whenever you change a file: the next visit installs a worker with a
// new cache and the old one is deleted. While you are editing, a hard reload
// skips the worker instead (see docs/guides/make-it-installable.md, "Edits that don't show").
const VERSION = '2';
const CACHE_NAME = 'pyscript-template-' + VERSION;
const SHELL = [
'./',
'./index.html',
'./main.py',
'./pyscript.json',
'./manifest.json',
'./style.css',
'./pwa.js',
'./icon-192.png',
'./icon-512.png',
'./vendor/pyscript/core.css',
'./vendor/pyscript/core.js',
];
self.addEventListener('install', function (event) {
event.waitUntil(caches.open(CACHE_NAME).then(function (cache) {
return cache.addAll(SHELL);
}).then(function () {
return self.skipWaiting();
}));
});
self.addEventListener('activate', function (event) {
event.waitUntil(caches.keys().then(function (names) {
return Promise.all(names.filter(function (name) {
return name !== CACHE_NAME;
}).map(function (name) {
return caches.delete(name);
}));
}).then(function () {
return self.clients.claim();
}));
});
// On the first visit the page starts loading before this worker controls it,
// so whatever it fetched in that window (PyScript's hashed chunks, the
// interpreter) never passed through the fetch handler below. pwa.js posts
// the URLs the page loaded once the worker is ready; cache the missing ones
// so the first visit alone is enough to launch offline.
self.addEventListener('message', function (event) {
var data = event.data || {};
if (data.type !== 'cache-urls' || !Array.isArray(data.urls)) return;
event.waitUntil(caches.open(CACHE_NAME).then(function (cache) {
return Promise.all(data.urls.map(function (url) {
return cache.match(url).then(function (hit) {
if (hit) return;
return fetch(url).then(function (response) {
if (response && response.status === 200) return cache.put(url, response);
}).catch(function () {});
});
}));
}));
});
self.addEventListener('fetch', function (event) {
if (event.request.method !== 'GET') return;
event.respondWith(caches.match(event.request).then(function (cached) {
if (cached) return cached;
return fetch(event.request).then(function (response) {
if (!response || response.status !== 200) return response;
var copy = response.clone();
caches.open(CACHE_NAME).then(function (cache) {
cache.put(event.request, copy);
});
return response;
});
}));
});