|
1 | 1 | /// <reference lib="webworker" /> |
2 | 2 | import { clientsClaim } from 'workbox-core' |
3 | | -import { cleanupOutdatedCaches, createHandlerBoundToURL, precacheAndRoute } from 'workbox-precaching' |
4 | | -import { NavigationRoute, registerRoute } from 'workbox-routing' |
| 3 | +import { cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching' |
5 | 4 |
|
6 | | -// self.__WB_MANIFEST is the default injection point |
7 | | -precacheAndRoute(self.__WB_MANIFEST) |
| 5 | +// Navigation requests (request.mode === 'navigate') are intentionally NOT |
| 6 | +// handled by the Service Worker. This lets the browser perform normal network |
| 7 | +// navigation, which is critical when the dashboard sits behind an |
| 8 | +// authentication proxy (Cloudflare Access, OAuth2 Proxy, Authelia, etc.). |
| 9 | +// Without this, the SW would serve a cached index.html, preventing the browser |
| 10 | +// from following HTTP redirects to the login page when the auth session expires. |
| 11 | +// |
| 12 | +// directoryIndex and cleanURLs are disabled so that precacheAndRoute does not |
| 13 | +// implicitly serve index.html for navigation requests to '/dashboard/'. |
| 14 | +// Static assets (JS, CSS, fonts, images) are still served from the precache |
| 15 | +// via exact URL match, so page loads remain fast. |
| 16 | +precacheAndRoute(self.__WB_MANIFEST, { |
| 17 | + directoryIndex: null, |
| 18 | + cleanURLs: false |
| 19 | +}) |
8 | 20 |
|
9 | 21 | // clean old assets |
10 | 22 | cleanupOutdatedCaches() |
11 | 23 |
|
12 | | -/** @type {RegExp[] | undefined} */ |
13 | | -const denylist = [] |
14 | | - |
15 | | -// in dev mode, do not precache anything |
16 | | -if (import.meta.env.DEV) { |
17 | | - // don't precache anything |
18 | | - console.log('Development mode, not pre-caching anything') |
19 | | - denylist.push(/.*/) |
20 | | -} else { |
21 | | - // don't precache anything where the urls pathname ends with a slash (including times when the url has a query string) |
22 | | - // this permits the request to be handled by the server which will do a redirect as required |
23 | | - const configPath = self.location.pathname.split('/')[1] |
24 | | - denylist.push(new RegExp(`/${configPath}/[^?]*/(\\?.*)*$`)) |
25 | | -} |
26 | | - |
27 | | -// to allow work offline for allowed routes only |
28 | | -registerRoute(new NavigationRoute( |
29 | | - createHandlerBoundToURL('index.html'), |
30 | | - { denylist } |
31 | | -)) |
32 | | - |
33 | 24 | self.skipWaiting() |
34 | 25 | // https://developer.mozilla.org/en-US/docs/Web/API/Clients/claim |
35 | 26 | clientsClaim() |
36 | | - |
37 | | -// Add custom service worker code here |
0 commit comments