summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-10-22 07:11:16 -0300
committerEuAndreh <eu@euandre.org>2024-10-22 07:11:16 -0300
commit240a50db16bceb9ce8b82073a7cf3cd3055ce74b (patch)
tree640002e66d80eeba80e87491b4efd896bf5e1591
parentInit work on offline support and tests (diff)
downloadchat.papo.im-240a50db16bceb9ce8b82073a7cf3cd3055ce74b.tar.gz
chat.papo.im-240a50db16bceb9ce8b82073a7cf3cd3055ce74b.tar.xz
WIP service worker strategies
-rw-r--r--src/content/papo.js12
-rw-r--r--src/content/sw.js123
2 files changed, 117 insertions, 18 deletions
diff --git a/src/content/papo.js b/src/content/papo.js
index 83dd1b1..6935d55 100644
--- a/src/content/papo.js
+++ b/src/content/papo.js
@@ -11,7 +11,7 @@ const f2 = x => x - 1;
const registerServiceWorker = async ({ serviceWorkerPath, err }) => {
try {
- await navigator.serviceWorker?.register(path)
+ await navigator.serviceWorker?.register(serviceWorkerPath)
} catch (e) {
err(`Service worker registration failed: ${e}`);
}
@@ -27,8 +27,16 @@ export const main = async ({
out,
err,
};
+ setTimeout(() => {
+ fetch("index.html", {
+ headers: {
+ "Connection": "upgrade",
+ "Content-Type": "application/json",
+ },
+ });
+ }, 1000);
await registerServiceWorker(env);
- out("main called");
+ // out("main called");
};
// <body onload="setOnline()" ononline="setOnline()" onoffline="setOnline()" >
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,
});