[ticket/17321] Provide correct push worker update

PHPBB3-17321
This commit is contained in:
rxu 2024-05-27 11:30:15 +07:00
parent c6fd14352e
commit ad715dac08
No known key found for this signature in database
GPG key ID: 8117904FEDEFDD17
3 changed files with 52 additions and 1 deletions

View file

@ -0,0 +1,29 @@
'use strict';
function webpushWorkerUpdate() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.getRegistration(serviceWorkerUrl)
.then((registration) => {
registration.update();
})
.catch(error => {
// Service worker could not be updated
console.info(error);
});
}
}
// Do not redeclare function if exist
if (typeof domReady === 'undefined') {
window.domReady = function(callBack) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', callBack);
} else {
callBack();
}
}
}
domReady(() => {
/* global serviceWorkerUrl */
webpushWorkerUpdate();
});

View file

@ -3855,7 +3855,8 @@ function page_header($page_title = '', $display_online_list = false, $item_id =
// Output the notifications
$notifications = false;
if ($config['load_notifications'] && $config['allow_board_notifications'] && $user->data['user_id'] != ANONYMOUS && $user->data['user_type'] != USER_IGNORE)
$notifications_enabled = $config['load_notifications'] && $config['allow_board_notifications'] && $user->data['user_id'] != ANONYMOUS && $user->data['user_type'] != USER_IGNORE;
if ($notifications_enabled)
{
/* @var $phpbb_notifications \phpbb\notification\manager */
$phpbb_notifications = $phpbb_container->get('notification_manager');
@ -4032,6 +4033,20 @@ function page_header($page_title = '', $display_online_list = false, $item_id =
'SITE_LOGO_IMG' => $user->img('site_logo'),
));
$webpush_available = $notifications_enabled && $phpbb_notifications->get_method_class('notification.method.webpush')->is_available();
if ($webpush_available)
{
if (!$this->template->retrieve_var('U_WEBPUSH_WORKER_URL'))
{
$this->template->assign_var('U_WEBPUSH_WORKER_URL', $controller_helper->route('phpbb_ucp_push_worker_controller'));
}
if (!$this->template->retrieve_var('NOTIFICATIONS_WEBPUSH_ENABLE'))
{
$this->template->assign_var('NOTIFICATIONS_WEBPUSH_ENABLE', true);
}
}
$http_headers = array();
if ($send_headers)

View file

@ -117,6 +117,13 @@
</script>
<!-- ENDIF -->
{% if NOTIFICATIONS_WEBPUSH_ENABLE %}
<script>
const serviceWorkerUrl = '{{ U_WEBPUSH_WORKER_URL }}';
</script>
{% INCLUDEJS(T_ASSETS_PATH ~ '/javascript/update_worker.js') %}
{% endif %}
<!-- EVENT overall_footer_after -->
<!-- IF S_PLUPLOAD --><!-- INCLUDE plupload.html --><!-- ENDIF -->