Reputation: 111
here is the process i would like to dev :
Behavior :
Now i would like that if userA modify the page :
but it seems that :
so my question is :
In other word : On page update, serviceWorker order subscriber to reload
such serviceworker code fail :
if (client.url == self.registration.scope && 'focus' in client) {
client.postMessage('reload'); // DOESN'T WORK
return client.focus();
}
Failing code on android/chrome for setinterval :
setInterval(async function(){
const response = await fetch('/lastupdatetimestamp', {
method: 'get',
headers: { 'Content-Type': 'application/json' }
});
const json= await response.json();
if(LASTUP<json.up.lastdate){
window.location.reload()
LASTUP=json.up.lastdate
}
}, 30000);
Any idea ?
Upvotes: 0
Views: 1450
Reputation: 111
I finally founded a first step :
server send push -> serviceworker show notification -> click on notification -> goes open window and reload
in the serviceWorker.js
self.addEventListener('push', function (event) { ... }
self.addEventListener('notificationclick', async function(event) {
event.notification.close();
await event.waitUntil(clients.matchAll({
type: "window",
includeUncontrolled: true
}).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == self.registration.scope && 'focus' in client) { //
client.postMessage('reload'); // <= TRIGGER 'RELOAD' EVENT HERE
return client.focus();
}
}
if (clients.openWindow) {
return clients.openWindow(self.registration.scope);
}
}));
});
in the app.js
function initPostMessageListener() {
console.log("event listener message")
navigator.serviceWorker.addEventListener('message', function(e) {
var message = e.data;
console.log("ON A UN MESSAGE")
switch (message) {
case 'reload':
window.location.reload(true);
break;
default:
console.warn("Message '" + message + "' not handled.");
break;
}
});
}
call initPostMessageListener() in the app.js where the SW is registered
So the SW.js handle the notificationClick then open the browser window if exist and reload it
Upvotes: 2