-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
122 lines (112 loc) · 3.36 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
importScripts('/js/idb.js');
const version = '0.2.3';
let cacheName = `restaurant-v${version}`;
let assetCache = 'restaurant-image-cache';
self.addEventListener("install", event => {
event.waitUntil(
caches.open(cacheName)
.then(cache => {
return cache.addAll([
"/",
"/restaurant.html",
"/css/styles.css",
"/js/idb.js",
"/js/dbhelper.js",
"/js/main.js",
"/js/restaurant_info.js",
"/manifest.json",
"/sw.js"
])
.catch(error => {
console.log("Cache failed: " + error);
})
})
);
});
self.addEventListener('fetch', event => {
let requestUrl = new URL(event.request.url);
// skip review url
if (requestUrl.pathname.startsWith('/reviews/') || requestUrl.pathname.startsWith('/restaurants')){
return;
}
if (requestUrl.pathname.startsWith('/img/')){
event.respondWith(imageCache(event.request));
return;
}
if (event.request.method === 'POST'){
return;
}
event.respondWith(
caches.match(event.request).then(response =>{
return response || fetch(event.request)
.then(fetchRes => {
return caches.open(cacheName)
.then(cache => {
cache.put(event.request, fetchRes.clone());
return fetchRes;
});
})
.catch(error => {
if (requestUrl.pathname.startsWith('/img/')){
return caches.match('/img/na.png');
}
return new Response('No internet connection',{
status: 404,
statusText: "No internet."
});
});
})
);
});
// when go live, process the data queue
self.addEventListener('sync', event => {
if (event.tag === 'outbox') {
event.waitUntil(
// process all the data
idb.open('restaurant-outbox')
.then(db => {
return db.transaction('outbox').objectStore('outbox').getAll();
})
.then(data => {
return Promise.all(data.map(item => {
return fetch(`http://localhost:1337/restaurants/${item.restaurantId}/`, {
method: 'POST',
body: JSON.stringify(item)
})
.then(res => {
return res.json();
})
.then(res => {
idb.open('restaurant-outbox').then(db => {
const database = db.transaction('outbox', "readwrite");
database.objectStore('outbox').delete(item.id);
return database.complete;
})
})
.catch(err => {
console.log('Got problem on fetching data while after sync: ', err);
})
}))
})
.catch(err => {
console.error('Having some problem when opening db: ', err);
})
);
}
});
// cache images
imageCache = (request) => {
let imageUrl = request.url.replace(/_\dx.jpg$/, '');
// return images from the "assetCache" cache if they
// are in there. If not fetch from the cache.
return caches.open(assetCache).then(cache => {
return cache.match(imageUrl).then(response => {
if (response) return response;
return fetch(request).then(networkResponse => {
// send copy of the response to the cache.
cache.put(imageUrl, networkResponse.clone());
return networkResponse;
});
})
})
};