Skip to content

Commit

Permalink
add sw
Browse files Browse the repository at this point in the history
  • Loading branch information
xerosanyam committed Feb 5, 2024
1 parent 8560a3d commit e757c0a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,21 @@ <h4>when to use</h4>
<li>less user interactivity</li>
</ul>
</section>

<script>
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("/sw.js")
.then((registration) =>
console.log(
"Service Worker registered with scope:",
registration.scope
)
)
.catch((error) =>
console.error("Service Worker registration failed:", error)
);
}
</script>
</body>
</html>
19 changes: 19 additions & 0 deletions sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const CACHE_NAME = 'my-cache-v0';
const urlsToCache = [
'/',
'/index.html'
];

self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => cache.addAll(urlsToCache))
);
});

self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(response => response || fetch(event.request))
);
});

0 comments on commit e757c0a

Please sign in to comment.