Skip to content

Commit

Permalink
update sw.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kaheetonaa authored Oct 20, 2024
1 parent 5dc4390 commit b5d5ed1
Showing 1 changed file with 52 additions and 19 deletions.
71 changes: 52 additions & 19 deletions sw.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open('iDSandbox4All').then((cache) => cache.addAll([
'/id_sandbox_for_all/',
'/id_sandbox_for_all/index.html',
'/id_sandbox_for_all/land.html',
'/id_sandbox_for_all/icon/icon.png',
'/id_sandbox_for_all/dist/iD.js',
'/id_sandbox_for_all/dist/iD.css',
])),
);
});

self.addEventListener('fetch', (e) => {
console.log(e.request.url);
e.respondWith(
caches.match(e.request).then((response) => response || fetch(e.request)),
);
});
var GHPATH = '/github-page-pwa';
var APP_PREFIX = 'gppwa_';
var VERSION = 'version_002';
var URLS = [
`${GHPATH}/`,
`${GHPATH}/index.html`,
`${GHPATH}/css/styles.css`,
`${GHPATH}/img/icon.png`,
`${GHPATH}/js/app.js`
]

var CACHE_NAME = APP_PREFIX + VERSION
self.addEventListener('fetch', function (e) {
console.log('Fetch request : ' + e.request.url);
e.respondWith(
caches.match(e.request).then(function (request) {
if (request) {
console.log('Responding with cache : ' + e.request.url);
return request
} else {
console.log('File is not cached, fetching : ' + e.request.url);
return fetch(e.request)
}
})
)
})

self.addEventListener('install', function (e) {
e.waitUntil(
caches.open(CACHE_NAME).then(function (cache) {
console.log('Installing cache : ' + CACHE_NAME);
return cache.addAll(URLS)
})
)
})

self.addEventListener('activate', function (e) {
e.waitUntil(
caches.keys().then(function (keyList) {
var cacheWhitelist = keyList.filter(function (key) {
return key.indexOf(APP_PREFIX)
})
cacheWhitelist.push(CACHE_NAME);
return Promise.all(keyList.map(function (key, i) {
if (cacheWhitelist.indexOf(key) === -1) {
console.log('Deleting cache : ' + keyList[i] );
return caches.delete(keyList[i])
}
}))
})
)
})

0 comments on commit b5d5ed1

Please sign in to comment.