133 lines
4.0 KiB
JavaScript
133 lines
4.0 KiB
JavaScript
|
|
/**
|
||
|
|
* @fileoverview Service Worker for Rantii PWA
|
||
|
|
* @author retoor <retoor@molodetz.nl>
|
||
|
|
* @description Handles caching and offline functionality
|
||
|
|
* @keywords service worker, pwa, cache, offline
|
||
|
|
*/
|
||
|
|
|
||
|
|
const CACHE_NAME = 'rantii-v1';
|
||
|
|
const STATIC_ASSETS = [
|
||
|
|
'./',
|
||
|
|
'./index.html',
|
||
|
|
'./manifest.json',
|
||
|
|
'./css/variables.css',
|
||
|
|
'./css/base.css',
|
||
|
|
'./css/themes/dark.css',
|
||
|
|
'./css/themes/light.css',
|
||
|
|
'./css/themes/black.css',
|
||
|
|
'./css/themes/white.css',
|
||
|
|
'./css/themes/ocean.css',
|
||
|
|
'./css/themes/forest.css',
|
||
|
|
'./css/themes/sunset.css',
|
||
|
|
'./css/components/app-header.css',
|
||
|
|
'./css/components/app-nav.css',
|
||
|
|
'./css/components/components.css',
|
||
|
|
'./css/components/rant.css',
|
||
|
|
'./css/components/comment.css',
|
||
|
|
'./css/components/user.css',
|
||
|
|
'./css/components/notification.css',
|
||
|
|
'./css/components/form.css',
|
||
|
|
'./css/components/pages.css',
|
||
|
|
'./js/app.js',
|
||
|
|
'./js/api/client.js',
|
||
|
|
'./js/services/storage.js',
|
||
|
|
'./js/services/auth.js',
|
||
|
|
'./js/services/router.js',
|
||
|
|
'./js/services/theme.js',
|
||
|
|
'./js/utils/date.js',
|
||
|
|
'./js/utils/url.js',
|
||
|
|
'./js/utils/markdown.js',
|
||
|
|
'./js/utils/template.js',
|
||
|
|
'./lib/marked.min.js',
|
||
|
|
'./lib/highlight.min.js',
|
||
|
|
'./lib/highlight.css'
|
||
|
|
];
|
||
|
|
|
||
|
|
self.addEventListener('install', (event) => {
|
||
|
|
event.waitUntil(
|
||
|
|
caches.open(CACHE_NAME).then((cache) => {
|
||
|
|
return cache.addAll(STATIC_ASSETS);
|
||
|
|
})
|
||
|
|
);
|
||
|
|
self.skipWaiting();
|
||
|
|
});
|
||
|
|
|
||
|
|
self.addEventListener('activate', (event) => {
|
||
|
|
event.waitUntil(
|
||
|
|
caches.keys().then((cacheNames) => {
|
||
|
|
return Promise.all(
|
||
|
|
cacheNames.map((cacheName) => {
|
||
|
|
if (cacheName !== CACHE_NAME) {
|
||
|
|
return caches.delete(cacheName);
|
||
|
|
}
|
||
|
|
})
|
||
|
|
);
|
||
|
|
})
|
||
|
|
);
|
||
|
|
self.clients.claim();
|
||
|
|
});
|
||
|
|
|
||
|
|
self.addEventListener('fetch', (event) => {
|
||
|
|
const url = new URL(event.request.url);
|
||
|
|
|
||
|
|
if (url.origin === 'https://dr.molodetz.nl' ||
|
||
|
|
url.origin === 'https://devrant.com' ||
|
||
|
|
url.origin === 'https://www.devrant.com' ||
|
||
|
|
url.pathname.startsWith('/api/')) {
|
||
|
|
event.respondWith(
|
||
|
|
fetch(event.request).catch(() => {
|
||
|
|
return new Response(JSON.stringify({ success: false, error: 'Offline' }), {
|
||
|
|
headers: { 'Content-Type': 'application/json' }
|
||
|
|
});
|
||
|
|
})
|
||
|
|
);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (url.hostname.includes('avatars.devrant.com') || url.hostname.includes('img.devrant.com')) {
|
||
|
|
event.respondWith(
|
||
|
|
caches.open('rantii-images').then((cache) => {
|
||
|
|
return cache.match(event.request).then((response) => {
|
||
|
|
if (response) {
|
||
|
|
return response;
|
||
|
|
}
|
||
|
|
return fetch(event.request).then((networkResponse) => {
|
||
|
|
cache.put(event.request, networkResponse.clone());
|
||
|
|
return networkResponse;
|
||
|
|
}).catch(() => {
|
||
|
|
return new Response('', { status: 404 });
|
||
|
|
});
|
||
|
|
});
|
||
|
|
})
|
||
|
|
);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
event.respondWith(
|
||
|
|
caches.match(event.request).then((response) => {
|
||
|
|
if (response) {
|
||
|
|
return response;
|
||
|
|
}
|
||
|
|
return fetch(event.request).then((networkResponse) => {
|
||
|
|
if (networkResponse && networkResponse.status === 200) {
|
||
|
|
const responseClone = networkResponse.clone();
|
||
|
|
caches.open(CACHE_NAME).then((cache) => {
|
||
|
|
cache.put(event.request, responseClone);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
return networkResponse;
|
||
|
|
}).catch(() => {
|
||
|
|
if (event.request.destination === 'document') {
|
||
|
|
return caches.match('./index.html');
|
||
|
|
}
|
||
|
|
});
|
||
|
|
})
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
self.addEventListener('message', (event) => {
|
||
|
|
if (event.data && event.data.type === 'SKIP_WAITING') {
|
||
|
|
self.skipWaiting();
|
||
|
|
}
|
||
|
|
});
|