From bb28ecaff765ca2ac70358a6c88c4118a2c4fec5 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Thu, 7 Mar 2019 21:14:50 +0200 Subject: [PATCH] Use async/await in service worker, do not wait for cache open/put for successful requests --- client/service-worker.js | 45 +++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/client/service-worker.js b/client/service-worker.js index 18c67fb4..e05df454 100644 --- a/client/service-worker.js +++ b/client/service-worker.js @@ -2,6 +2,7 @@ /* global clients */ "use strict"; +const cacheName = "thelounge"; const excludedPathsFromCache = /^(?:socket\.io|storage|uploads|cdn-cgi)\//; self.addEventListener("install", function() { @@ -36,31 +37,33 @@ self.addEventListener("fetch", function(event) { uri.hash = ""; uri.search = ""; - event.respondWith(networkOrCache(uri)); + event.respondWith(networkOrCache(event, uri)); }); -function networkOrCache(uri) { - return caches.open("thelounge").then(function(cache) { - // Despite the "no-cache" name, it is a conditional request if proper headers are set - return fetch(uri, {cache: "no-cache"}) - .then(function(response) { - if (response.ok) { - return cache.put(uri, response.clone()).then(function() { - return response; - }); - } +async function putInCache(uri, response) { + const cache = await caches.open(cacheName); + await cache.put(uri, response); +} - // eslint-disable-next-line no-console - console.error(`Request for ${uri.href} failed with HTTP ${response.status}`); +async function networkOrCache(event, uri) { + try { + const response = await fetch(uri, {cache: "no-cache"}); - return Promise.reject("request-failed"); - }) - .catch(function() { - return cache.match(uri).then(function(matching) { - return matching || Promise.reject("request-not-in-cache"); - }); - }); - }); + if (response.ok) { + event.waitUntil(putInCache(uri, response)); + return response.clone(); + } + + throw new Error(`Request failed with HTTP ${response.status}`); + } catch (e) { + // eslint-disable-next-line no-console + console.error(e.message, uri.href); + + const cache = await caches.open(cacheName); + const matching = await cache.match(uri); + + return matching || Promise.reject("request-not-in-cache"); + } } self.addEventListener("message", function(event) {