Skip to content

Commit

Permalink
sw: fix static path
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Patil <[email protected]>
  • Loading branch information
pulsejet committed Nov 25, 2023
1 parent a4ae2c8 commit 7c94a4e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
16 changes: 15 additions & 1 deletion lib/Controller/OtherController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -171,7 +172,20 @@ public function static(string $name): Http\Response
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' => '/',
]);
Expand Down
8 changes: 4 additions & 4 deletions src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PrecacheEntry>;

// 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();
Expand Down

0 comments on commit 7c94a4e

Please sign in to comment.