-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservice-worker.js
More file actions
104 lines (93 loc) · 3.06 KB
/
Copy pathservice-worker.js
File metadata and controls
104 lines (93 loc) · 3.06 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
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
// Replace 3.6.3 with the current version number of Workbox.
importScripts(
"https://storage.googleapis.com/workbox-cdn/releases/6.5.2/workbox-sw.js"
);
self.addEventListener("install", (event) => {
console.log("sw installed");
event.waitUntil(
caches.open("v1").then((cache) => {
return cache.addAll([
"index.html",
"styles.css",
"modules/map.js",
"modules/thirdparty/gpx.js",
"modules/thirdparty/leaflet.filelayer.js",
"modules/hikingTimeCalculation.js",
"modules/swissTopo.js",
"modules/applicationReset.js",
"modules/mapLayer.js",
"modules/recordMapLayer.js",
"modules/sampleRoute.js",
"modules/serviceWorkerRegister.js",
"modules/localStorageTracks.js",
"images/folder.svg",
"images/pin-icon-end.png",
"images/pin-icon-start.png",
"images/pin-icon-wpt.png",
"images/pin-shadow.png",
"https://storage.googleapis.com/workbox-cdn/releases/6.5.2/workbox-sw.js",
"https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.js",
]);
})
);
});
workbox.routing.registerRoute(
({ url }) => url.href.endsWith(".gpx"),
new workbox.strategies.CacheFirst({
cacheName: "gpx-file-cache",
})
);
workbox.routing.registerRoute(
({ url }) =>
url.href.indexOf("storage.googleapis.com/workbox-cdn/releases/") > 0,
new workbox.strategies.CacheFirst({
cacheName: "workbox-cache",
})
);
function registerMapCache(name, host) {
var checkMatch = function (event) {
var match = addMapTilesToCache && event.url.host == host;
// console.log("checking route:" + event.url + " its a match" + match, {
// addMapTilesToCache,
// match,
// });
return match;
};
workbox.routing.registerRoute(
checkMatch,
new workbox.strategies.CacheFirst({
cacheName: name,
plugins: [
new workbox.expiration.ExpirationPlugin({
// Only cache requests for two weeks
maxAgeSeconds: 2 * 7 * 24 * 60 * 60,
}),
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
],
})
);
}
const shareTargetHandler = async ({ event }) => {
console.log("Saving media locally...");
const formData = await event.request.formData();
const mediaFiles = formData.getAll("media");
const channel = new BroadcastChannel("file_dropped");
for (const mediaFile of mediaFiles) {
if (!mediaFile.name) {
console.log("Sorry! No name found on incoming media.");
continue;
}
channel.postMessage({ file: mediaFile });
}
return Response.redirect("/?shareTarget&fileCount=" + mediaFiles.length, 303);
};
registerMapCache("openstreetmap-cache", "b.tile.openstreetmap.org");
registerMapCache("swisstopo-cache", "wmts.geo.admin.ch");
workbox.routing.registerRoute("/share-target", shareTargetHandler, "POST");
let addMapTilesToCache = false;
const broadcastChannel = new BroadcastChannel("settings_isRecording");
broadcastChannel.onmessage = (event) => {
addMapTilesToCache = event.data.isRecording;
};