Skip to content

Commit ddf6553

Browse files
committed
Simplify service worker asset handling logic
Removed unused base path and manifest URL list logic from the service worker. Updated asset request creation to remove 'no-cache' option and simplified navigation request handling for cached index.html.
1 parent 8886dee commit ddf6553

1 file changed

Lines changed: 3 additions & 9 deletions

File tree

SoundTest/wwwroot/service-worker.published.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,14 @@ const cacheName = `${cacheNamePrefix}${self.assetsManifest.version}`;
1111
const offlineAssetsInclude = [/\.dll$/, /\.pdb$/, /\.wasm/, /\.html/, /\.js$/, /\.json$/, /\.css$/, /\.woff$/, /\.png$/, /\.jpe?g$/, /\.gif$/, /\.ico$/, /\.blat$/, /\.dat$/];
1212
const offlineAssetsExclude = [/^service-worker\.js$/];
1313

14-
// Replace with your base path if you are hosting on a subfolder. Ensure there is a trailing '/'.
15-
const base = "/";
16-
const baseUrl = new URL(base, self.origin);
17-
const manifestUrlList = self.assetsManifest.assets.map(asset => new URL(asset.url, baseUrl).href);
18-
1914
async function onInstall(event) {
2015
console.info('Service worker: Install');
2116

2217
// Fetch and cache all matching items from the assets manifest
2318
const assetsRequests = self.assetsManifest.assets
2419
.filter(asset => offlineAssetsInclude.some(pattern => pattern.test(asset.url)))
2520
.filter(asset => !offlineAssetsExclude.some(pattern => pattern.test(asset.url)))
26-
.map(asset => new Request(asset.url, {integrity: asset.hash, cache: 'no-cache'}));
21+
.map(asset => new Request(asset.url, { integrity: asset.hash }));
2722
await caches.open(cacheName).then(cache => cache.addAll(assetsRequests));
2823
}
2924

@@ -40,10 +35,9 @@ async function onActivate(event) {
4035
async function onFetch(event) {
4136
let cachedResponse = null;
4237
if (event.request.method === 'GET') {
43-
// For all navigation requests, try to serve index.html from cache,
44-
// unless that request is for an offline resource.
38+
// For all navigation requests, try to serve index.html from cache
4539
// If you need some URLs to be server-rendered, edit the following check to exclude those URLs
46-
const shouldServeIndexHtml = event.request.mode === 'navigate' && !manifestUrlList.some(url => url === event.request.url);
40+
const shouldServeIndexHtml = event.request.mode === 'navigate';
4741

4842
const request = shouldServeIndexHtml ? 'index.html' : event.request;
4943
const cache = await caches.open(cacheName);

0 commit comments

Comments
 (0)