131 lines
3.8 KiB
JavaScript
131 lines
3.8 KiB
JavaScript
const CACHE_NAME = 'linkding-v2';
|
|
const urlsToCache = [
|
|
'/',
|
|
'/index.html',
|
|
'/styles.css',
|
|
'/app.js',
|
|
'/manifest.json',
|
|
'/icon-192.png',
|
|
'/icon-512.png'
|
|
];
|
|
|
|
// Install event - cache resources
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then((cache) => {
|
|
console.log('Opened cache');
|
|
return cache.addAll(urlsToCache);
|
|
})
|
|
.catch((error) => {
|
|
console.error('Cache install failed:', error);
|
|
})
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
// Activate event - clean up old caches
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((cacheNames) => {
|
|
return Promise.all(
|
|
cacheNames.map((cacheName) => {
|
|
if (cacheName !== CACHE_NAME) {
|
|
console.log('Deleting old cache:', cacheName);
|
|
return caches.delete(cacheName);
|
|
}
|
|
})
|
|
);
|
|
}).then(() => {
|
|
// Claim all clients immediately to ensure new service worker takes control
|
|
return self.clients.claim();
|
|
})
|
|
);
|
|
});
|
|
|
|
// Fetch event - network first for HTML/CSS/JS, cache first for images
|
|
self.addEventListener('fetch', (event) => {
|
|
// API requests - always fetch from network, don't cache
|
|
if (event.request.url.includes('/api/')) {
|
|
event.respondWith(fetch(event.request));
|
|
return;
|
|
}
|
|
|
|
const url = new URL(event.request.url);
|
|
const isHTML = event.request.destination === 'document' || url.pathname === '/' || url.pathname.endsWith('.html');
|
|
const isCSS = url.pathname.endsWith('.css');
|
|
const isJS = url.pathname.endsWith('.js');
|
|
const isImage = event.request.destination === 'image' || url.pathname.match(/\.(png|jpg|jpeg|gif|svg|webp|ico)$/i);
|
|
|
|
// For HTML, CSS, and JS: Network first, then cache (for offline support)
|
|
if (isHTML || isCSS || isJS) {
|
|
event.respondWith(
|
|
fetch(event.request)
|
|
.then((response) => {
|
|
// Don't cache non-successful responses
|
|
if (!response || response.status !== 200 || response.type !== 'basic') {
|
|
return response;
|
|
}
|
|
|
|
// Clone the response
|
|
const responseToCache = response.clone();
|
|
|
|
caches.open(CACHE_NAME)
|
|
.then((cache) => {
|
|
cache.put(event.request, responseToCache);
|
|
});
|
|
|
|
return response;
|
|
})
|
|
.catch(() => {
|
|
// Network failed, try cache
|
|
return caches.match(event.request).then((response) => {
|
|
// If it's a document request and cache fails, return index.html
|
|
if (!response && isHTML) {
|
|
return caches.match('/index.html');
|
|
}
|
|
return response;
|
|
});
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
// For images and other static assets: Cache first, then network
|
|
if (isImage) {
|
|
event.respondWith(
|
|
caches.match(event.request)
|
|
.then((response) => {
|
|
// Return cached version or fetch from network
|
|
return response || fetch(event.request).then((response) => {
|
|
// Don't cache non-successful responses
|
|
if (!response || response.status !== 200 || response.type !== 'basic') {
|
|
return response;
|
|
}
|
|
|
|
// Clone the response
|
|
const responseToCache = response.clone();
|
|
|
|
caches.open(CACHE_NAME)
|
|
.then((cache) => {
|
|
cache.put(event.request, responseToCache);
|
|
});
|
|
|
|
return response;
|
|
});
|
|
})
|
|
.catch(() => {
|
|
// If both cache and network fail, return offline page if available
|
|
if (event.request.destination === 'document') {
|
|
return caches.match('/index.html');
|
|
}
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
// For other requests: Network first
|
|
event.respondWith(fetch(event.request));
|
|
});
|
|
|