Merge pull request #6631 from marc1706/ticket/17321

[ticket/17321] Add versioning to push worker to force updates
This commit is contained in:
Marc Alexander 2024-06-04 20:16:41 +02:00 committed by GitHub
commit df0dcae3ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 1 deletions

View file

@ -210,6 +210,7 @@ class webpush extends messenger_base implements extended_method_interface
$data = [
'item_id' => $notification->item_id,
'type_id' => $notification->notification_type_id,
'version' => $this->config['assets_version'],
];
$json_data = json_encode($data);

View file

@ -138,6 +138,7 @@ class webpush
// @todo: only work for logged in users, no anonymous & bot
$content = $this->template->render('push_worker.js.twig', [
'U_WEBPUSH_GET_NOTIFICATION' => $this->controller_helper->route('phpbb_ucp_push_get_notification_controller'),
'ASSETS_VERSION' => $this->config['assets_version'],
]);
$response = new Response($content);

View file

@ -1,3 +1,18 @@
/**
* Event listener for install event
*/
self.addEventListener('install', () => {
// Call to ensure service worker is correctly updated
self.skipWaiting();
});
/**
* Event listener for activate event
*/
self.addEventListener('activate', event => {
event.waitUntil(self.clients.claim());
});
/**
* Event listener for push event
*/
@ -7,17 +22,25 @@ self.addEventListener('push', event => {
}
let itemId = 0;
let typeId = 0;
let typeId = 0;
let notificationVersion = 5;
try {
const notificationData = event.data.json();
itemId = notificationData.item_id;
typeId = notificationData.type_id;
notificationVersion = parseInt(notificationData.version, 10);
} catch {
self.registration.showNotification(event.data.text());
return;
}
const getNotificationUrl = '{{ U_WEBPUSH_GET_NOTIFICATION }}';
const assetsVersion = parseInt('{{ ASSETS_VERSION }}', 10);
// Force update if versions differ
if (assetsVersion !== notificationVersion) {
self.registration.update();
}
const formData = new FormData();
formData.append('item_id', itemId.toString(10));