From a666b508e5200dc965068581e828331d7bbf5ef4 Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Wed, 27 Nov 2019 10:15:47 -0500 Subject: [PATCH 1/4] Update service worker cache strategy - Use fetch then cache strategy - Use literal cache version to avoid using already cached version even if the service got updated --- src/Uno.Wasm.Bootstrap/ShellTask.cs | 8 ++++---- .../WasmScripts/service-worker.js | 16 ++++++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/Uno.Wasm.Bootstrap/ShellTask.cs b/src/Uno.Wasm.Bootstrap/ShellTask.cs index 6a2a5f506..4dbde323e 100644 --- a/src/Uno.Wasm.Bootstrap/ShellTask.cs +++ b/src/Uno.Wasm.Bootstrap/ShellTask.cs @@ -181,10 +181,10 @@ 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 workerBody = File.ReadAllText(Path.Combine(_distPath, "service-worker.js")); + + workerBody = workerBody.Replace("$(CACHE_KEY)", Path.GetFileName(_managedPath)); + workerBody += $"\r\n\r\n// {Path.GetFileName(_managedPath)}"; } /// diff --git a/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js b/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js index aa97e36fd..6a2930401 100644 --- a/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js +++ b/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js @@ -9,7 +9,7 @@ self.addEventListener('install', function (e) { .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,13 @@ 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, { ignoreSearch: true }); + } + }()); }); From c9324a407bdcf399cdcfc198a01eb3324967c2e3 Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Wed, 27 Nov 2019 10:27:34 -0500 Subject: [PATCH 2/4] Adjust service worker for relative uris --- src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js | 2 +- src/Uno.Wasm.Bootstrap/WasmScripts/uno-bootstrap.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js b/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js index 6a2930401..8d4558f59 100644 --- a/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js +++ b/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js @@ -5,7 +5,7 @@ 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); diff --git a/src/Uno.Wasm.Bootstrap/WasmScripts/uno-bootstrap.js b/src/Uno.Wasm.Bootstrap/WasmScripts/uno-bootstrap.js index fb753c037..2b98149db 100644 --- a/src/Uno.Wasm.Bootstrap/WasmScripts/uno-bootstrap.js +++ b/src/Uno.Wasm.Bootstrap/WasmScripts/uno-bootstrap.js @@ -495,7 +495,7 @@ if (typeof window === 'object' /* ENVIRONMENT_IS_WEB */) { if (config.enable_pwa && 'serviceWorker' in navigator) { console.log('Registering service worker now'); - navigator.serviceWorker.register('/service-worker.js') + navigator.serviceWorker.register('./service-worker.js') .then(function () { console.log('Service Worker Registered'); }); From de0d6645ddd1cf5d1363755e02aad2ad2f6edcbb Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Wed, 27 Nov 2019 10:29:48 -0500 Subject: [PATCH 3/4] Adjust registration --- src/Uno.Wasm.Bootstrap/ShellTask.cs | 7 +++++-- .../WasmScripts/service-worker.js | 4 +++- .../WasmScripts/uno-bootstrap.js | 19 ++++++++++++++----- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Uno.Wasm.Bootstrap/ShellTask.cs b/src/Uno.Wasm.Bootstrap/ShellTask.cs index 4dbde323e..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. - var workerBody = File.ReadAllText(Path.Combine(_distPath, "service-worker.js")); + 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 8d4558f59..21d09acf2 100644 --- a/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js +++ b/src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js @@ -30,7 +30,9 @@ self.addEventListener('fetch', event => { // cache content if needed. return await fetch(event.request); } catch (err) { - return caches.match(event.request, { ignoreSearch: true }); + 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 2b98149db..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'); + }); + } } } From 9aeac022eef43042752748ed231908a808014810 Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Wed, 27 Nov 2019 12:36:55 -0500 Subject: [PATCH 4/4] Adjust PR versioning --- gitversion.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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/.*