summaryrefslogtreecommitdiff
path: root/src/content/sw.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/content/sw.js')
-rw-r--r--src/content/sw.js123
1 files changed, 107 insertions, 16 deletions
diff --git a/src/content/sw.js b/src/content/sw.js
index 9e17707..03a09e4 100644
--- a/src/content/sw.js
+++ b/src/content/sw.js
@@ -22,8 +22,9 @@ const DEFAULT_INSTALL_PATHS = [
"index.html",
"style.css",
"img/favicon.svg",
- "papo.js",
-].concat(collectLeaves(FALLBACK_PATHS));
+ // "papo.js",
+// ].concat(collectLeaves(FALLBACK_PATHS));
+];
const mkInstallHandler = ({ self, caches }) => event => {
self.skipWaiting();
@@ -55,17 +56,6 @@ const getPrefixIn = (paths, segments) => {
return getPrefixIn(paths[segments[0]], segments.slice(1));
};
-const maybeFallback = async (caches, request, paths = FALLBACK_PATHS) => {
- const url = new URL(request.url)
- const segments = url.pathname.split("/").filter(s => !!s)
- const fallbackPath = getPrefixIn(paths, segments);
- if (fallbackPath) {
- return await caches.match(fallbackPath);
- }
-
- return null;
-}
-
const fromCache = async (caches, fetch, { request, preloadResponse }) => {
const cachedResponse = await caches.match(request);
if (cachedResponse) {
@@ -81,7 +71,6 @@ const fromCache = async (caches, fetch, { request, preloadResponse }) => {
}
try {
- // FIXME: do integration test with real lack of internet.
const fetchedResponse = await fetch(request);
store(caches, request, fetchedResponse.clone());
return fetchedResponse;
@@ -95,12 +84,113 @@ const fromCache = async (caches, fetch, { request, preloadResponse }) => {
}
};
-const mkFetchHandler = ({ caches }) => (event, mkfetch = () => fetch) => {
+const maybeFallback = async (caches, request, paths = FALLBACK_PATHS) => {
+ const url = new URL(request.url)
+ const segments = url.pathname.split("/").filter(s => !!s)
+ const fallbackPath = getPrefixIn(paths, segments);
+
+ if (fallbackPath) {
+ return await caches.match(fallbackPath);
+ }
+ return null;
+}
+
+const CONFIG_PATHS = {
+ immutable: {
+ "some": {
+ "path": true,
+ },
+ },
+ staleOK: {
+ },
+};
+const PRIORITY_ORDERING = [
+ "immutable",
+ "staleFallback",
+ "stale",
+];
+
+const pullThroughCache = (env, ctx) => {
+};
+
+/*
+// FIXME: handle base paths
+ const STRATEGIES = [
+ {
+ name: "immutable",
+ routes: new Set([
+ "/cas",
+ ]),
+ fn: immutable,
+ },
+ {
+ name: "staleFallback",
+ routes: {
+ "/img/icon/": "",
+ },
+ fn: staleFallback,
+ },
+ {
+ name: "staleOnly",
+ routes: new Set([
+ // FIXME: add "/"
+ "/index.html": true,
+ "/style.css": true,
+ "/papo.js": true,
+ ]),
+ img: "/fallback-image-FIXME.svg",
+ data: {
+ static: "/fallback-data-FIXME.json",
+ },
+ fn: staleOnly,
+ },
+};
+*/
+
+const asSegments = pathname =>
+ pathname.split("/").filter(s => !!s);
+
+const requestSegments = ({ url }) =>
+ asSegments(new URL(url).pathname);
+
+/*
+ const strategyFor = (segments, paths = CONFIG_PATHS) => {
+ for (const strategy of PRIORITY_ORDERING) {
+ if (getPrefixIn(paths[strategy.name], segments)) {
+ return strategy;
+ }
+ }
+};
+*/
+
+const networkOnly = async (_env, { fetch, event }) => {
+ const preloaded = await event.preloadResponse;
+ if (preloaded) {
+ return preloaded;
+ }
+
+ return await fetch(event.request);
+};
+
+const strategyFor = (segments, paths = CONFIG_PATHS) => {
+ return networkOnly;
+};
+
+// FIXME: noop if "Connection": "Upgrade" is in header
+// if (event.request.headers.get("Connection").toLowerCase() == "upgrade");
+// out([...event.request.headers.entries()]);
+const mkFetchHandler = env => (event, mkfetch = () => fetch) => {
if (event.request.method !== "GET") {
return;
}
- event.respondWith(fromCache(caches, mkfetch(), event));
+ const segments = requestSegments(event.request);
+ const strategyFn = strategyFor(segments);
+ event.respondWith(strategyFn(env, {
+ event,
+ segments,
+ fetch: mkfetch(),
+ }));
};
const mkActivateHandler = ({ self, clients }) => event =>
@@ -126,6 +216,7 @@ const main = ({
self,
caches,
clients,
+ baseSegments: asSegments(self.location.pathname),
out,
err,
});