Skip to content

Node.js based forum software built for the modern web

feat: add onmessage handler for service worker

Julian Lam40fe4d88f90e
diff --git a/public/src/app.js b/public/src/app.js
index 599792838f..b5d4dddb8d 100644
--- a/public/src/app.js
+++ b/public/src/app.js
@@ -358,6 +358,20 @@ if (document.readyState === 'loading') {
if (!config.useragent.isSafari && 'serviceWorker' in navigator) {
navigator.serviceWorker.register(config.relative_path + '/service-worker.js', { scope: config.relative_path + '/' })
.then(function () {
+ navigator.serviceWorker.addEventListener('message', (event) => {
+ const { action, url } = event.data;
+ switch (action) {
+ case 'ajaxify': {
+ const check = ajaxify.check(url);
+ if (check) {
+ ajaxify.go(url);
+ } else {
+ window.location.href = url;
+ }
+ }
+ }
+ });
+
console.info('ServiceWorker registration succeeded.');
}).catch(function (err) {
console.info('ServiceWorker registration failed: ', err);