Skip to content

Commit

Permalink
New approach to check webpush worker update on all pages. See
Browse files Browse the repository at this point in the history
  • Loading branch information
rxu committed May 26, 2024
1 parent 5d87722 commit fbf6ec2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
9 changes: 9 additions & 0 deletions event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static function getSubscribedEvents()
'core.ucp_notifications_output_notification_types_modify_template_vars' => 'load_template_data',
'core.ucp_display_module_before' => 'load_language',
'core.acp_main_notice' => 'compatibility_notice',
'core.page_header_after' => 'service_worker_url',
];
}

Expand Down Expand Up @@ -90,4 +91,12 @@ public function compatibility_notice()
{
$this->template->assign_var('S_WPN_COMPATIBILITY_NOTICE', phpbb_version_compare(PHPBB_VERSION, '4.0.0-dev', '>='));
}

/**
* Generate service worker URL globally for update
*/
public function service_worker_url()
{
$this->template->assign_var('U_WEBPUSH_WORKER_URL', $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'));
}
}
2 changes: 1 addition & 1 deletion notification/method/webpush.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ protected function clean_expired_subscriptions(array $user_subscription_map, arr
* into https://myboard.url/path/to/avatar=123456789.gif
*
* @param string $avatar
* @return string Absolute path to avatar image
* @return array 'src' => Absolute path to avatar image
*/
protected function prepare_avatar($avatar)
{
Expand Down
27 changes: 27 additions & 0 deletions styles/all/template/update_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

function webpushWorkerUpdate() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register(serviceWorkerUrl)
.then((registration) => {
registration.update();
})
.catch(error => {
// Service worker could not be updated
console.info(error);
});
}
}

function domReady(callBack) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', callBack);
} else {
callBack();
}
}

domReady(() => {
/* global serviceWorkerUrl */
webpushWorkerUpdate();
});
3 changes: 1 addition & 2 deletions styles/all/template/webpush.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ function PhpbbWebpush() {

if ('serviceWorker' in navigator && 'PushManager' in window) {
navigator.serviceWorker.register(serviceWorkerUrl)
.then((registration) => {
.then(() => {
subscribeButton.addEventListener('click', subscribeButtonHandler);
unsubscribeButton.addEventListener('click', unsubscribeButtonHandler);
registration.update();

updateButtonState();
})
Expand Down
4 changes: 4 additions & 0 deletions styles/prosilver/template/event/overall_footer_after.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
var serviceWorkerUrl = '{{ U_WEBPUSH_WORKER_URL }}';
</script>
{% INCLUDEJS('@phpbb_webpushnotifications/update_worker.js') %}
1 change: 1 addition & 0 deletions tests/event/listener_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public function test_getSubscribedEvents()
'core.ucp_notifications_output_notification_types_modify_template_vars',
'core.ucp_display_module_before',
'core.acp_main_notice',
'core.page_header_after',
], array_keys(\phpbb\webpushnotifications\event\listener::getSubscribedEvents()));
}

Expand Down

0 comments on commit fbf6ec2

Please sign in to comment.