From d46feb704dadedac96f71150a7f8015e896a924f Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Thu, 9 Nov 2023 14:56:41 -0500 Subject: [PATCH] fix: Disable service worker in debug to work around service workers interfering with browserlink See https://developercommunity.visualstudio.com/t/BrowserLink-WebSocket-is-disconnecting-a/10500228 --- .../WasmScripts/service-worker.js | 78 +++++++++++-------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js b/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js index 53ab08b73..655f3dc3b 100644 --- a/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js +++ b/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js @@ -1,41 +1,51 @@ import { config } from "$(REMOTE_WEBAPP_PATH)$(REMOTE_BASE_PATH)/uno-config.js"; -console.debug("[ServiceWorker] Initializing"); +if (config.environmentVariables["UNO_BOOTSTRAP_DEBUGGER_ENABLED"] !== "True") { + console.debug("[ServiceWorker] Initializing"); -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'); + 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 { - await cache.add(config.offline_files[i]); + // 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 { + await cache.add(config.offline_files[i]); + } + catch (e) { + console.debug(`[ServiceWorker] Failed to fetch ${config.offline_files[i]}`); + } } - catch (e) { - console.debug(`[ServiceWorker] Failed to fetch ${config.offline_files[i]}`); - } - } - }) - ); -}); + }) + ); + }); -self.addEventListener('activate', event => { - event.waitUntil(self.clients.claim()); -}); + 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); - }); - } - }()); -}); + 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', () => { }); +}