Skip to content

Commit

Permalink
Merge pull request #156 from unoplatform/dev/jela/pwa-update-1.0
Browse files Browse the repository at this point in the history
Update PWA caching
  • Loading branch information
jeromelaban authored Nov 27, 2019
2 parents 4e73e2e + 9aeac02 commit 680e650
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gitversion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ branches:
regex: ^(pull|pull\-requests|pr)[/-]
mode: ContinuousDeployment
tag: PullRequest
increment: Inherit
increment: None

stable:
regex: release/stable/.*
Expand Down
13 changes: 8 additions & 5 deletions src/Uno.Wasm.Bootstrap/ShellTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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 || {{}};");

Expand Down
20 changes: 13 additions & 7 deletions src/Uno.Wasm.Bootstrap/WasmScripts/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
}
}());
});
19 changes: 14 additions & 5 deletions src/Uno.Wasm.Bootstrap/WasmScripts/uno-bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}
}
}

0 comments on commit 680e650

Please sign in to comment.