From 63184d66b0c2d1a96eca1c2b277b332eb2c6964a Mon Sep 17 00:00:00 2001 From: Vinay Gawade Date: Mon, 22 Jan 2024 23:43:51 +0530 Subject: [PATCH] feat: minor improvements for performance --- docs/_headers | 3 ++ docs/index.html | 4 +-- docs/sw.js | 79 +++++++++++++++---------------------------------- 3 files changed, 28 insertions(+), 58 deletions(-) diff --git a/docs/_headers b/docs/_headers index 2a1bd07..c57e48b 100644 --- a/docs/_headers +++ b/docs/_headers @@ -1,2 +1,5 @@ +/* + Cache-Control: max-age=31536000 + /docs/images/* Cache-Control: public, max-age=604800 diff --git a/docs/index.html b/docs/index.html index 0095906..9e581e5 100755 --- a/docs/index.html +++ b/docs/index.html @@ -7,9 +7,7 @@ - - - + { - var now = Date.now() - var url = new URL(req.url) + var now = Date.now(); + var url = new URL(req.url); - // 1. fixed http URL - // Just keep syncing with location.protocol - // fetch(httpURL) belongs to active mixed content. - // And fetch(httpRequest) is not supported yet. - url.protocol = self.location.protocol + // 1. Fixed http URL + // Keep syncing with location.protocol + url.protocol = self.location.protocol; - // 2. add query for caching-busting. - // Github Pages served with Cache-Control: max-age=600 - // max-age on mutable content is error-prone, with SW life of bugs can even extend. - // Until cache mode of Fetch API landed, we have to workaround cache-busting with query string. + // 2. Add query for caching-busting. // Cache-Control-Bug: https://bugs.chromium.org/p/chromium/issues/detail?id=453190 if (url.hostname === self.location.hostname) { - url.search += (url.search ? '&' : '?') + 'cache-bust=' + now + url.search += (url.search ? '&' : '?') + 'cache-bust=' + now; } - return url.href -} + return url.href; +}; -/** - * @Lifecycle Activate - * New one activated when old isnt being used. - * - * waitUntil(): activating ====> activated - */ +// Lifecycle Activate self.addEventListener('activate', event => { - event.waitUntil(self.clients.claim()) -}) + event.waitUntil(self.clients.claim()); +}); -/** - * @Functional Fetch - * All network requests are being intercepted here. - * - * void respondWith(Promise r) - */ +// Functional Fetch self.addEventListener('fetch', event => { // Skip some of cross-origin requests, like those for Google Analytics. if (HOSTNAME_WHITELIST.indexOf(new URL(event.request.url).hostname) > -1) { - // Stale-while-revalidate - // similar to HTTP's stale-while-revalidate: https://www.mnot.net/blog/2007/12/12/stale - // Upgrade from Jake's to Surma's: https://gist.github.com/surma/eb441223daaedf880801ad80006389f1 - const cached = caches.match(event.request) - const fixedUrl = getFixedUrl(event.request) - const fetched = fetch(fixedUrl, { cache: 'no-store' }) - const fetchedCopy = fetched.then(resp => resp.clone()) + const cached = caches.match(event.request); + const fixedUrl = getFixedUrl(event.request); + const fetched = fetch(fixedUrl, { cache: 'no-store' }); + const fetchedCopy = fetched.then(resp => resp.clone()); - // Call respondWith() with whatever we get first. - // If the fetch fails (e.g disconnected), wait for the cache. - // If there’s nothing in cache, wait for the fetch. - // If neither yields a response, return offline pages. event.respondWith( Promise.race([fetched.catch(_ => cached), cached]) .then(resp => resp || fetched) - .catch(_ => { /* eat any errors */ }) - ) + .catch(_ => {}) + ); - // Update the cache with the version we fetched (only for ok status) event.waitUntil( Promise.all([fetchedCopy, caches.open(RUNTIME)]) .then(([response, cache]) => response.ok && cache.put(event.request, response)) - .catch(_ => { /* eat any errors */ }) - ) + .catch(_ => {}) + ); } -}) \ No newline at end of file +});