-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
119 lines (100 loc) · 3 KB
/
sw.js
File metadata and controls
119 lines (100 loc) · 3 KB
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
const CACHE_NAME = 'whyai-cache-v5';
const SW_PATH = self.location.pathname;
const BASE_PATH = SW_PATH.substring(0, SW_PATH.lastIndexOf('/'));
function asset(path) {
if (path.startsWith(BASE_PATH)) return path;
if (path === '/') return BASE_PATH + '/index.html';
return BASE_PATH + path;
}
const STATIC_ASSETS = [
asset('/index.html'),
asset('/offline.html'),
asset('/manifest.json'),
asset('/custom-whyai.css'),
asset('/high.css'),
asset('/install.html'),
asset('/redirect.html'),
asset('/assets/index-BZ_wFqig.js'),
asset('/assets/index-q-smNyI7.css'),
asset('/assets/wllama-DTxmcCWH.wasm'),
asset('/assets/wllama-JeypyGAC.wasm'),
asset('/icons/192.png'),
asset('/icons/512.png'),
asset('/icons/logo192.png'),
asset('/icons/logo512.png'),
asset('/whyai.png'),
asset('/icons/whyai-off.png'),
asset('/power.png'),
asset('/image.png'),
asset('/generar.png')
];
// INSTALL
self.addEventListener('install', e => {
e.waitUntil(
caches.open(CACHE_NAME).then(async cache => {
for (const a of STATIC_ASSETS) {
try {
const res = await fetch(a);
if (res.ok) await cache.put(a, res);
} catch {}
}
})
);
self.skipWaiting();
});
// ACTIVATE
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k)))
)
);
self.clients.claim();
});
// FETCH
self.addEventListener('fetch', event => {
const req = event.request;
const url = new URL(req.url);
if (req.method !== 'GET') return;
// 🚫 dejar ipapi libre
if (url.hostname === 'ipapi.co') return;
event.respondWith(
(async () => {
try {
const netRes = await fetch(req);
// 🧠 SOLO OFFLINE.HTML → COEP + COOP
if (
req.mode === 'navigate' &&
url.origin === self.location.origin &&
url.pathname.endsWith('/offline.html')
) {
const headers = new Headers(netRes.headers);
headers.set('Cross-Origin-Opener-Policy', 'same-origin');
headers.set('Cross-Origin-Embedder-Policy', 'require-corp');
return new Response(netRes.body, {
status: netRes.status,
statusText: netRes.statusText,
headers
});
}
// 📦 cache solo mismo origen (no documentos)
if (
url.origin === self.location.origin &&
netRes.ok &&
req.destination !== 'document'
) {
const cache = await caches.open(CACHE_NAME);
cache.put(req, netRes.clone()).catch(() => {});
}
return netRes;
} catch {
const cached = await caches.match(req);
if (cached) return cached;
if (req.mode === 'navigate') {
return caches.match(asset('/offline.html'));
}
throw new Error('Network error');
}
})()
);
});