Skip to content

Commit f3879e1

Browse files
committed
My first service worker
1 parent 15f9b49 commit f3879e1

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

_layouts/default.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77

88
{{ content }}
99

10+
<script>
11+
if ('serviceWorker' in navigator) {
12+
window.addEventListener('load', function() {
13+
navigator.serviceWorker.register('/sw.js').then(function(registration) {
14+
// Successfully registered the Service Worker
15+
//console.log('Service Worker registration successful with scope: ', registration.scope);
16+
}).catch(function(err) {
17+
// Failed to register the Service Worker
18+
//console.log('Service Worker registration failed: ', err);
19+
});
20+
});
21+
}
22+
</script>
23+
1024
<script>
1125
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
1226
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

offline.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
layout: page
3+
title: "Oops! It Looks Like Your Connection Is Offline"
4+
page-class: page--offline
5+
meta: "CSS Wizardry’s offline content"
6+
permalink: /offline/
7+
---
8+
9+
It looks as though your device is offline. You’ll probably want to check back
10+
when you have a connection, but until then you can:
11+
12+
* [head to the homepage](/);
13+
* [read a little more about CSS Wizardry](/about/);
14+
* [learn about the services I offer](/services/), or;
15+
* [find out how best to get in touch with me](/contact/).
16+
17+
Or if you’d like to read something:
18+
19+
* [Pragmatic, Practical, and Progressive Theming with Custom Properties](/2016/10/pragmatic-practical-progressive-theming-with-custom-properties/)
20+
* [Mixins Better for Performance](/2016/02/mixins-better-for-performance/)
21+
* [More Transparent UI Code with Namespaces](/2015/03/more-transparent-ui-code-with-namespaces/)
22+
* [BEMIT: Taking the BEM Naming Convention a Step Further](/2015/08/bemit-taking-the-bem-naming-convention-a-step-further/)

sw.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var cacheName = 'csswizardry:0001';
2+
var cacheFiles = [
3+
'/',
4+
'/about/',
5+
'/services/',
6+
'/contact/',
7+
'/offline/',
8+
'/2015/03/more-transparent-ui-code-with-namespaces/',
9+
'/2015/08/bemit-taking-the-bem-naming-convention-a-step-further/',
10+
'/2016/02/mixins-better-for-performance/',
11+
'/2016/10/pragmatic-practical-progressive-theming-with-custom-properties/'
12+
];
13+
14+
self.addEventListener('install', function(event) {
15+
event.waitUntil(
16+
caches.open(cacheName)
17+
.then(function(cache) {
18+
//console.log('Opened cache');
19+
return cache.addAll(cacheFiles);
20+
})
21+
);
22+
});
23+
24+
self.addEventListener('fetch', function(event) {
25+
event.respondWith(
26+
caches.match(event.request)
27+
.then(function(response) {
28+
// Grab the asset from SW cache.
29+
if (response) {
30+
return response;
31+
}
32+
return fetch(event.request);
33+
}).catch(function() {
34+
// Can't access the network return an offline page from the cache
35+
return caches.match('/offline/');
36+
})
37+
);
38+
});

0 commit comments

Comments
 (0)