-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
66 lines (60 loc) · 2.2 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
/**
* Welcome to your Workbox-powered service worker!
*
* You'll need to register this file in your web app and you should
* disable HTTP caching for this file too.
* See https://goo.gl/nhQhGp
*
* The rest of the code is auto-generated. Please don't update this file
* directly; instead, make changes to your Workbox build configuration
* and re-run your build process.
* See https://goo.gl/2aRDsh
*/
importScripts("https://storage.googleapis.com/workbox-cdn/releases/3.6.3/workbox-sw.js");
workbox.core.setCacheNameDetails({prefix: "goBuy-cache"});
workbox.skipWaiting(); // 强制等待中的 Service Worker 被激活
workbox.clientsClaim(); // Service Worker 被激活后使其立即获得页面控制权
/**
* The workboxSW.precacheAndRoute() method efficiently caches and responds to
* requests for URLs in the manifest.
* See https://goo.gl/S9QRab
*/
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.suppressWarnings();
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});
workbox.routing.registerRoute(/.*\.(?:js|css|png|jpg)/, workbox.strategies.cacheFirst(), 'GET');
workbox.routing.registerRoute(/.*\.html/, workbox.strategies.networkFirst(), 'GET');
workbox.routing.registerRoute(/(.*)list(.*)/g, workbox.strategies.cacheFirst(), 'GET');
// 后台同步
const NotificationForUser = () => {
self.registration.showNotification('留言发送成功', {
body: '感谢您的留言!',
icon: './static/phoneIcon.png'
});
};
const bgSyncPlugin = new workbox.backgroundSync.Plugin('leaveMsg-queue', {
maxRetentionTime: 24 * 60, // Retry for max of 24 Hours
callbacks: {
queueDidReplay: NotificationForUser // 通知用户
}
});
workbox.routing.registerRoute(
/(.*)leaveMsg(.*)/g,
new workbox.strategies.NetworkOnly({
plugins: [bgSyncPlugin]
}),
'POST'
);
self.addEventListener('push', function (event) {
console.log('收到服务端push消息')
var payload = event.data ? JSON.parse(event.data.text()) : 'no payload'
console.log(payload.msg)
var title = '好购商城'
event.waitUntil(
self.registration.showNotification(title, {
body: payload.msg,
// url: payload.url,
icon: './static/phoneIcon.png'
})
)
})