import {manifest, version} from '@parcel/service-worker';

console.log("[Service Worker] Version:", version);
console.log("[Service Worker] Manifest:", manifest);

// install Service Worker
async function install() {
	const cache = await caches.open(version); //cacheNAme
	console.log("[Service Worker] Caching all: app shell and content");
	await cache.addAll(manifest); // fileList
}
console.log("[Service Worker] Install");
self.addEventListener("install", e => e.waitUntil(install()));

// fetch content
/*self.addEventListener('fetch', (e) => {
	e.respondWith((async () => {
		const r = await caches.match(e.request);
		console.log(`[Service Worker] Fetching resource: ${e.request.url}`);
		if (r) {
			return r;
		}
		const response = await fetch(e.request);
		const cache = await caches.open(cacheName);
		console.log(`[Service Worker] Caching new resource: ${e.request.url}`);
		cache.put(e.request, response.clone());
		return response;
	})());
});*/


// activate: clear outdated files from cache
async function activate() {
	const keys = await caches.keys();
	await Promise.all(keys.map(key => key !== version && caches.delete(key)));
};
console.log("[Service Worker] Activate");
self.addEventListener("activate", e => e.waitUntil(activate()));
