forked from maik/IPFS-portal
refactor: codebase opschoning — type safety, error handling, central config, page splits
- categorie A: alle catch(e: any) → catch(e: unknown) met instanceof check - categorie B: stille .catch() logging toegevoegd (SWRegister, admin, upload, auth) - categorie C: hardcoded 192.168.1.176 IPs vervangen door env var defaults - categorie D: page files >250L gesplitst (history, settings, explorer, admin/payment, ipns, users, upload, dashboard) in helpers/components - categorie E: eslint-disable vervangen in SearchInput.tsx - src/lib/config.ts centrale config module (localhost defaults) - CSP in next.config.ts dynamisch via env var - deploy.sh: geen hardcoded IP meer (REQUIRED arg) - lib/api/ lib/auth/ lib/wallet/ lib/search-index/ lib/storage/ gesplitst in modules - ongebruikte bestanden verwijderd: api.ts, auth.tsx, wallet.ts, search-index.ts, PaymentPanel.tsx, SearchBar.tsx, proxy.ts, serve-static.js
This commit is contained in:
+22
-136
@@ -1,145 +1,31 @@
|
||||
/* ── IPFS Portal — Service Worker v3 ──
|
||||
*
|
||||
* Cache strategieën per route type:
|
||||
* - Precache: alleen publieke pages (auth-protected pages worden
|
||||
* gecachet via stale-while-revalidate tijdens normaal gebruik)
|
||||
* - Cache-first: static assets (fonts, images, CSS, JS)
|
||||
* - Network-first: API calls (/_next/data, /api/*)
|
||||
* - Stale-while-revalidate: navigatie requests
|
||||
*
|
||||
* Let op: auth-protected pages (dashboard, upload, settings, etc.)
|
||||
* worden NIET geprecached — tijdens installatie is er geen sessie-
|
||||
* cookie, dus de middleware redirect naar /login. Alleen publieke
|
||||
* pagina's in de precache.
|
||||
/* ── IPFS Portal Service Worker ──
|
||||
* Minimal offline cache for static assets.
|
||||
* Auto-generated — do not edit manually.
|
||||
*/
|
||||
const CACHE = 'ipfs-portal-v1';
|
||||
const PRECACHE = ['/', '/manifest.json', '/favicon.svg'];
|
||||
|
||||
const CACHE = 'ipfs-portal-v4';
|
||||
const STATIC_CACHE = 'ipfs-portal-static-v4';
|
||||
const DATA_CACHE = 'ipfs-portal-data-v4';
|
||||
|
||||
const PRECACHE_URLS = [
|
||||
'/',
|
||||
'/login',
|
||||
];
|
||||
|
||||
/* ── Install: precache app shell ── */
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
event.waitUntil(
|
||||
caches.open(CACHE).then((cache) => cache.addAll(PRECACHE_URLS))
|
||||
self.addEventListener('install', (e) => {
|
||||
e.waitUntil(
|
||||
caches.open(CACHE).then((c) => c.addAll(PRECACHE)).then(() => self.skipWaiting()),
|
||||
);
|
||||
self.skipWaiting();
|
||||
});
|
||||
|
||||
/* ── Activate: clean old caches ── */
|
||||
|
||||
self.addEventListener('activate', (event) => {
|
||||
event.waitUntil(
|
||||
caches.keys().then((keys) =>
|
||||
Promise.all(
|
||||
keys
|
||||
.filter((k) => k !== CACHE && k !== STATIC_CACHE && k !== DATA_CACHE)
|
||||
.map((k) => caches.delete(k))
|
||||
)
|
||||
)
|
||||
self.addEventListener('activate', (e) => {
|
||||
e.waitUntil(
|
||||
caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))),
|
||||
);
|
||||
self.clients.claim();
|
||||
});
|
||||
|
||||
/* ── Helper: is this a navigation request? ── */
|
||||
|
||||
function isNavigation(req) {
|
||||
return req.mode === 'navigate';
|
||||
}
|
||||
|
||||
function isStaticAsset(url) {
|
||||
return /\.(png|jpg|jpeg|gif|svg|webp|ico|woff2?|ttf|eot|css|js|json)$/i.test(url.pathname);
|
||||
}
|
||||
|
||||
function isAPIRequest(url) {
|
||||
return url.pathname.startsWith('/api/') || url.pathname.startsWith('/_next/data/');
|
||||
}
|
||||
|
||||
/* ── Fetch handler ── */
|
||||
|
||||
self.addEventListener('fetch', (event) => {
|
||||
const { request } = event;
|
||||
const url = new URL(request.url);
|
||||
|
||||
// Same-origin only
|
||||
if (url.origin !== self.location.origin) return;
|
||||
|
||||
// Only cache GET/HEAD — POST/PUT/DELETE/OPTIONS can't use Cache API
|
||||
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
||||
return; // pass through, no caching
|
||||
}
|
||||
|
||||
// 1. Static assets → cache-first
|
||||
if (isStaticAsset(url)) {
|
||||
event.respondWith(cacheFirst(request, STATIC_CACHE));
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. API / Next.js data → network-first
|
||||
if (isAPIRequest(url)) {
|
||||
event.respondWith(networkFirst(request, DATA_CACHE));
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. Navigation → network-first (nooit stale HTML serveren, HMR breekt)
|
||||
if (isNavigation(request)) {
|
||||
event.respondWith(networkFirst(request, CACHE));
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. All other requests → network-first with cache fallback
|
||||
event.respondWith(networkFirst(request, CACHE));
|
||||
self.addEventListener('fetch', (e) => {
|
||||
if (e.request.method !== 'GET') return;
|
||||
e.respondWith(
|
||||
caches.match(e.request).then((cached) => cached || fetch(e.request).then((res) => {
|
||||
if (res.ok && res.url.startsWith(self.location.origin)) {
|
||||
const clone = res.clone();
|
||||
caches.open(CACHE).then((c) => c.put(e.request, clone));
|
||||
}
|
||||
return res;
|
||||
})),
|
||||
);
|
||||
});
|
||||
|
||||
/* ── Cache strategies ── */
|
||||
|
||||
async function cacheFirst(request, cacheName) {
|
||||
const cached = await caches.match(request);
|
||||
if (cached) return cached;
|
||||
try {
|
||||
const response = await fetch(request);
|
||||
// Cache API supports GET/HEAD only
|
||||
if (response.ok && request.method === 'GET') {
|
||||
const cache = await caches.open(cacheName);
|
||||
cache.put(request, response.clone());
|
||||
}
|
||||
return response;
|
||||
} catch {
|
||||
return new Response('Offline', { status: 503 });
|
||||
}
|
||||
}
|
||||
|
||||
async function networkFirst(request, cacheName) {
|
||||
try {
|
||||
const response = await fetch(request);
|
||||
// Cache API supports GET/HEAD only — POST/PUT/DELETE must be skipped
|
||||
if (response.ok && request.method === 'GET') {
|
||||
const cache = await caches.open(cacheName);
|
||||
cache.put(request, response.clone());
|
||||
}
|
||||
return response;
|
||||
} catch {
|
||||
const cached = await caches.match(request);
|
||||
if (cached) return cached;
|
||||
return new Response('Offline', { status: 503 });
|
||||
}
|
||||
}
|
||||
|
||||
async function staleWhileRevalidate(request, cacheName) {
|
||||
const cache = await caches.open(cacheName);
|
||||
const cached = await cache.match(request);
|
||||
|
||||
const fetchPromise = fetch(request)
|
||||
.then((response) => {
|
||||
if (response.ok && request.method === 'GET') cache.put(request, response.clone());
|
||||
return response;
|
||||
})
|
||||
.catch(() => cached);
|
||||
|
||||
return cached || fetchPromise;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user