-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.ts
More file actions
126 lines (116 loc) · 4.23 KB
/
service-worker.ts
File metadata and controls
126 lines (116 loc) · 4.23 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
120
121
122
123
124
125
126
/// <reference lib="webworker" />
// FIX: Removed redundant declaration of 'self'. The 'webworker' lib reference already defines it.
// declare const self: ServiceWorkerGlobalScope;
const CACHE_NAME = 'roadguard-ai-cache-v2'; // Bumped version to ensure new SW activates
const URLS_TO_CACHE = [
'/',
'/index.html',
'/manifest.json',
'/vite.svg',
// Core scripts
'/index.tsx',
'/App.tsx',
// Components
'/components/AnalysisDisplay.tsx',
'/components/AreaHealthDisplay.tsx',
'/components/CameraCapture.tsx',
'/components/ErrorMessage.tsx',
'/components/GisDashboard.tsx',
'/components/Header.tsx',
'/components/IconComponents.tsx',
'/components/ImageUploader.tsx',
'/components/OfflineBanner.tsx',
'/components/RealTimeDetector.tsx',
'/components/ResultCard.tsx',
'/components/SatelliteAnalysis.tsx',
'/components/SatelliteAnalysisDisplay.tsx',
'/components/Spinner.tsx',
'/components/VideoAnalysis.tsx',
'/components/WelcomeScreen.tsx',
// Services & Utils
'/services/geminiService.ts',
'/utils/exportUtils.ts',
'/utils/fileUtils.ts',
'/utils/gpxParser.ts',
'/utils/locationUtils.ts',
'/utils/validationUtils.ts',
'/utils/xmlParser.ts',
// App Logic & Types
'/constants.ts',
'/types.ts',
// External CDNs
'https://cdn.tailwindcss.com',
'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js',
'https://js.arcgis.com/4.29/esri/themes/dark/main.css',
'https://aistudiocdn.com/@google/genai@^1.17.0',
'https://aistudiocdn.com/react@^19.1.1',
'https://aistudiocdn.com/react-dom@^19.1.1/client',
'https://aistudiocdn.com/webworker@^0.8.4'
];
// The install event is fired when the service worker is first installed.
self.addEventListener('install', (event: ExtendableEvent) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('Opened cache and caching app shell');
// Add all the assets to the cache for offline access.
return cache.addAll(URLS_TO_CACHE);
})
.catch(err => {
console.error("Failed to cache resources during install:", err);
})
);
});
// The activate event is fired when the service worker is activated.
// This is a good place to clean up old caches.
self.addEventListener('activate', (event: ExtendableEvent) => {
const cacheWhitelist = [CACHE_NAME];
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheWhitelist.indexOf(cacheName) === -1) {
console.log('Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
})
);
});
// The fetch event is fired for every request the browser makes.
self.addEventListener('fetch', (event: FetchEvent) => {
event.respondWith(
(async () => {
const { request } = event;
// For API calls and non-GET requests, bypass the cache and go directly to the network.
if (request.method !== 'GET' || request.url.includes('generativelanguage.googleapis.com')) {
return fetch(request);
}
const cache = await caches.open(CACHE_NAME);
const cachedResponse = await cache.match(request);
// If we have a response in the cache, serve it (cache-first).
if (cachedResponse) {
return cachedResponse;
}
// If not in cache, fetch from the network.
try {
const networkResponse = await fetch(request);
// If the fetch was successful, clone it and add it to the cache for next time.
// We only cache successful responses to avoid caching errors.
if (networkResponse && networkResponse.status === 200) {
// We must clone the response to be able to cache it and return it.
const responseToCache = networkResponse.clone();
await cache.put(request, responseToCache);
}
return networkResponse;
} catch (error) {
console.error('Service worker fetch failed:', error);
// This will be caught by the browser as a network failure.
// A fallback page could be served here from the cache if available.
throw error;
}
})()
);
});