-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
38 lines (32 loc) · 994 Bytes
/
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
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-messaging.js');
firebase.initializeApp({
'messagingSenderId': '187708231252'
});
var CACHE_NAME = 'my-site-cache-v2';
const URLS = [
'index.html',
'main.js',
'main.css',
'sw.js'
];
const messaging = firebase.messaging();
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CACHE_NAME).then(function (cache) {
return cache.addAll(URLS)
})
)
})
self.addEventListener('fetch', function(event) {
event.respondWith(caches.match(event.request).then(function (response) {
return response || fetch(event.request);
}))
// fetch and cache new data in the backgrond so it can be used on next
// boot
event.waitUntil(caches.open(CACHE_NAME).then(function (cache) {
return fetch(event.request).then(function (response) {
return cache.put(event.request, response);
});
}));
})