Skip to content

armgit / bygga

Public Imported from GitHub
0

IKEA assembly, step by step: animated, manual-style 3D guides. Scan any paper manual with Together AI.

TypeScript

Offline from the first visit, voice included

Youssef357b2f7ec7da
diff --git a/public/sw.js b/public/sw.js
index 0f79b2d..e182556 100644
--- a/public/sw.js
+++ b/public/sw.js
@@ -22,6 +22,66 @@ self.addEventListener("activate", (e) => {
);
});
+/** A byte range of a clip, cut from the whole clip in the cache (fetched and kept first when missing). */
+async function ranged(req) {
+ let res = await caches.match(req.url);
+ if (!res) {
+ try {
+ const full = await fetch(req.url);
+ if (full.ok) {
+ await (await caches.open(FILES)).put(req.url, full.clone());
+ res = full;
+ }
+ } catch {
+ /* offline and never heard: let the request fail below */
+ }
+ }
+ if (!res) return fetch(req);
+ const buf = await res.arrayBuffer();
+ const m = /bytes=(\d*)-(\d*)/.exec(req.headers.get("range") || "");
+ const start = m && m[1] ? Number(m[1]) : 0;
+ const end = m && m[2] ? Math.min(Number(m[2]), buf.byteLength - 1) : buf.byteLength - 1;
+ return new Response(buf.slice(start, end + 1), {
+ status: 206,
+ headers: {
+ "Content-Type": res.headers.get("Content-Type") || "audio/mpeg",
+ "Content-Range": `bytes ${start}-${end}/${buf.byteLength}`,
+ "Content-Length": String(end - start + 1),
+ "Accept-Ranges": "bytes",
+ },
+ });
+}
+
+// the page that registered the worker loaded before it was in charge, and pages reached in the app never
+// load as a page at all: the app names them here, so they are kept like any other visit
+self.addEventListener("message", (e) => {
+ const d = e.data;
+ if (!d || d.type !== "keep") return;
+ const same = (u) => {
+ try {
+ return new URL(u, location.origin).origin === location.origin;
+ } catch {
+ return false;
+ }
+ };
+ e.waitUntil(
+ Promise.all([
+ d.page && same(d.page)
+ ? fetch(d.page, { credentials: "same-origin" })
+ .then((res) => res.ok && caches.open(PAGES).then((c) => c.put(d.page, res).then(() => trim(PAGES, 40))))
+ .catch(() => {})
+ : null,
+ caches.open(FILES).then((c) =>
+ Promise.all(
+ (d.files || [])
+ .filter((u) => same(u) && /\/_next\/static\/|\.(woff2?|png|svg)$/.test(new URL(u, location.origin).pathname))
+ .map((u) => c.match(u).then((hit) => hit || c.add(u).catch(() => {}))),
+ ),
+ ),
+ ]),
+ );
+});
+
async function trim(name, max) {
const c = await caches.open(name);
const keys = await c.keys();
@@ -33,8 +93,11 @@ self.addEventListener("fetch", (e) => {
if (req.method !== "GET") return;
const url = new URL(req.url);
if (url.origin !== location.origin || url.pathname.startsWith("/api/") || url.pathname.startsWith("/dev/")) return;
- // media elements ask for byte ranges: let the network answer those
- if (req.headers.has("range")) return;
+ // audio asks for byte ranges: voice clips answer them from the cache, so the voice works offline too
+ if (req.headers.has("range")) {
+ if (url.pathname.startsWith("/voice/")) e.respondWith(ranged(req));
+ return;
+ }
if (req.mode === "navigate") {
e.respondWith(
diff --git a/src/components/Offline.tsx b/src/components/Offline.tsx
index a09ed94..06c963d 100644
--- a/src/components/Offline.tsx
+++ b/src/components/Offline.tsx
@@ -1,14 +1,31 @@
"use client";
import { useEffect } from "react";
+import { usePathname } from "next/navigation";
/** Registers the service worker that keeps opened guides and their voice working offline. */
export default function Offline() {
+ const path = usePathname();
+
useEffect(() => {
if (process.env.NODE_ENV !== "production" || !("serviceWorker" in navigator)) return;
const go = () => navigator.serviceWorker.register("/sw.js").catch(() => {});
if (document.readyState === "complete") go();
else window.addEventListener("load", go, { once: true });
}, []);
+
+ // every page you see is kept for offline, with the files it loaded, even the first one and in-app moves
+ useEffect(() => {
+ if (process.env.NODE_ENV !== "production" || !("serviceWorker" in navigator)) return;
+ let gone = false;
+ navigator.serviceWorker.ready.then((reg) => {
+ if (gone) return;
+ const files = performance.getEntriesByType("resource").map((r) => r.name);
+ reg.active?.postMessage({ type: "keep", page: location.origin + path, files });
+ });
+ return () => {
+ gone = true;
+ };
+ }, [path]);
return null;
}