Don't trim fundamental pages

This commit is contained in:
Brandon Rozek 2024-10-20 10:56:57 -04:00
parent 55a8368001
commit 5e1ec3f213
No known key found for this signature in database
GPG key ID: 26E457DA82C9F480

View file

@ -6,7 +6,8 @@ let maxItems = 100;
self.addEventListener('install', function (event) { self.addEventListener('install', function (event) {
// Cache offline fundamentals // Cache offline fundamentals
event.waitUntil(caches.open(version + cacheName).then(function (cache) { event.waitUntil(
caches.open(version + cacheName).then(function (cache) {
return cache.addAll(offlineFundamentals); return cache.addAll(offlineFundamentals);
})); }));
}); });
@ -14,11 +15,21 @@ self.addEventListener('install', function (event) {
function trimCache(name, maxItems) { function trimCache(name, maxItems) {
caches.open(name).then(function(cache) { caches.open(name).then(function(cache) {
cache.keys().then(function(keys) { cache.keys().then(function(keys) {
if (keys.length > maxItems) { // Make sure offlineFundamentals don't get deleted
cache.delete(keys[0]).then(trimCache(name, maxItems)); possibleDelete = keys.filter(function(key) {
!offlineFundamentals.includes(key)
});
// Keep track of each delete
deleteInProgress = []
for (let i = 0; i < keys.length - maxItems; i++) {
deleteInProgress.push(cache.delete(possibleDelete[i]));
} }
});
}); // Return when everything is resolved
return Promise.all(deleteInProgress);
})
})
} }
// Listens for trimCache command which occurs on page load // Listens for trimCache command which occurs on page load
@ -28,17 +39,17 @@ self.addEventListener('message', event => {
} }
}); });
// If the version changes, invalidate the cache // If the version changes, invalidate the cache
self.addEventListener('activate', function (event) { self.addEventListener('activate', function (event) {
event.waitUntil( event.waitUntil(
caches.keys() caches.keys()
.then(function (keys) { .then(function (keys) {
// Remove caches whose name is no longer valid
return Promise.all(keys return Promise.all(keys
// Find all caches that aren't the current version
.filter(function (key) { .filter(function (key) {
return key.indexOf(version) !== 0; return !key.includes(version)
}) })
// Delete the cache
.map(function (key) { .map(function (key) {
return caches.delete(key); return caches.delete(key);
}) })
@ -53,16 +64,19 @@ self.addEventListener('fetch', function (event) {
// Always fetch non-GET requests from the network // Always fetch non-GET requests from the network
if (request.method !== 'GET') { if (request.method !== 'GET') {
event.respondWith( event.respondWith(fetch(request)
fetch(request) .catch(function () {
.catch(function () { return caches.match(offlinePage);
return caches.match(offlinePage); })
})
); );
return; return;
} }
let isRequestType = function(name) { return request.headers.get('Accept').includes(name); } let isRequestType = function(name) {
return request.headers
.get('Accept')
.includes(name);
}
// Network-first Approach // Network-first Approach
event.respondWith( event.respondWith(
@ -71,7 +85,7 @@ self.addEventListener('fetch', function (event) {
// If successful, create a copy of the response // If successful, create a copy of the response
// and save it to the cache // and save it to the cache
// Note: Ignore badges // Note: Ignore badges
if (request.url.includes("/badges")) { if (!request.url.includes("/badges")) {
let cacheCopy = response.clone(); let cacheCopy = response.clone();
event.waitUntil(caches.open(version + cacheName).then(function (cache) { event.waitUntil(caches.open(version + cacheName).then(function (cache) {
return cache.put(request, cacheCopy); return cache.put(request, cacheCopy);