-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathservice_worker_script.js
More file actions
50 lines (47 loc) · 1.2 KB
/
service_worker_script.js
File metadata and controls
50 lines (47 loc) · 1.2 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
var url= window.location.href;
var pos = url.search('using_pwa');
if (pos+1 && url[pos+8] && 'serviceWorker' in navigator) {
navigator.serviceWorker
.register('service-worker.js')
.then(function(registration) {
if (!navigator.serviceWorker.controller){
location.reload();
}
sendNewLocation();
})
.catch(function(err) {
console.log("Service Worker Failed to Register", err);
});
}
if ('serviceWorker' in navigator){
navigator.serviceWorker.addEventListener('message', function(e) {
if(e.data.type=='redirect') {
location.replace(e.data.url);
} else if(e.data.type='refresh') {
sendNewLocation();
}
});
}
function sendNewLocation() {
getCurrentGeolocation().then( function(latlng){
lng=latlng.lng();
lat=latlng.lat();
var urlGet = '/api/location/nearby/'+lng+'/'+lat;
var ajaxReq = $.ajax({
type: 'get',
url: urlGet,
dataType: 'json',
success: function(data, status, xhr){
navigator.serviceWorker.controller.postMessage(data);
},
error: function(xhr, reason, ex){
var sampleData={
name: '404',
id:'0'
};
navigator.serviceWorker.controller.postMessage(sampleData);
console.log("No nearby locations found at "+urlGet);
}
});
});
};