-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
101 lines (90 loc) · 2.85 KB
/
sw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
layout: null
---
var urlsToCache = [];
var CACHE_NAME = 'aji-setiawan-cache-v18';
// Cache posts
// Limits the number of posts that gets cached to 3
// Reads a piece of front-matter in each post that directs the second loop to the folder where the assets are held
{% for post in site.posts limit:3 %}
urlsToCache.push("{{ post.url }}");
{% for file in site.static_files %}
{% if file.path contains post.assets %}
urlsToCache.push("{{ file.path }}");
{% endif %}
{% endfor %}
{% endfor %}
// Cache pages
// Do nothing if it's either an AMP page (as these are served via Googles cache) or the blog page
// Fallback to the offline pages for these
{% for page in site.html_pages %}
{% if page.path contains 'amp-html' or page.path contains 'blog' or page.path contains 'projects' or page.path contains 'services' %}
{% else %}
urlsToCache.push("{{ page.url }}");
{% endif %}
{% endfor %}
// Cache assets
// Removed assets/posts because I only want assets from the most recent posts getting cached
{% for file in site.static_files %}
{% if file.path contains '/assets/css' or file.path contains '/assets/js' or file.path contains '/assets/images' %}
urlsToCache.push("{{ file.path }}");
{% endif %}
{% endfor %}
// Trim specified cache to max size
function trimCache(cacheName, maxItems) {
caches.open(cacheName).then(function(cache) {
cache.keys().then(function(keys) {
if (keys.length > maxItems) {
cache.delete(keys[0]).then(trimCache(cacheName, maxItems));
}
});
});
}
self.addEventListener('message', event => {
if (event.data.command == 'trimCaches') {
// caches.open(CACHE_NAME).then(function(cache) {
// console.log(cache.keys());
// });
// trimCache(CACHE_NAME, 75);
}
});
// Installation of service worker
self.addEventListener('install', function(event) {
self.skipWaiting();
// Perform install steps
caches.open(CACHE_NAME)
.then(function(cache) {
return cache.addAll(urlsToCache);
})
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((key) => {
if (key !== CACHE_NAME){
return caches.delete(key);
}
})
);
})
);
});
self.addEventListener('fetch', (event) => {
let request = event.request,
acceptHeader = request.headers.get('Accept');
// get all non-html pages from cache
// if (acceptHeader.indexOf('text/html') === -1) {
event.respondWith(
caches.open(CACHE_NAME).then(async (cache) => {
return cache.match(event.request).then((response) => {
return response || fetch(event.request).then((response) => {
// if event.request
// cache.put(event.request, response.clone());
return response;
});
});
})
);
// }
});