-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
89 lines (84 loc) · 2.7 KB
/
service-worker.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
const VERSION = '004';
const filesToCache = [
// HTML
'https://atrogolo.github.io/Materialize/',
'https://atrogolo.github.io/Materialize/index.html',
'samples/cards.html',
'samples/carousel.html',
'samples/collections.html',
'samples/forms.html',
'samples/materialbox.html',
'samples/scrollfire.html',
// Font and CSS
'/Materialize/favicons/favicon-16x16.png?v=2',
'/Materialize/favicons/favicon-32x32.png?v=2',
'css/fonts/material-icons-v33.woff2',
'css/fonts/monserrat-v12.woff2',
'https://atrogolo.github.io/Materialize/css/fonts/fontawesome-webfont.woff2',
'css/materialize.min.css',
'css/my_style.css',
'css/my_fontawesome.min.css',
// Images
'css/backgrounds/background1.jpg',
'css/backgrounds/background2.jpg',
'css/backgrounds/background3.jpg',
'css/backgrounds/bg_middle_crop.jpg',
'css/backgrounds/bg_middle.jpg',
'css/backgrounds/bg_sunrise_crop.jpg',
'css/backgrounds/bg_sunrise.jpg',
'css/backgrounds/cards_ascanio.jpg',
'css/backgrounds/cards_colosseum.jpg',
'css/backgrounds/cards_vespa.jpg',
'css/backgrounds/italy_flag.jpg',
'css/backgrounds/me.jpg',
// Scripts
'js/materialize.min.js',
'js/init.js',
'js/cards.js',
'js/materialbox.js',
'js/scrollfire.js',
'https://code.jquery.com/jquery-2.1.1.min.js' //, 'https://www.google-analytics.com/analytics.js'
];
// Install event listener - First event on service worker detection
self.addEventListener('install', function (event) {
console.log("Installing SW - Version: " + VERSION);
event.waitUntil(
caches.open(VERSION).then(function (cache) {
console.log('[ServiceWorker] Caching app shell');
return cache.addAll(filesToCache);
})
.catch(function (error) {
console.error("INSTALL FAILED! ", error);
})
);
});
// Activate event listener - Remove other caches different from the last one
self.addEventListener('activate', function (event) {
event.waitUntil(
caches.keys().then(function (keyList) {
return Promise.all(keyList.map(function (key) {
if (key !== VERSION) {
console.log("Deleting cache key: " + key);
return caches.delete(key);
}
}));
})
);
});
// Fetch event listener - Every network request will be intercepted here
self.addEventListener('fetch', function (event) {
event.respondWith(
caches.open(VERSION)
.then(function (cache) {
console.log("Fetch event for request: " + event.request);
// cache first, fallback to network
return cache.match(event.request, {
ignoreSearch: true
})
.then(function (response) {
console.log("Response from cache: " + response);
return response || fetch(event.request);
});
})
);
});