Files
the-nexus/sw.js
Alexander Whitestone e3f474662e feat: add PWA manifest + service worker for offline + home screen install
- manifest.json with icons, theme colors, standalone display mode
- sw.js: cache-first service worker caching local assets and Three.js CDN
  modules, fonts; graceful offline fallback with cached index.html
- Offline banner (visible when navigator.onLine === false)
- iOS/Android home screen meta tags (apple-mobile-web-app-capable etc.)
- 192x192 and 512x512 PNG icons with nexus sigil design

Fixes #14
2026-03-23 21:23:15 -04:00

113 lines
3.0 KiB
JavaScript

// ◈ The Nexus — Service Worker
// Offline-first caching for sovereign space
const CACHE_VERSION = 'nexus-v1';
const STATIC_CACHE = `${CACHE_VERSION}-static`;
const CDN_CACHE = `${CACHE_VERSION}-cdn`;
// Core local assets — always cache
const STATIC_ASSETS = [
'/',
'/index.html',
'/style.css',
'/app.js',
'/manifest.json',
'/icons/icon-192.png',
'/icons/icon-512.png',
];
// CDN assets for Three.js — cache on first fetch
const CDN_ORIGINS = [
'https://cdn.jsdelivr.net',
'https://fonts.googleapis.com',
'https://fonts.gstatic.com',
];
// ═══ INSTALL — pre-cache static assets ═══
self.addEventListener('install', event => {
event.waitUntil(
caches.open(STATIC_CACHE)
.then(cache => cache.addAll(STATIC_ASSETS))
.then(() => self.skipWaiting())
);
});
// ═══ ACTIVATE — clean up old caches ═══
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys()
.then(keys => Promise.all(
keys
.filter(key => key.startsWith('nexus-') && key !== STATIC_CACHE && key !== CDN_CACHE)
.map(key => caches.delete(key))
))
.then(() => self.clients.claim())
);
});
// ═══ FETCH — serve from cache, fall back to network ═══
self.addEventListener('fetch', event => {
const { request } = event;
const url = new URL(request.url);
// Skip non-GET requests
if (request.method !== 'GET') return;
// CDN resources: cache-first with network fallback
if (CDN_ORIGINS.some(origin => request.url.startsWith(origin))) {
event.respondWith(cdnFirst(request));
return;
}
// Same-origin static assets: cache-first
if (url.origin === self.location.origin) {
event.respondWith(staticFirst(request));
return;
}
});
// Cache-first for CDN (Three.js modules, fonts)
async function cdnFirst(request) {
const cached = await caches.match(request);
if (cached) return cached;
try {
const response = await fetch(request);
if (response.ok) {
const cache = await caches.open(CDN_CACHE);
cache.put(request, response.clone());
}
return response;
} catch {
// Offline and not cached — return a minimal error response
return new Response('/* offline */', {
headers: { 'Content-Type': 'text/plain' },
});
}
}
// Cache-first for local static assets
async function staticFirst(request) {
const cached = await caches.match(request);
if (cached) return cached;
try {
const response = await fetch(request);
if (response.ok) {
const cache = await caches.open(STATIC_CACHE);
cache.put(request, response.clone());
}
return response;
} catch {
// Offline fallback: serve index.html for navigation requests
if (request.mode === 'navigate') {
const fallback = await caches.match('/index.html');
if (fallback) return fallback;
}
return new Response('Nexus offline — cached assets not found.', {
status: 503,
headers: { 'Content-Type': 'text/plain' },
});
}
}