Remove htaccess for serviceWorker.js
authorCyperghost <olaf_schmitz_1@t-online.de>
Tue, 20 Feb 2024 12:03:29 +0000 (13:03 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Fri, 23 Feb 2024 13:43:02 +0000 (14:43 +0100)
Adding the header `Service-Worker-Allowed: /` throw the php script

com.woltlab.wcf/templates/headIncludeJavaScript.tpl
wcfsetup/install/files/js/.htaccess [deleted file]
wcfsetup/install/files/js/serviceWorker.js [deleted file]
wcfsetup/install/files/service-worker/index.php [new file with mode: 0644]

index b51fbc7b305f7b7bfa87606f1c3e9a0c977237ae..6f0ee4bb03263e120918d131c98481b648ed5e3d 100644 (file)
@@ -78,7 +78,7 @@ window.addEventListener('pageshow', function(event) {
                        {if $__wcf->user->userID && SERVICE_WORKER_PUBLIC_KEY !== ''}
                        serviceWorker: {
                                publicKey: '{@SERVICE_WORKER_PUBLIC_KEY|encodeJS}',
-                               serviceWorkerJsUrl: '{$__wcf->getPath('wcf')}js/serviceWorker.js',
+                               serviceWorkerJsUrl: '{$__wcf->getPath('wcf')}js/service-worker/',
                                registerUrl: '{link controller="RegisterServiceWorker"}{/link}',
                        },
                        {/if}
diff --git a/wcfsetup/install/files/js/.htaccess b/wcfsetup/install/files/js/.htaccess
deleted file mode 100644 (file)
index 36f874d..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-<IfModule mod_headers.c>
-    <Files ~ "service-worker\.js">
-        Header set Service-Worker-Allowed: /
-    </Files>
-</IfModule>
diff --git a/wcfsetup/install/files/js/serviceWorker.js b/wcfsetup/install/files/js/serviceWorker.js
deleted file mode 100644 (file)
index 3899715..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-/**
- * @author      Olaf Braun
- * @copyright   2001-2024 WoltLab GmbH
- * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- */
-
-self.addEventListener("push", (event) => {
-       if (!(self.Notification && self.Notification.permission === "granted")) {
-               return;
-       }
-       if (!event.data) {
-               return;
-       }
-
-       const payload = event.data.json();
-
-       event.waitUntil(
-               removeOldNotifications(payload.notificationID, payload.time).then(() =>
-                       self.registration.showNotification(payload.title, {
-                               body: payload.message,
-                               icon: payload.icon,
-                               timestamp: payload.time,
-                               tag: payload.notificationID,
-                               data: {
-                                       url: payload.url,
-                                       time: payload.time,
-                               },
-                       }),
-               ),
-       );
-});
-
-self.addEventListener("notificationclick", (event) => {
-       event.notification.close();
-
-       event.waitUntil(self.clients.openWindow(event.notification.data.url));
-});
-
-async function removeOldNotifications(notificationID, time) {
-       const notifications = await self.registration.getNotifications({ tag: notificationID });
-       // Close old notifications
-       notifications
-               .filter((notifications) => {
-                       if (!notifications.data || !notifications.data.time) {
-                               return false;
-                       }
-                       return notifications.data.time <= time;
-               })
-               .forEach((notification) => {
-                       notification.close();
-               });
-}
diff --git a/wcfsetup/install/files/service-worker/index.php b/wcfsetup/install/files/service-worker/index.php
new file mode 100644 (file)
index 0000000..01c6491
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+@\header('Service-Worker-Allowed: /');
+?>
+/**
+ * @author      Olaf Braun
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ */
+
+self.addEventListener("push", (event) => {
+       if (!(self.Notification && self.Notification.permission === "granted")) {
+               return;
+       }
+       if (!event.data) {
+               return;
+       }
+
+       const payload = event.data.json();
+
+       event.waitUntil(
+               removeOldNotifications(payload.notificationID, payload.time).then(() =>
+                       self.registration.showNotification(payload.title, {
+                               body: payload.message,
+                               icon: payload.icon,
+                               timestamp: payload.time,
+                               tag: payload.notificationID,
+                               data: {
+                                       url: payload.url,
+                                       time: payload.time,
+                               },
+                       }),
+               ),
+       );
+});
+
+self.addEventListener("notificationclick", (event) => {
+       event.notification.close();
+
+       event.waitUntil(self.clients.openWindow(event.notification.data.url));
+});
+
+async function removeOldNotifications(notificationID, time) {
+       const notifications = await self.registration.getNotifications({ tag: notificationID });
+       // Close old notifications
+       notifications
+               .filter((notifications) => {
+                       if (!notifications.data || !notifications.data.time) {
+                               return false;
+                       }
+                       return notifications.data.time <= time;
+               })
+               .forEach((notification) => {
+                       notification.close();
+               });
+}