From 74d8a258fb4f31f5e8cf91880a94a4f629546e9d Mon Sep 17 00:00:00 2001 From: David Lechner Date: Thu, 8 Dec 2022 14:34:30 -0600 Subject: [PATCH] service-worker: don't serve index.html for static This fixes issues where the index.html page can be served if we have something that requests a static resource that does not exist. In the worst case, this would the app appear recursively in the docs pane. Also remove some unnecessary typing while we are touching this. --- src/service-worker.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/service-worker.ts b/src/service-worker.ts index 6af3be77..b9bbe9c6 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -34,12 +34,17 @@ precacheAndRoute(self.__WB_MANIFEST, { ignoreURLParametersMatching: [/.*/] }); const fileExtensionRegexp = new RegExp('/[^/?]+\\.[^/]+$'); registerRoute( // Return false to exempt requests from being fulfilled by index.html. - ({ request, url }: { request: Request; url: URL }) => { + ({ request, url }) => { // If this isn't a navigation, skip. if (request.mode !== 'navigate') { return false; } + // If this is a URL for a static resource, skip. + if (url.pathname.startsWith('/static/')) { + return false; + } + // If this is a URL that starts with /_, skip. if (url.pathname.startsWith('/_')) { return false;