static: intercept only the shell paths in the service worker (section 31 point 6)
This commit is contained in:
1 parent
703b0c28e9
commit
a025b04e8e
1 file changed
+10
-15
+10
-15
@@ -1,15 +1,16 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
// The VardKey service worker of section 31 point 6. It caches only the static
|
// The VardKey service worker of section 31 point 6. It caches only the static
|
||||||
// shell, never an API response, never /health, and never the login page, so the
|
// shell, never an API response, never the health endpoint, and never the login
|
||||||
// app can never show stale or unauthenticated data. Every fetch still goes to
|
// page, so the app can never show stale or unauthenticated data. It does that
|
||||||
// the server first; the cache is only there for the shell while the server is
|
// with an allowlist: only a request for one of the shell paths below is handled
|
||||||
// unreachable.
|
// at all. Everything else is left to the browser and goes straight to the
|
||||||
|
// server. Every shell fetch still tries the server first; the cache is only
|
||||||
|
// there while the server is unreachable.
|
||||||
|
|
||||||
const CACHE_NAME = "vardkey-shell-v1";
|
const CACHE_NAME = "vardkey-shell-v1";
|
||||||
|
|
||||||
// The shell: the pages and the assets they need. /api/*, /health and /login are
|
// The shell: the pages and the assets they need.
|
||||||
// deliberately absent.
|
|
||||||
const SHELL = [
|
const SHELL = [
|
||||||
"/",
|
"/",
|
||||||
"/setup",
|
"/setup",
|
||||||
@@ -24,6 +25,7 @@ const SHELL = [
|
|||||||
"/health-check.js",
|
"/health-check.js",
|
||||||
"/history.js",
|
"/history.js",
|
||||||
];
|
];
|
||||||
|
const SHELL_PATHS = new Set(SHELL);
|
||||||
|
|
||||||
self.addEventListener("install", (event) => {
|
self.addEventListener("install", (event) => {
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
@@ -52,15 +54,8 @@ function isShellRequest(request) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
if (url.origin !== self.location.origin) {
|
// A query string, such as the history detail link, still matches its path.
|
||||||
return false;
|
return url.origin === self.location.origin && SHELL_PATHS.has(url.pathname);
|
||||||
}
|
|
||||||
// The API, the readiness endpoint and the login page always go to the server.
|
|
||||||
return !(
|
|
||||||
url.pathname.startsWith("/api/")
|
|
||||||
|| url.pathname === "/health"
|
|
||||||
|| url.pathname === "/login"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.addEventListener("fetch", (event) => {
|
self.addEventListener("fetch", (event) => {
|
||||||
|
|||||||
Reference in new issue
Block a user