1 const CACHE_NAME = 'jcr-guide-pro-v1.0.0';
2 const urlsToCache = [
3 '/',
4 '/static/js/bundle.js',
5 '/static/css/main.css',
6 '/manifest.json',
7 '/icon-192.png',
8 '/icon-512.png'
9 ];
10
11 // Install Service Worker
12 self.addEventListener('install', (event) => {
13 event.waitUntil(
14 caches.open(CACHE_NAME)
15 .then((cache) => {
16 console.log('Opened cache');
17 return cache.addAll(urlsToCache);
18 })
19 );
20 });
21
22 // Fetch
23 self.addEventListener('fetch', (event) => {
24 event.respondWith(
25 caches.match(event.request)
26 .then((response) => {
27 // Return cached version or fetch from network
28 if (response) {
29 return response;
30 }
31 return fetch(event.request);
32 }
33 )
34 );
35 });
36
37 // Activate
38 self.addEventListener('activate', (event) => {
39 event.waitUntil(
40 caches.keys().then((cacheNames) => {
41 return Promise.all(
42 cacheNames.map((cacheName) => {
43 if (cacheName !== CACHE_NAME) {
44 return caches.delete(cacheName);
45 }
46 })
47 );
48 })
49 );
50 });
51
52 // Background sync for offline functionality
53 self.addEventListener('sync', (event) => {
54 if (event.tag === 'background-sync') {
55 event.waitUntil(doBackgroundSync());
56 }
57 });
58
59 function doBackgroundSync() {
60 // Handle background sync tasks
61 console.log('Background sync triggered');
62 }
63
64 // Push notifications (if needed in future)
65 self.addEventListener('push', (event) => {
66 const options = {
67 body: event.data ? event.data.text() : 'New update available',
68 icon: '/icon-192.png',
69 badge: '/icon-192.png'
70 };
71
72 event.waitUntil(
73 self.registration.showNotification('JCR Guide Pro', options)
74 );
75 });