From 7c94a4efcc120a59d84d19d6b8f1d76347fd08d6 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sat, 25 Nov 2023 12:41:22 -0800 Subject: [PATCH] sw: fix static path Signed-off-by: Varun Patil --- lib/Controller/OtherController.php | 16 +++++++++++++++- src/service-worker.ts | 8 ++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/Controller/OtherController.php b/lib/Controller/OtherController.php index 356af3ce..516a8514 100644 --- a/lib/Controller/OtherController.php +++ b/lib/Controller/OtherController.php @@ -28,6 +28,7 @@ use OCA\Memories\Exceptions; use OCA\Memories\Settings\SystemConfig; use OCA\Memories\Util; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataDisplayResponse; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\StreamResponse; use OCP\IRequest; @@ -171,7 +172,20 @@ class OtherController extends GenericApiController throw Exceptions::NotFound('Service worker is disabled in global configuration'); } - $response = (new StreamResponse(__DIR__.'/../../js/memories-service-worker.js'))->setHeaders([ + // Get relative URL to JS web root of the app + $prefix = \OC::$server->get(\OCP\IURLGenerator::class)->linkTo('memories', 'js/memories-main.js'); + $prefix = preg_replace('/memories-main\.js.*$/', '', $prefix); + + // Make sure prefix starts and ends with a slash + $prefix = '/'.ltrim($prefix, '/'); + $prefix = rtrim($prefix, '/').'/'; + + // Replace relative URLs to have correct prefix + $sw = file_get_contents(__DIR__.'/../../js/memories-service-worker.js'); + $sw = str_replace('/apps/memories/js/', $prefix, $sw); + + // Return processed service worker + $response = (new DataDisplayResponse($sw))->setHeaders([ 'Content-Type' => 'application/javascript', 'Service-Worker-Allowed' => '/', ]); diff --git a/src/service-worker.ts b/src/service-worker.ts index 632c1127..86eb4995 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -6,12 +6,12 @@ import { ExpirationPlugin } from 'workbox-expiration'; declare var self: ServiceWorkerGlobalScope; type PrecacheEntry = Exclude<(typeof self.__WB_MANIFEST)[number], string>; + +// Paths are updated in PHP. See OtherController.php const manifest = self.__WB_MANIFEST as Array; -// Exclude files that are not needed -const filteredManifest = manifest.filter((entry) => { - return !/(LICENSE\.txt|\.map)(\?.*)?$/.test(entry.url ?? String()); -}); +// Only include JS files +const filteredManifest = manifest.filter((entry) => /\.js(\?.*)?$/.test(entry.url)); precacheAndRoute(filteredManifest); cleanupOutdatedCaches();