Skip to content

Commit 612f769

Browse files
author
Pablo Vieira
committed
feat(pwa): service worker
1 parent 2582598 commit 612f769

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

_includes/service-worker.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script>
2+
if ('serviceWorker' in navigator) {
3+
console.log('ServiceWorker available \\o/');
4+
window.addEventListener('load', function() {
5+
navigator.serviceWorker.register('/sw.js').then(function(registration) {
6+
console.log('ServiceWorker registration successful with scope: ', registration.scope);
7+
}, function(err) {
8+
console.log('ServiceWorker registration failed: ', err);
9+
});
10+
});
11+
} else {
12+
console.log('ServiceWorker not available :(');
13+
}
14+
</script>

_layouts/default.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@
2020
{% include footer.html %}
2121

2222
{% include analytics.html %}
23+
24+
{% include service-worker.html %}
2325
</body>
2426
</html>

sw.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
layout: null
3+
---
4+
5+
var CACHE_NAME = 'pv8-web-cache-v1';
6+
7+
var urlsToCache = [
8+
'/',
9+
'/css/main.css',
10+
{% for page in site.html_pages %}
11+
'{{ page.url }}',
12+
{% endfor %}
13+
{% for post in site.posts %}
14+
'{{ post.url }}',
15+
{% endfor %}
16+
{% for file in site.static_files %}
17+
{% if file.extname == '.js' or file.path contains '/media' %} '{{ file.path }}', {% endif %}
18+
{% endfor %}
19+
];
20+
21+
// {% if file.extname == '.js' or file.path contains '/portfolio/screenshots' or file.path contains '/portfolio/thumbnails' %} '{{ file.path }}', {% endif %}
22+
23+
const CACHE_STATIC = [
24+
'/css/main.css',
25+
'/media/gamification_balance.png',
26+
'/media/gamification_experience.png',
27+
'/media/gamification_gameelements.png',
28+
'/media/gamification_icon.png',
29+
'/media/gamification_journey.png',
30+
'/media/gamification_players.png',
31+
'/media/Python-logo-notext.png'
32+
];
33+
34+
const CACHE_APP = [
35+
'/',
36+
'/index.html',
37+
'/posts/',
38+
'/contact/'
39+
];
40+
41+
self.addEventListener('install',function(event){
42+
event.waitUntil(
43+
caches.open(CACHE_NAME)
44+
.then(function(cache) {
45+
console.log('Opened cache');
46+
return cache.addAll(urlsToCache);
47+
})
48+
);
49+
});
50+
51+
self.addEventListener('fetch', function(event) {
52+
event.respondWith(
53+
caches.match(event.request)
54+
.then(function(response) {
55+
// Cache hit - return response
56+
if (response) {
57+
return response;
58+
}
59+
return fetch(event.request);
60+
}
61+
)
62+
);
63+
});
64+
65+
self.addEventListener('activate', function(event) {
66+
67+
var cacheWhitelist = [CACHE_NAME];
68+
69+
event.waitUntil(
70+
caches.keys().then(function(cacheNames) {
71+
return Promise.all(
72+
cacheNames.map(function(cacheName) {
73+
if (cacheWhitelist.indexOf(cacheName) === -1) {
74+
return caches.delete(cacheName);
75+
}
76+
})
77+
);
78+
})
79+
);
80+
});

0 commit comments

Comments
 (0)