Skip to content

Commit 29e059a

Browse files
committed
fix: let browser handle navigation requests to support auth proxies
When the dashboard sits behind an authentication proxy (Cloudflare Access, OAuth2 Proxy, Authelia, etc.), the service worker must not intercept navigation requests. Otherwise it serves a cached index.html, preventing the browser from following HTTP redirects to the login page when the auth session expires. Remove the NavigationRoute + createHandlerBoundToURL registration and disable precacheAndRoute's directoryIndex and cleanURLs options so it cannot implicitly serve index.html for navigation requests. Static assets (JS, CSS, fonts, images) are still served from the precache via exact URL match, so page loads remain fast.
1 parent 431bfdd commit 29e059a

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

ui/src/sw.js

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,26 @@
11
/// <reference lib="webworker" />
22
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'
54

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+
})
820

921
// clean old assets
1022
cleanupOutdatedCaches()
1123

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-
3324
self.skipWaiting()
3425
// https://developer.mozilla.org/en-US/docs/Web/API/Clients/claim
3526
clientsClaim()
36-
37-
// Add custom service worker code here

0 commit comments

Comments
 (0)