-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.js
106 lines (96 loc) · 2.86 KB
/
worker.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
'use strict';
/**
* @description cacheVersion
* This is not fiction. When you have your own already
* production running and you need to update frontend
* code. You will need only to change this number
* increment 1 for example old 1 replace with `2`
* for `cacheVersion`.
* @param cacheVersion
*/
var cacheVersion = 2;
var cacheName = 'matrix-engine-' + cacheVersion;
try {
for (var j = 0;j < cacheVersion;j++) {
var oldCacheName = 'matrix-engine-' + j;
caches.delete(oldCacheName);
}
}
catch(e) {}
const offlineUrl = 'offline.html';
self.addEventListener('install', function (event) {
self.skipWaiting();
event.waitUntil(
caches.open(cacheName).then(function (cache) {
return cache.addAll([
offlineUrl,
'builds/app.js'
]);
})
);
});
self.addEventListener('fetch', function (event) {
if (event.request.method === 'POST') {
return;
}
event.respondWith(
caches.open(cacheName).then(function (cache) {
return cache.match(event.request).then(function (response) {
return (
response ||
fetch(event.request).then(function (response) {
if (response.status == 206) {
// statusText: "Partial Content"
return response;
} else {
cache.put(event.request, response.clone());
}
return response;
})
);
});
})
);
});
// self.addEventListener('fetch', (event) => {
// event.respondWith(
// caches.match(event.request).then((resp) => {
// return (
// resp ||
// fetch(event.request).then((response) => {
// return caches.open(cacheName).then((cache) => {
// if (response.status == 206) {
// // statusText: "Partial Content"
// return response;
// } else {
// cache.put(event.request, response.clone());
// }
// return response;
// });
// })
// );
// })
// );
// });
const fireAddToHomeScreenImpression = (event) => {
fireTracking('Add to homescreen shown');
// will not work for chrome, untill fixed
event.userChoice.then((choiceResult) => {
fireTracking(`User clicked ${choiceResult}`);
});
// This is to prevent `beforeinstallprompt` event that triggers again on `Add` or `Cancel` click
self.removeEventListener('beforeinstallprompt', fireAddToHomeScreenImpression);
};
self.addEventListener('beforeinstallprompt', fireAddToHomeScreenImpression);
//Track from where your web app has been opened/browsed
self.addEventListener('load', () => {
let trackText;
if (navigator && navigator.standalone) {
trackText = 'Launched: Installed (iOS)';
} else if (matchMedia('(display-mode: standalone)').matches) {
trackText = 'Launched: Installed';
} else {
trackText = 'Launched: Browser Tab';
}
fireTracking(track);
});