From 62c8d6574c8b811f78fe7c6784a6307982d34dc8 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 23 Oct 2020 12:58:45 +0200 Subject: [PATCH] fix index.html not being restored from cache when offline on #hash url --- src/service-worker.template.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/service-worker.template.js b/src/service-worker.template.js index 52d508ff..903589a8 100644 --- a/src/service-worker.template.js +++ b/src/service-worker.template.js @@ -84,16 +84,17 @@ function isCacheableThumbnail(url) { return false; } +const baseURL = new URL(self.registration.scope); async function handleRequest(request) { - const baseURL = self.registration.scope; - if (request.url === baseURL) { - request = new Request(new URL("index.html", baseURL)); + const url = new URL(request.url); + if (url.origin === baseURL.origin && url.pathname === baseURL.pathname) { + request = new Request(new URL("index.html", baseURL.href)); } let response = await readCache(request); if (!response) { // use cors so the resource in the cache isn't opaque and uses up to 7mb // https://developers.google.com/web/tools/chrome-devtools/progressive-web-apps?utm_source=devtools#opaque-responses - if (isCacheableThumbnail(new URL(request.url))) { + if (isCacheableThumbnail(url)) { response = await fetch(request, {mode: "cors", credentials: "omit"}); } else { response = await fetch(request);