forked from matomo-org/matomo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offline-service-worker.js
175 lines (151 loc) · 6.23 KB
/
offline-service-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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var matomoAnalytics = {initialize: function (options) {
if ('object' !== typeof options) {
options = {};
}
var maxLimitQueue = options.queueLimit || 50;
var maxTimeLimit = options.timeLimit || (60 * 60 * 24); // in seconds...
// same as configured in in tracking_requests_require_authentication_when_custom_timestamp_newer_than
function getQueue()
{
return new Promise(function(resolve, reject) {
// do a thing, possibly async, then...
if (!indexedDB) {
reject(new Error('No support for IndexedDB'));
return;
}
var request = indexedDB.open("matomo", 1);
request.onerror = function() {
console.error("Error", request.error);
reject(new Error(request.error));
};
request.onupgradeneeded = function(event) {
console.log('onupgradeneeded')
var db = event.target.result;
if (!db.objectStoreNames.contains('requests')) {
db.createObjectStore('requests', {autoIncrement : true, keyPath: 'id'});
}
};
request.onsuccess = function(event) {
var db = event.target.result;
let transaction = db.transaction("requests", "readwrite");
let requests =transaction.objectStore("requests");
resolve(requests);
};
});
}
function syncQueue () {
// check something in indexdb
return getQueue().then(function (queue) {
queue.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor && navigator.onLine) {
cursor.continue();
var queueId = cursor.value.id;
var secondsQueuedAgo = ((Date.now() - cursor.value.created) / 1000);
secondsQueuedAgo = parseInt(secondsQueuedAgo, 10);
if (secondsQueuedAgo > maxTimeLimit) {
// too old
getQueue().then(function (queue) {
queue.delete(queueId);
});
return;
}
console.log("Cursor " + cursor.key);
var init = {
headers: cursor.value.headers,
method: cursor.value.method,
}
if (cursor.value.body) {
init.body = cursor.value.body;
}
if (cursor.value.url.includes('?')) {
cursor.value.url += '&cdo=' + secondsQueuedAgo;
} else if (init.body) {
// todo test if this actually works for bulk requests
init.body = init.body.replace('&idsite=', '&cdo=' + secondsQueuedAgo + '&idsite=');
}
fetch(cursor.value.url, init).then(function (response) {
console.log('server response', response);
if (response.status < 400) {
getQueue().then(function (queue) {
queue.delete(queueId);
});
}
}).catch(function (error) {
console.error('Send to Server failed:', error);
throw error
})
}
else {
console.log("No more entries!");
}
};
});
}
function limitQueueIfNeeded(queue)
{
var countRequest = queue.count();
countRequest.onsuccess = function(event) {
if (event.result > maxLimitQueue) {
// we delete only one at a time because of concurrency some other process might delete data too
queue.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
queue.delete(cursor.value.id);
limitQueueIfNeeded(queue);
}
}
}
}
}
self.addEventListener('sync', function(event) {
if (event.tag === 'matomoSync') {
syncQueue();
}
});
self.addEventListener('fetch', function (event) {
let isOnline = navigator.onLine;
let isTrackingRequest = (event.request.url.includes('/matomo.php')
|| event.request.url.includes('/piwik.php'));
let isTrackerRequest = event.request.url.endsWith('/matomo.js')
|| event.request.url.endsWith('/piwik.js');
if (isTrackerRequest) {
if (isOnline) {
syncQueue();
}
caches.open('matomo').then(function(cache) {
return cache.match(event.request).then(function (response) {
return response || fetch(event.request).then(function(response) {
cache.put(event.request, response.clone());
return response;
});
});
})
} else if (isTrackingRequest && isOnline) {
syncQueue();
event.respondWith(fetch(event.request));
} else if (isTrackingRequest && !isOnline) {
var headers = {};
for (const [header, value] of event.request.headers) {
headers[header] = value;
}
let requestInfo = {
url: event.request.url,
referrer : event.request.referrer,
method : event.request.method,
referrerPolicy : event.request.referrerPolicy,
headers : headers,
created: Date.now()
};
event.request.text().then(function (postData) {
requestInfo.body = postData;
getQueue().then(function (queue) {
queue.add(requestInfo);
limitQueueIfNeeded(queue);
return queue;
});
});
}
});
}
};