-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
78 lines (74 loc) · 2.89 KB
/
Copy pathsw.js
File metadata and controls
78 lines (74 loc) · 2.89 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
76
77
78
// VERSION is replaced with the deploy commit SHA by .github/workflows/deploy.yml.
// Locally it stays 'dev', which still busts the cache on every vendor/edit
// commit made to this file; for iterative local testing, hard-refresh
// (or unregister the service worker) instead of relying on this value to change.
const VERSION = '__DEPLOY_VERSION__';
// Deliberately not compared against the literal placeholder string above:
// the deploy workflow's sed replaces every occurrence of that literal, which
// would also rewrite this check and always report "deployed". Substring
// match on the un-prefixed word survives the replacement instead.
const CACHE_NAME = 'pyscript-template-' + (VERSION.indexOf('DEPLOY_VERSION') !== -1 ? 'dev' : 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;
});
}));
});