Skip to content

Commit

Permalink
SW font subsets
Browse files Browse the repository at this point in the history
  • Loading branch information
trys committed Sep 23, 2018
1 parent b6a68cc commit 8619980
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions site/static/sw.js
Expand Up @@ -4,7 +4,7 @@ which is in turn a modified version of Jeremy Keith's service worker (https://ad
with a few additional edits borrowed from Filament Group's. (https://www.filamentgroup.com/sw.js)
*/

(function() {
(function () {
const version = 'v3';
const cacheName = ':trysmudford-2018:';

Expand All @@ -19,46 +19,46 @@ with a few additional edits borrowed from Filament Group's. (https://www.filamen
'/offline/',
'/app.js',
'/css/main.css',
'/fonts/1475520/b290e775-e0f9-4980-914b-a4c32a5e3e36.woff2',
'/fonts/1475544/d513e15e-8f35-4129-ad05-481815e52625.woff2'
'/fonts/1475520/9bdf0737-f98c-477a-9365-ffc41b9d1285-subset.woff2',
'/fonts/1475544/ccd17c6b-e7ed-4b73-b0d2-76712a4ef46b-subset.woff2'
];

function updateStaticCache() {
// These items must be cached for the Service Worker to complete installation
return caches.open(staticCacheName)
.then(cache => {
return cache.addAll(staticAssets.map(url => new Request(url, {credentials: 'include'})));
});
.then(cache => {
return cache.addAll(staticAssets.map(url => new Request(url, { credentials: 'include' })));
});
}

function stashInCache(cacheName, request, response) {
caches.open(cacheName)
.then(cache => cache.put(request, response));
.then(cache => cache.put(request, response));
}

// Limit the number of items in a specified cache.
function trimCache(cacheName, maxItems) {
caches.open(cacheName)
.then(cache => {
cache.keys()
.then(keys => {
if (keys.length > maxItems) {
cache.delete(keys[ 0 ])
.then(trimCache(cacheName, maxItems));
}
.then(cache => {
cache.keys()
.then(keys => {
if (keys.length > maxItems) {
cache.delete(keys[0])
.then(trimCache(cacheName, maxItems));
}
});
});
});
}

// Remove caches whose name is no longer valid
function clearOldCaches() {
return caches.keys()
.then(keys => {
return Promise.all(keys
.filter(key => key.indexOf(version) !== 0)
.map(key => caches.delete(key))
);
});
.then(keys => {
return Promise.all(keys
.filter(key => key.indexOf(version) !== 0)
.map(key => caches.delete(key))
);
});
}

// Events!
Expand All @@ -72,13 +72,13 @@ with a few additional edits borrowed from Filament Group's. (https://www.filamen
self.addEventListener('install', event => {
event.waitUntil(updateStaticCache()
.then(() => self.skipWaiting())
);
);
});

self.addEventListener('activate', event => {
event.waitUntil(clearOldCaches()
.then(() => self.clients.claim())
);
);
});

self.addEventListener('fetch', event => {
Expand All @@ -92,7 +92,7 @@ with a few additional edits borrowed from Filament Group's. (https://www.filamen
if (request.headers.get('Accept').includes('text/html')) {
event.respondWith(
fetch(request)
.then( response => {
.then(response => {
let copy = response.clone();
if (staticAssets.includes(url.pathname) || staticAssets.includes(url.pathname + '/')) {
stashInCache(staticCacheName, request, copy);
Expand All @@ -101,10 +101,10 @@ with a few additional edits borrowed from Filament Group's. (https://www.filamen
}
return response;
})
.catch( () => {
.catch(() => {
// CACHE or FALLBACK
return caches.match(request)
.then( response => response || caches.match('/offline/') );
.then(response => response || caches.match('/offline/'));
})
);
return;
Expand All @@ -119,7 +119,7 @@ with a few additional edits borrowed from Filament Group's. (https://www.filamen
}
return response;
})
.catch( () => {
.catch(() => {
return caches.match(request)
.then(response => response)
.catch(console.error)
Expand Down

0 comments on commit 8619980

Please sign in to comment.