forked from adams549659584/northbound-trading-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
182 lines (172 loc) · 4.73 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// 引入workbox 框架
importScripts('./js/sw/workbox-sw.js');
const SW_VERSION = '1.0.0';
const DATE_NOW = new Date();
const DATE_NOW_NUM = +`${DATE_NOW.getHours()}${DATE_NOW.getMinutes().toString().padStart(2, '0')}`;
const IS_TRADING_TIME = DATE_NOW_NUM >= 930 && DATE_NOW_NUM <= 1500;
// todo debugger
// workbox.setConfig({ debug: true });
workbox.core.setCacheNameDetails({
prefix: 'northbound-trading-monitor',
});
// Updating SW lifecycle to update the app after user triggered refresh
workbox.core.skipWaiting();
workbox.core.clientsClaim();
// 注册成功后要立即缓存的资源列表
workbox.precaching.precacheAndRoute([
// css
{
url: './css/style.css',
revision: '2022.07.23',
},
// js
{
url: './js/index.js',
revision: '2022.07.23',
},
{
url: './js/api/api.js',
revision: '2022.07.23',
},
{
url: './js/echarts/echarts.esm.min.js',
revision: '2022.07.23',
},
{
url: './js/echarts/use-northbound-trading-chart.js',
revision: '2022.07.23',
},
{
url: './js/petite-vue/petite-vue.es.js',
revision: '2022.07.23',
},
{
url: './js/sw/use-workbox.js',
revision: '2022.07.23',
},
{
url: './js/sw/workbox-sw.js',
revision: '2022.07.23',
},
{
url: './js/sw/workbox-window.prod.js',
revision: '2022.07.23',
},
{
url: './js/utils/use-notify.js',
revision: '2022.07.23',
},
// html
{
url: './',
revision: '2022.10.01',
},
// ico
{
url: './favicon.ico',
revision: '2022.07.23',
},
]);
workbox.precaching.cleanupOutdatedCaches();
// image
workbox.routing.registerRoute(
({ request }) => request.destination === 'image',
new workbox.strategies.CacheFirst({
cacheName: 'northbound-trading-monitor-image',
plugins: [
new workbox.expiration.ExpirationPlugin({
maxEntries: 60, // 60 个
maxAgeSeconds: 7 * 24 * 60 * 60, // 7 天
}),
],
})
);
// css
workbox.routing.registerRoute(
({ request }) => request.destination === 'style',
new workbox.strategies.CacheFirst({
cacheName: 'northbound-trading-monitor-css',
plugins: [
new workbox.expiration.ExpirationPlugin({
maxEntries: 60, // 60 个
maxAgeSeconds: 7 * 24 * 60 * 60, // 7 天
}),
],
})
);
// js
workbox.routing.registerRoute(
({ request }) => request.destination === 'script',
new workbox.strategies.CacheFirst({
cacheName: 'northbound-trading-monitor-js',
plugins: [
new workbox.expiration.ExpirationPlugin({
maxEntries: 60, // 60 个
maxAgeSeconds: 7 * 24 * 60 * 60, // 7 天
}),
],
})
);
const apiCacheName = 'northbound-trading-monitor-api';
/**
* @type [import('workbox-core').WorkboxPlugin[]]
*/
const apiCachePlugins = [
new workbox.cacheableResponse.CacheableResponsePlugin({
statues: [0, 200],
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: 10, // 2 个
maxAgeSeconds: 7 * 24 * 60 * 60, // 7 天
}),
{
cacheKeyWillBeUsed: async ({ request, mode, params, event, state }) => {
// `request` is the `Request` object that would otherwise be used as the cache key.
// `mode` is either 'read' or 'write'.
// Return either a string, or a `Request` whose `url` property will be used as the cache key.
// Returning the original `request` will make this a no-op.
// 北向资金接口缓存 key 不要时间戳
if (request.url.includes('/api/qt/kamtbs.rtmin/get') || request.url.includes('/api/qt/kamt.rtmin/get')) {
return request.url.substr(0, request.url.lastIndexOf('&_='));
}
return request;
},
},
];
workbox.routing.registerRoute(
({ request }) => request.url.includes('/api/qt'),
IS_TRADING_TIME
? new workbox.strategies.NetworkFirst({
cacheName: apiCacheName,
plugins: apiCachePlugins,
})
: new workbox.strategies.CacheFirst({
cacheName: apiCacheName,
plugins: apiCachePlugins,
})
);
// service worker通过message和主线程通讯
self.addEventListener('message', event => {
const replyPort = event.ports[0];
const message = event.data;
console.log(`sw message : `, message);
if (replyPort && message && message.type === 'GET_VERSION') {
replyPort.postMessage(SW_VERSION);
}
});
// 安装阶段可删除旧缓存等等,hack
self.addEventListener('install', async event => {
await caches.open(apiCacheName).then(async cache => {
const requests = await cache.keys();
return await Promise.all(
requests.map(request => {
if (request.url.includes('/api/qt/kamt.rtmin/get') && request.url.lastIndexOf('&_=')) {
console.log(`del bad req : `, request);
return cache.delete(request);
} else {
return Promise.resolve();
}
})
);
});
});