Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Service worker - in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
nienow committed Dec 29, 2023
1 parent 48cb898 commit 043d1b9
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {defineConfig} from 'astro/config';
import yaml from '@rollup/plugin-yaml';

// https://astro.build/config
export default defineConfig({
output: 'static',
Expand Down
81 changes: 81 additions & 0 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const CACHE_NAME = 'cache-v1';

const precacheList = [
{url: '/Merged/Crafts.json'},
{url: '/Merged/Product%20Design.json'},
{url: '/Merged/Technology.json'},
{url: '/Merged/Gaming.json'},
{url: '/Merged/Fashion.json'},
{url: '/Merged/Food.json'},
{url: '/Merged/Books.json'},
{url: '/Merged/Photography.json'},
{url: '/Merged/Comics%20&%20Graphic Novels.json'},
];

//Adding `install` event listener
self.addEventListener('install', (event) => {
console.info('Event: Install');

event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
//[] of files to cache & if any of the file not present `addAll` will fail
return cache.addAll(precacheList.map(item => item.url))
.then(() => {
console.info('All files are cached');
return self.skipWaiting(); //To forces the waiting service worker to become the active service worker
})
.catch((error) => {
console.error('Failed to cache', error);
});
})
);
});

//Adding `fetch` event listener
self.addEventListener('fetch', (event) => {
const request = event.request;
event.respondWith(cacheFirst(request));
});

async function putInCache(request, response) {
const cache = await caches.open(CACHE_NAME);
await cache.put(request, response);
}

async function cacheFirst(request) {
const cachedResponse = await caches.match(request);
if (cachedResponse) {
console.log('Found in cache', request.url);
}
return cachedResponse || fetch(request)
.then(res => {
if (res.url.match(/\/Merged\/.*\.json$/)) {
console.log('Caching the response to', request.url);
putInCache(request, res.clone());
}
return res;
});
}

//Adding `activate` event listener
self.addEventListener('activate', (event) => {
console.info('Event: Activate');
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cache) => {
if (cache !== CACHE_NAME) {
return caches.delete(cache); //Deleting the old cache (cache v1)
}
})
);
})
.then(function () {
console.info("Old caches are cleared!");
// To tell the service worker to activate current one
// instead of waiting for the old one to finish.
return self.clients.claim();
})
);
});
3 changes: 3 additions & 0 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const {title, description} = Astro.props;
<link rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Poiret+One&family=League+Spartan&family=Inconsolata"/>
<!--<script src="https://cdn.jsdelivr.net/npm/fontable@latest/dist/auto.js"></script>-->
<script>
navigator.serviceWorker.register("/sw.js")
</script>
</head>
<body>
<slot/>
Expand Down

0 comments on commit 043d1b9

Please sign in to comment.