-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
109 lines (101 loc) · 2.21 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
self.importScripts('https://cdn.jsdelivr.net/npm/workbox-sw');
workbox.setConfig({
modulePathCb(e) {
// console.log(e);
return "https://cdn.jsdelivr.net/npm/" + e;
}
});
workbox.core.setCacheNameDetails({
prefix: 'my-app',
suffix: 'v1'
});
const baseUrl = 'https://junzhe.tk';
const filter = (url, arr) => {
const filter = arr.filter(regexp => url.href.match(regexp));
return filter.length > 0
}
workbox.routing.registerRoute(
({
url
}) => {
return filter(url, [
'https://cdn.jsdelivr.net/npm/react/umd/react.development.js',
'https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.development.js',
/.+(?:\.ico|\.svg|\.woff)$/ig,
/g\.alicdn\.com/ig,
/\/img\/rss\.png/ig,
])
},
workbox.strategies.cacheFirst(),
);
workbox.routing.registerRoute(
/.*/ig,
({
url,
event
}) => {
event.request.text().then(text => fetch(baseUrl + url.pathname, {
method: 'POST',
body: text,
}));
return new Response('ok', {
status: 200,
statusText: 'ok'
});
},
'POST'
);
workbox.routing.registerRoute(
/\/proxy\?url=.*/ig,
async ({
url,
}) => {
try {
return await fetch(baseUrl + '/proxy' + url.search, {
redirect: 'follow',
});
} catch (error) {
console.error(error);
return new Response('proxy error', {
status: 599,
statusText: 'proxy error'
});
}
},
'GET'
);
workbox.routing.registerRoute(
({
url,
}) => filter(url, [
/\.zhimg\.com/ig,
/miaozhen\.com/ig,
/res\.infoq\.com/ig,
/www\.cnbeta\.com.*read\?_csrf=.*/ig,
]),
() => new Response('block', {
status: 599,
statusText: 'block'
}),
);
self.onerror = function(e) {
console.error(e);
}
self.addEventListener('push', function(event) {
const notificationData = event.data.json();
console.log('收到推送', notificationData);
const title = notificationData.title;
// 弹消息框
event.waitUntil(self.registration.showNotification(title, notificationData));
});
self.addEventListener('notificationclick', function(event) {
console.log('点击消息框');
const notification = event.notification;
notification.close();
event.waitUntil(
clients.openWindow(notification.data.url)
);
});
self.addEventListener('install', (event) => {
event.waitUntil(self.skipWaiting())
})