Compare commits

..

No commits in common. "55a8368001d86661596f3deda3a7dc291ae864d6" and "df0ec44936cb06d4ec0282e3cd057321ad655b2a" have entirely different histories.

2 changed files with 51 additions and 55 deletions

View file

@ -66,7 +66,4 @@
{{- partial "external/twitter_cards.html" . -}} {{- partial "external/twitter_cards.html" . -}}
{{ partial "citation.html" . }} {{ partial "citation.html" . }}
{{- $script := resources.Get "js/script.js" -}}
<script src="{{ $script.Permalink }}" async></script>
</head> </head>

View file

@ -12,81 +12,80 @@ 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) { if (keys.length > maxItems) {
cache.delete(keys[0]).then(trimCache(name, maxItems)); cache.delete(keys[0]).then(trimCache(name, maxItems));
} }
});
}); });
});
} }
// Listens for trimCache command which occurs on page load // Listens for trimCache command which occurs on page load
self.addEventListener('message', event => { self.addEventListener('message', event => {
if (event.data.command == 'trimCache') { if (event.data.command == 'trimCache') {
trimCache(version + cacheName, maxItems); trimCache(version + cacheName, maxItems);
} }
}); });
// 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 // Remove caches whose name is no longer valid
return Promise.all(keys return Promise.all(keys
.filter(function (key) { .filter(function (key) {
return key.indexOf(version) !== 0; return key.indexOf(version) !== 0;
}) })
.map(function (key) { .map(function (key) {
return caches.delete(key); return caches.delete(key);
}) })
); );
}) })
); );
}); });
// Listen for request events // Listen for request events
self.addEventListener('fetch', function (event) { self.addEventListener('fetch', function (event) {
let request = event.request; let request = event.request;
// 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(
// Attepmt to grab the latest copy from the network // Attepmt to grab the latest copy from the network
fetch(request).then(function (response) { fetch(request).then(function (response) {
// 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 let cacheCopy = response.clone();
if (request.url.includes("/badges")) { event.waitUntil(caches.open(version + cacheName).then(function (cache) {
let cacheCopy = response.clone(); return cache.put(request, cacheCopy);
event.waitUntil(caches.open(version + cacheName).then(function (cache) { }));
return cache.put(request, cacheCopy);
}));
}
return response; return response;
}).catch(function (error) { }).catch(function (error) {
// Otherwise, check the cache. // Otherwise, check the cache.
return caches.match(request).then(function (response) { return caches.match(request).then(function (response) {
// Show offline page if cache misses for HTML pages. // Show offline page if cache misses for HTML pages.
if (isRequestType('text/html')) { if (isRequestType('text/html')) {
return response || caches.match(offlinePage); return response || caches.match(offlinePage);
} }
return response; return response;
});
});
}) })
); );
}); });