Files
openclaw-backups/memory-viewer/public/sw.js
Krilly c9acf0c4da Add memory-viewer from silicondawn
- Web UI for browsing and editing OpenClaw memory files
- Cloned from https://github.com/silicondawn/memory-viewer
- Built and running on port 8901
- Features: file tree, markdown rendering, search, live reload
- Full access to MEMORY.md, daily notes, skills, automations
2026-02-21 02:28:57 +00:00

61 lines
1.6 KiB
JavaScript

const CACHE_NAME = 'memory-viewer-v1';
const STATIC_ASSETS = ['/'];
// Install: cache shell
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS))
);
self.skipWaiting();
});
// Activate: clean old caches
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k)))
)
);
self.clients.claim();
});
// Fetch: network-first for API, cache-first for assets
self.addEventListener('fetch', (e) => {
const url = new URL(e.request.url);
// API calls: network only
if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/ws')) {
return;
}
// Assets with hash in filename: cache-first (immutable)
if (url.pathname.startsWith('/assets/') && url.pathname.match(/\-[a-zA-Z0-9_-]{8}\./)) {
e.respondWith(
caches.match(e.request).then((cached) => {
if (cached) return cached;
return fetch(e.request).then((resp) => {
if (resp.ok) {
const clone = resp.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(e.request, clone));
}
return resp;
});
})
);
return;
}
// HTML: network-first, fallback to cache
e.respondWith(
fetch(e.request)
.then((resp) => {
if (resp.ok) {
const clone = resp.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(e.request, clone));
}
return resp;
})
.catch(() => caches.match(e.request))
);
});