Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Service Worker and an offline page #234

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
{%- seo -%}
<link rel="stylesheet" href="{{ "/assets/main.css" | relative_url }}">
<link rel="stylesheet" href="{{ "/assets/css/style.css" | relative_url }}">
{%- feed_meta -%}
{%- if jekyll.environment == 'production' and site.google_analytics -%}
{%- include google-analytics.html -%}
Expand Down
9 changes: 9 additions & 0 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@

{%- include footer.html -%}

<script type="text/javascript">
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/sw.js").then(function(registration) {
console.log('Success!');
}).catch(function(error) {
console.log('Failure!', error);
})
}
</script>
</body>

</html>
File renamed without changes.
23 changes: 23 additions & 0 deletions offline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
layout: default
---

<style type="text/css" media="screen">
.container {
margin: 10px auto;
max-width: 600px;
text-align: center;
}
h1 {
margin: 30px 0;
font-size: 4em;
line-height: 1;
letter-spacing: -1px;
}
</style>

<div class="container">
<h1>Oops!</h1>

<p>It looks like you're offline.</p>
</div>
61 changes: 61 additions & 0 deletions sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
layout: null
---
'use strict';

const version = 'v1';
const staticCacheName = "version-{{site.time | date: '%Y%m%d%H%M%S'}}";

addEventListener('install', installEvent => {
console.log('The service worker is installing...');
skipWaiting();
installEvent.waitUntil(
caches.open(staticCacheName)
.then( staticCache => {
return staticCache.addAll([
'/assets/css/style.css',
chrisfinazzo marked this conversation as resolved.
Show resolved Hide resolved
'/assets/minima-social-icons.svg',
'/about/index.html',
'/404.html',
'offline.html'
]);
})
);
});

addEventListener('activate', activateEvent => {
console.log('The service worker is activated. Deleting old caches...');
activateEvent.waitUntil(
caches.keys()
.then( cacheNames => {
return Promise.all(
cacheNames.map( cacheName => {
if (cacheName != staticCacheName) {
return caches.delete(cacheName);
}
})
);
})
.then( () => {
return clients.claim();
})
);
});


addEventListener('fetch', fetchEvent => {
console.log('The service worker is listening.');
const request = fetchEvent.request;
fetchEvent.respondWith(
caches.match(request)
.then ( responseFromCache => {
if (responseFromCache) {
return responseFromCache;
}
return fetch(request)
.catch( error => {
return caches.match('/offline.html');
});
})
);
});