diff --git a/gitversion.yml b/gitversion.yml index 59fc3ed63..62ae1e5aa 100644 --- a/gitversion.yml +++ b/gitversion.yml @@ -12,7 +12,7 @@ branches: regex: ^(pull|pull\-requests|pr)[/-] mode: ContinuousDeployment tag: PullRequest - increment: Inherit + increment: None stable: regex: release/stable/.* diff --git a/src/Uno.Wasm.Bootstrap/ShellTask.cs b/src/Uno.Wasm.Bootstrap/ShellTask.cs index 6a2a5f506..6acd0ac7a 100644 --- a/src/Uno.Wasm.Bootstrap/ShellTask.cs +++ b/src/Uno.Wasm.Bootstrap/ShellTask.cs @@ -181,10 +181,13 @@ private void TouchServiceWorker() { // The service worker file must change to be reloaded properly, add the dist digest // as cache trasher. - using (var stream = new StreamWriter(File.Open(Path.Combine(_distPath, "service-worker.js"), FileMode.Append))) - { - stream.WriteLine($"// {Path.GetFileName(_managedPath)}"); - } + var workerFilePath = Path.Combine(_distPath, "service-worker.js"); + var workerBody = File.ReadAllText(workerFilePath); + + workerBody = workerBody.Replace("$(CACHE_KEY)", Path.GetFileName(_managedPath)); + workerBody += $"\r\n\r\n// {Path.GetFileName(_managedPath)}"; + + File.WriteAllText(workerFilePath, workerBody); } /// @@ -902,7 +905,7 @@ private void GenerateConfig() config.AppendLine($"config.files_integrity = {{{filesIntegrityStr}}};"); config.AppendLine($"config.total_assemblies_size = {totalAssembliesSize};"); config.AppendLine($"config.enable_pwa = {enablePWA.ToString().ToLowerInvariant()};"); - config.AppendLine($"config.offline_files = [{offlineFiles}];"); + config.AppendLine($"config.offline_files = ['./', {offlineFiles}];"); config.AppendLine($"config.environmentVariables = config.environmentVariables || {{}};"); diff --git a/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js b/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js index aa97e36fd..21d09acf2 100644 --- a/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js +++ b/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js @@ -5,11 +5,11 @@ let config = {}; self.addEventListener('install', function (e) { console.debug('[ServiceWorker] Installing offline worker'); e.waitUntil( - fetch("/uno-config.js") + fetch("./uno-config.js") .then(r => r.text() .then(configStr => { eval(configStr); - caches.open(config.uno_remote_managedpath).then(function (cache) { + caches.open('$(CACHE_KEY)').then(function (cache) { console.debug('[ServiceWorker] Caching app binaries and content'); return cache.addAll(config.offline_files); }); @@ -24,9 +24,15 @@ self.addEventListener('activate', event => { }); self.addEventListener('fetch', event => { - event.respondWith( - caches.match(event.request, { ignoreSearch: true }).then(response => { - return response || fetch(event.request); - }) - ); + 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); + }); + } + }()); }); diff --git a/src/Uno.Wasm.Bootstrap/WasmScripts/uno-bootstrap.js b/src/Uno.Wasm.Bootstrap/WasmScripts/uno-bootstrap.js index fb753c037..01ae08f8a 100644 --- a/src/Uno.Wasm.Bootstrap/WasmScripts/uno-bootstrap.js +++ b/src/Uno.Wasm.Bootstrap/WasmScripts/uno-bootstrap.js @@ -494,10 +494,19 @@ if (typeof window === 'object' /* ENVIRONMENT_IS_WEB */) { document.addEventListener("DOMContentLoaded", () => App.preInit()); if (config.enable_pwa && 'serviceWorker' in navigator) { - console.log('Registering service worker now'); - navigator.serviceWorker.register('/service-worker.js') - .then(function () { - console.log('Service Worker Registered'); - }); + if (navigator.serviceWorker.controller) { + console.debug("Active service worker found, skipping register"); + } else { + console.debug('Registering service worker now'); + + navigator.serviceWorker + .register( + './service-worker.js', { + scope: "./" + }) + .then(function () { + console.debug('Service Worker Registered'); + }); + } } }