Skip to content

Commit 330e349

Browse files
committed
Restrict SW SPA fallback to allowlisted app routes only
1 parent b0e2b2c commit 330e349

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/service-worker.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,30 @@ clientsClaim();
1010

1111
precacheAndRoute(self.__WB_MANIFEST);
1212

13+
const SPA_ROUTE_ALLOWLIST = [
14+
/^\/$/, // Home
15+
/^\/settings$/, // Settings
16+
/^\/history$/, // History list
17+
/^\/pending$/, // Pending
18+
/^\/add$/, // Add credentials
19+
/^\/send$/, // Send credentials
20+
/^\/verification\/result$/, // Verification result
21+
/^\/login$/, // Login
22+
/^\/login-state$/, // Login state
23+
/^\/cb(\/.*)?$/, // Callback routes
24+
/^\/credential\/[^/]+$/, // Credential
25+
/^\/credential\/[^/]+\/history$/, // Credential history
26+
/^\/credential\/[^/]+\/details$/, // Credential details
27+
/^\/history\/[^/]+$/, // History detail
28+
];
29+
1330
registerRoute(
1431
({ request, url }) => {
1532
if (request.mode !== "navigate") return false;
1633
if (url.pathname.startsWith("/_")) return false;
17-
if (/\.[a-zA-Z]+$/.test(url.pathname)) return false;
18-
return true;
34+
if (/\.[a-zA-Z0-9]+$/.test(url.pathname)) return false;
35+
36+
return SPA_ROUTE_ALLOWLIST.some((re) => re.test(url.pathname));
1937
},
2038
createHandlerBoundToURL('/index.html')
2139
);

0 commit comments

Comments
 (0)