Skip to content

Commit

Permalink
add service worker.
Browse files Browse the repository at this point in the history
  • Loading branch information
ethnt committed Sep 8, 2017
1 parent 49ba22a commit 07445d7
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 1,396 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Expand Up @@ -43,4 +43,4 @@ RUBY VERSION
ruby 2.3.1p112

BUNDLED WITH
1.13.2
1.13.3
20 changes: 0 additions & 20 deletions Gulpfile.js

This file was deleted.

1 change: 1 addition & 0 deletions _config.yml
@@ -1,5 +1,6 @@
---
title: Ethan Turkeltaub
description: "The website of software developer Ethan Turkeltaub."
timezone: America/New_York
collections:
awards:
Expand Down
Empty file removed _jsc/application.js
Empty file.
23 changes: 20 additions & 3 deletions _layouts/default.html
Expand Up @@ -7,10 +7,17 @@
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="{{ site.description }}">

<link rel="shortcut icon" href="/favicon.png">

<link rel="manifest" href="/manifest.json">

<title>{{ site.title }}</title>

<link href="/assets/application.css" rel="stylesheet">

<script>
!function(e,t,n,a,c,l,m,o,d,f,h,i){c[l]&&(d=e.createElement(t),d[n]=c[l],e[a]("head")[0].appendChild(d),e.documentElement.className+=" wf-cached"),function s(){for(d=e[a](t),f="",h=0;h<d.length;h++)i=d[h][n],i.match(m)&&(f+=i);f&&(c[l]="/**/"+f),setTimeout(s,o+=o)}()}(document,"style","innerHTML","getElementsByTagName",localStorage,"tk",/^@font|^\.tk-/,100);
</script>
<script src="https://use.typekit.net/{{ site.typekit }}.js"></script>
<script>try{Typekit.load({ async: true });}catch(e){}</script>
</head>
Expand All @@ -21,7 +28,7 @@ <h1><a href="/">{{ site.title }}</a></h1>
<main role="main">
{{ content }}
</main>
<footer role="footer">
<footer role="contentinfo">
<p>&copy; {{ 'now' | date: '%Y' }} <a href="/">{{ site.title }}</a>.</p>
<nav>
<ul>
Expand All @@ -31,7 +38,17 @@ <h1><a href="/">{{ site.title }}</a></h1>
</ul>
</nav>
</footer>
{% include analytics.html %}
<script src="/assets/application.js"></script>

<!-- {% include analytics.html %} -->

<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('service-worker.js').then(function () {
console.log('ServiceWorker registered successfully');
}).catch(function (err) {
console.log('Service worker registration failed: ', err);
});
}
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion _sass/_footer.sass
@@ -1,4 +1,4 @@
footer[role="footer"]
footer[role="contentinfo"]
opacity: 0.6
margin-left: rhythm(4)
margin-top: rhythm(2)
Expand Down
7 changes: 7 additions & 0 deletions assets/application.js
@@ -0,0 +1,7 @@
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js').then(function () {
console.log('ServiceWorker registered successfully');
}).catch(function (err) {
console.log('Service worker registration failed: ', err);
});
}
16 changes: 16 additions & 0 deletions manifest.json
@@ -0,0 +1,16 @@
{
"name": "Ethan Turkeltaub",
"short_name": "Ethan Turkeltaub",
"start_url": ".",
"lang": "en-US",
"display": "browser",
"background_color": "#c7d4be",
"description": "The website of software developer Ethan Turkeltaub.",
"icons": [
{
"src": "favicon.png",
"sizes": "50x50",
"type": "image/png"
}
]
}
17 changes: 0 additions & 17 deletions package.json

This file was deleted.

50 changes: 50 additions & 0 deletions service-worker.js
@@ -0,0 +1,50 @@
const PRECACHE = 'precache-v1';
const RUNTIME = 'runtime';

const PRECACHE_URLS = [
'assets/application.css',
'index.html',
'./',
];

self.addEventListener('install', event => {
event.waitUntil(
caches.open(PRECACHE)
.then(cache => cache.addAll(PRECACHE_URLS))
.then(self.skipWaiting())
);
});

self.addEventListener('activate', event => {
const currentCaches = [PRECACHE, RUNTIME];

event.waitUntil(
caches.keys().then(cacheNames => {
return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
}).then(cachesToDelete => {
return Promise.all(cachesToDelete.map(cacheToDelete => {
return caches.delete(cacheToDelete);
}));
}).then(() => self.clients.claim())
);
});

self.addEventListener('fetch', event => {
if (event.request.url.startsWith(self.location.origin)) {
event.respondWith(
caches.match(event.request).then(cachedResponse => {
if (cachedResponse) {
return cachedResponse;
}

return caches.open(RUNTIME).then(cache => {
return fetch(event.request).then(response => {
return cache.put(event.request, response.clone()).then(() => {
return response;
});
});
});
})
);
}
});

0 comments on commit 07445d7

Please sign in to comment.