Skip to content

Commit fbf6ec2

Browse files
committed
New approach to check webpush worker update on all pages. See
https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/update
1 parent 5d87722 commit fbf6ec2

6 files changed

Lines changed: 43 additions & 3 deletions

File tree

event/listener.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static function getSubscribedEvents()
3030
'core.ucp_notifications_output_notification_types_modify_template_vars' => 'load_template_data',
3131
'core.ucp_display_module_before' => 'load_language',
3232
'core.acp_main_notice' => 'compatibility_notice',
33+
'core.page_header_after' => 'service_worker_url',
3334
];
3435
}
3536

@@ -90,4 +91,12 @@ public function compatibility_notice()
9091
{
9192
$this->template->assign_var('S_WPN_COMPATIBILITY_NOTICE', phpbb_version_compare(PHPBB_VERSION, '4.0.0-dev', '>='));
9293
}
94+
95+
/**
96+
* Generate service worker URL globally for update
97+
*/
98+
public function service_worker_url()
99+
{
100+
$this->template->assign_var('U_WEBPUSH_WORKER_URL', $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'));
101+
}
93102
}

notification/method/webpush.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ protected function clean_expired_subscriptions(array $user_subscription_map, arr
445445
* into https://myboard.url/path/to/avatar=123456789.gif
446446
*
447447
* @param string $avatar
448-
* @return string Absolute path to avatar image
448+
* @return array 'src' => Absolute path to avatar image
449449
*/
450450
protected function prepare_avatar($avatar)
451451
{
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
function webpushWorkerUpdate() {
4+
if ('serviceWorker' in navigator) {
5+
navigator.serviceWorker.register(serviceWorkerUrl)
6+
.then((registration) => {
7+
registration.update();
8+
})
9+
.catch(error => {
10+
// Service worker could not be updated
11+
console.info(error);
12+
});
13+
}
14+
}
15+
16+
function domReady(callBack) {
17+
if (document.readyState === 'loading') {
18+
document.addEventListener('DOMContentLoaded', callBack);
19+
} else {
20+
callBack();
21+
}
22+
}
23+
24+
domReady(() => {
25+
/* global serviceWorkerUrl */
26+
webpushWorkerUpdate();
27+
});

styles/all/template/webpush.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ function PhpbbWebpush() {
5757

5858
if ('serviceWorker' in navigator && 'PushManager' in window) {
5959
navigator.serviceWorker.register(serviceWorkerUrl)
60-
.then((registration) => {
60+
.then(() => {
6161
subscribeButton.addEventListener('click', subscribeButtonHandler);
6262
unsubscribeButton.addEventListener('click', unsubscribeButtonHandler);
63-
registration.update();
6463

6564
updateButtonState();
6665
})
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script>
2+
var serviceWorkerUrl = '{{ U_WEBPUSH_WORKER_URL }}';
3+
</script>
4+
{% INCLUDEJS('@phpbb_webpushnotifications/update_worker.js') %}

tests/event/listener_test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public function test_getSubscribedEvents()
117117
'core.ucp_notifications_output_notification_types_modify_template_vars',
118118
'core.ucp_display_module_before',
119119
'core.acp_main_notice',
120+
'core.page_header_after',
120121
], array_keys(\phpbb\webpushnotifications\event\listener::getSubscribedEvents()));
121122
}
122123

0 commit comments

Comments
 (0)