-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: separate classic and module service worker registration
- Loading branch information
Showing
4 changed files
with
127 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// As of Dec 2024, Firefox does not support ES6 modules in service workers, so we need to use importScripts | ||
// https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker#browser_compatibility | ||
importScripts("$(REMOTE_WEBAPP_PATH)$(REMOTE_BASE_PATH)/uno-config-script.js"); | ||
|
||
if (config.environmentVariables["UNO_BOOTSTRAP_DEBUGGER_ENABLED"] !== "True") { | ||
console.debug("[ServiceWorker] Initializing"); | ||
let uno_enable_tracing = config.uno_enable_tracing; | ||
|
||
self.addEventListener('install', function (e) { | ||
console.debug('[ServiceWorker] Installing offline worker'); | ||
e.waitUntil( | ||
caches.open('$(CACHE_KEY)').then(async function (cache) { | ||
console.debug('[ServiceWorker] Caching app binaries and content'); | ||
|
||
// Add files one by one to avoid failed downloads to prevent the | ||
// worker to fail installing. | ||
for (var i = 0; i < config.offline_files.length; i++) { | ||
try { | ||
if (uno_enable_tracing) { | ||
console.debug(`[ServiceWorker] cache ${key}`); | ||
} | ||
|
||
await cache.add(config.offline_files[i]); | ||
} | ||
catch (e) { | ||
console.debug(`[ServiceWorker] Failed to fetch ${config.offline_files[i]}`); | ||
} | ||
} | ||
|
||
// Add the runtime's own files to the cache. We cannot use the | ||
// existing cached content from the runtime as the keys contain a | ||
// hash we cannot reliably compute. | ||
var c = await fetch("$(REMOTE_WEBAPP_PATH)_framework/blazor.boot.json"); | ||
const monoConfigResources = (await c.json()).resources; | ||
|
||
var entries = { | ||
...(monoConfigResources.coreAssembly || {}) | ||
, ...(monoConfigResources.assembly || {}) | ||
, ...(monoConfigResources.lazyAssembly || {}) | ||
, ...(monoConfigResources.jsModuleWorker || {}) | ||
, ...(monoConfigResources.jsModuleGlobalization || {}) | ||
, ...(monoConfigResources.jsModuleNative || {}) | ||
, ...(monoConfigResources.jsModuleRuntime || {}) | ||
, ...(monoConfigResources.wasmNative || {}) | ||
, ...(monoConfigResources.icu || {}) | ||
, ...(monoConfigResources.coreAssembly || {}) | ||
}; | ||
|
||
for (var key in entries) { | ||
var uri = `$(REMOTE_WEBAPP_PATH)_framework/${key}`; | ||
|
||
if (uno_enable_tracing) { | ||
console.debug(`[ServiceWorker] cache ${uri}`); | ||
} | ||
|
||
await cache.add(uri); | ||
} | ||
}) | ||
); | ||
}); | ||
|
||
self.addEventListener('activate', event => { | ||
event.waitUntil(self.clients.claim()); | ||
}); | ||
|
||
self.addEventListener('fetch', event => { | ||
event.respondWith(async function () { | ||
try { | ||
// Network first mode to get fresh content every time, then fallback to | ||
// cache content if needed. | ||
return await fetch(event.request); | ||
} catch (err) { | ||
return caches.match(event.request).then(response => { | ||
return response || fetch(event.request); | ||
}); | ||
} | ||
}()); | ||
}); | ||
} | ||
else { | ||
// In development, always fetch from the network and do not enable offline support. | ||
// This is because caching would make development more difficult (changes would not | ||
// be reflected on the first load after each change). | ||
// It also breaks the hot reload feature because VS's browserlink is not always able to | ||
// inject its own framework in the served scripts and pages. | ||
self.addEventListener('fetch', () => { }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters