Skip to content

Commit

Permalink
Create service-worker.js
Browse files Browse the repository at this point in the history
  • Loading branch information
NajmAjmal-old authored Sep 29, 2023
1 parent 7063345 commit 739883b
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const CACHE_NAME = 'pwa-cache-v1'; // Change this to update the cache
const cacheUrls = [
'/',
'/index.html', // Add any other HTML files you have
'/styles.css',
'/app.js',
'/icon.png',
// Add other assets and resources you want to cache
];

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

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

self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => name !== CACHE_NAME)
.map((name) => caches.delete(name))
);
})
);
});

0 comments on commit 739883b

Please sign in to comment.