From 4f66ec87587fb1d4dd459274e293e810df5e48a0 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Tue, 20 May 2025 10:23:00 -0700 Subject: [PATCH 1/2] [ticket/17513] Wrap async logic inside event.waitUntil in push worker PHPBB-17513 --- phpBB/styles/all/js/push_worker.js.twig | 59 ++++++++++++++----------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/phpBB/styles/all/js/push_worker.js.twig b/phpBB/styles/all/js/push_worker.js.twig index cf0ca568ac..1d57d5c598 100644 --- a/phpBB/styles/all/js/push_worker.js.twig +++ b/phpBB/styles/all/js/push_worker.js.twig @@ -34,41 +34,48 @@ self.addEventListener('push', event => { notificationVersion = parseInt(notificationData.version, 10); pushToken = notificationData.token; } catch { - self.registration.showNotification(event.data.text()); + event.waitUntil(self.registration.showNotification(event.data.text())); return; } - const getNotificationUrl = '{{ U_WEBPUSH_GET_NOTIFICATION }}'; - const assetsVersion = parseInt('{{ ASSETS_VERSION }}', 10); + event.waitUntil((async () => { + const getNotificationUrl = '{{ U_WEBPUSH_GET_NOTIFICATION }}'; + const assetsVersion = parseInt('{{ ASSETS_VERSION }}', 10); - // Force update if versions differ - if (assetsVersion !== notificationVersion) { - self.registration.update(); - } + // Force update if versions differ + if (assetsVersion !== notificationVersion) { + await self.registration.update(); + } - const formData = new FormData(); - formData.append('item_id', itemId.toString(10)); - formData.append('type_id', typeId.toString(10)); - formData.append('user_id', userId.toString(10)); - formData.append('token', pushToken); + const formData = new FormData(); + formData.append('item_id', itemId.toString(10)); + formData.append('type_id', typeId.toString(10)); + formData.append('user_id', userId.toString(10)); + formData.append('token', pushToken); - fetch(getNotificationUrl, { - method: 'POST', - headers: { - 'X-Requested-With': 'XMLHttpRequest', - }, - body: formData, - }) - .then(response => response.json()) - .then(response => { - const responseBody = response.title + '\n' + response.text; + try { + const response = await fetch(getNotificationUrl, { + method: 'POST', + headers: { + 'X-Requested-With': 'XMLHttpRequest', + }, + body: formData, + }); + + const responseData = await response.json(); + + const responseBody = responseData.title + '\n' + responseData.text; const options = { body: responseBody, - data: response, - icon: response.avatar.src, + data: responseData, + icon: responseData.avatar.src, }; - self.registration.showNotification(response.heading, options); - }); + + await self.registration.showNotification(responseData.heading, options); + } catch (e) { + console.error('Push error:', e); + } + })()); }); /** From 6b6b5ffc28a9473a1498391804a9aa242c218e4f Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Tue, 20 May 2025 10:32:47 -0700 Subject: [PATCH 2/2] [ticket/17513] Fix JS linting PHPBB-17513 --- phpBB/styles/all/js/push_worker.js.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/styles/all/js/push_worker.js.twig b/phpBB/styles/all/js/push_worker.js.twig index 1d57d5c598..115c509427 100644 --- a/phpBB/styles/all/js/push_worker.js.twig +++ b/phpBB/styles/all/js/push_worker.js.twig @@ -38,7 +38,7 @@ self.addEventListener('push', event => { return; } - event.waitUntil((async () => { + event.waitUntil((async() => { const getNotificationUrl = '{{ U_WEBPUSH_GET_NOTIFICATION }}'; const assetsVersion = parseInt('{{ ASSETS_VERSION }}', 10);