Skip to content

Commit

Permalink
add service worker #5
Browse files Browse the repository at this point in the history
  • Loading branch information
michivonah committed Sep 3, 2022
1 parent 285d86e commit b654d6b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,18 @@ <h2>Impressum <i class="ai-paper"></i></h2>
<script>
// Loading
window.addEventListener('load', () => {
// detect darkmode
if(window.matchMedia('(prefers-color-scheme: dark)').matches || localStorage.getItem('darkmode') == "true"){
document.body.style.setProperty('--background', '#181818');
document.body.style.setProperty('--color', '#fff');
console.log('Darkmode activated.');
}
// disable animation
AOS.init({disable: true});
// show current tool
changeTool();

// install service worker
if('serviceWorker' in navigator) navigator.serviceWorker.register('/service-worker.js');
// hide loadingscreen
setTimeout(function(){
document.getElementById('loadingscreen').style.display = "none";
Expand Down
36 changes: 36 additions & 0 deletions service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const workerCache = "math-tools-app-v1"
const assets = [
"/",
"/index.html",
"/style.css",
"/manifest.json",
"/assets/calc.js",
"/assets/functions.js",
"/assets/images/logo.svg",
"/assets/images/logo_96.ico",
"/assets/images/logo_96.png",
"/assets/images/logo_256.png",
"/assets/images/logo_512.png",
"/assets/images/logo_720.png",
"/assets/images/logo_1024.png",
"https://unpkg.com/akar-icons-fonts",
"https://unpkg.com/aos@2.3.1/dist/aos.js",
"https://unpkg.com/aos@2.3.1/dist/aos.css",
"https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;500;600;700;800&display=swap"
]

self.addEventListener("install", installEvent => {
installEvent.waitUntil(
caches.open(workerCache).then(cache => {
cache.addAll(assets)
})
)
})

self.addEventListener("fetch", fetchEvent => {
fetchEvent.respondWith(
caches.match(fetchEvent.request).then(res => {
return res || fetch(fetchEvent.request)
})
)
})

0 comments on commit b654d6b

Please sign in to comment.