Skip to content

Commit

Permalink
service-worker?
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Keating committed Aug 2, 2023
1 parent 905b697 commit 21269ee
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
66 changes: 66 additions & 0 deletions public/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// service-worker.js
const CACHE_NAME = 'nvAux-cache';
const urlsToCache = [
'/',
'/index.html',
'/favicon.png',
];

self.addEventListener('install', (event) => {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});

self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then((response) => {
// Cache hit - return response
if (response) {
return response;
}
return fetch(event.request).then(
(response) => {
// Check if we received a valid response
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}

// IMPORTANT: Clone the response. A response is a stream
// and because we want the browser to consume the response
// as well as the cache consuming the response, we need
// to clone it so we have two streams.
const responseToCache = response.clone();

caches.open(CACHE_NAME)
.then((cache) => {
cache.put(event.request, responseToCache);
});

return response;
}
);
})
);
});

self.addEventListener('activate', (event) => {
const cacheWhitelist = [CACHE_NAME];
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
});
28 changes: 28 additions & 0 deletions src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
<script>
import { onMount } from 'svelte';
import OmniBar from './lib/OmniBar.svelte';
import NoteList from './lib/NoteList.svelte';
import ResizeHandle from './lib/ResizeHandle.svelte';
import NoteDetail from './lib/NoteDetail.svelte';
import StatusBar from './lib/StatusBar.svelte';
import { fullScreen, maximumFullScreen } from './lib/store';
onMount(() => {
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("/service-worker.js")
.then((registration) => {
registration.addEventListener("updatefound", () => {
// If updatefound is fired, it means that there's
// a new service worker being installed.
const installingWorker = registration.installing;
console.log(
"A new service worker is being installed:",
installingWorker,
);
// You can listen for changes to the installing service worker's
// state via installingWorker.onstatechange
});
})
.catch((error) => {
console.error(`Service worker registration failed: ${error}`);
});
} else {
console.error("Service workers are not supported.");
}
});
</script>

<div class="h-screen w-screen overflow-hidden flex flex-col justify-center items-center {$fullScreen ? '' : 'p-2'}">
Expand Down

0 comments on commit 21269ee

Please sign in to comment.