Skip to content

Commit

Permalink
Test adding a service worked for PWA installing
Browse files Browse the repository at this point in the history
  • Loading branch information
FormularSumo committed Jan 4, 2024
1 parent 3bd79e2 commit 64e6f22
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,11 @@
document.getElementById("fullscreen toggle").style.display = 'none';
}
</script>

<script>
if('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js', { scope: '/' });
}
</script>
</body>
</html>
34 changes: 34 additions & 0 deletions sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const CACHE_NAME = `v1`;

// Use the install event to pre-cache all initial resources.
self.addEventListener('install', event => {
event.waitUntil((async () => {
const cache = await caches.open(CACHE_NAME);
cache.addAll([
'/',
]);
})());
});

self.addEventListener('fetch', event => {
event.respondWith((async () => {
const cache = await caches.open(CACHE_NAME);

// Get the resource from the cache.
const cachedResponse = await cache.match(event.request);
if (cachedResponse) {
return cachedResponse;
} else {
try {
// If the resource was not in the cache, try the network.
const fetchResponse = await fetch(event.request);

// Save the resource in the cache and return it.
cache.put(event.request, fetchResponse.clone());
return fetchResponse;
} catch (e) {
// The network failed.
}
}
})());
});

0 comments on commit 64e6f22

Please sign in to comment.