-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
52 lines (44 loc) · 1.6 KB
/
sw.js
File metadata and controls
52 lines (44 loc) · 1.6 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
// sw.js
importScripts('paramProcessor.js');
self.addEventListener('install', (event) => {
console.log('Service Worker: Installazione in corso');
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
console.log('Service Worker: Attivazione in corso');
event.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', (event) => {
if (event.request.url.includes('/api')) {
event.respondWith(handleApiRequest(event.request));
}
});
async function handleApiRequest(request) {
try {
// Registra il tempo di inizio per le performance
const startTime = performance.now();
const url = new URL(request.url);
const params = ParamProcessor.parseParametersFromQuery(url.searchParams.entries());
// Usa l'analizzatore con startTime (come in index.html)
const result = ParamProcessor.analyzeParameters(params, startTime);
return new Response(JSON.stringify(result, null, 2), {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-store'
}
});
} catch (error) {
return new Response(JSON.stringify({
error: "Service Worker API Error",
message: error.message,
expected: "Identical behavior to index.html parameter processing"
}), {
status: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
});
}
}