-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
147 lines (125 loc) · 3.87 KB
/
service-worker.js
File metadata and controls
147 lines (125 loc) · 3.87 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Service Worker for SecureVault PWA
const CACHE_NAME = 'nodetools-v1';
const ASSETS_TO_CACHE = [
'/',
'/index.html',
'/styles/main.css',
'/js/app.js',
'/js/auth.js',
'/js/database.js',
'/js/editor.js',
'/js/files.js',
'/js/photos.js',
'/js/ui.js',
'/manifest.json'
];
// Install event - cache static assets
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('Caching app assets');
return cache.addAll(ASSETS_TO_CACHE);
})
.then(() => self.skipWaiting())
);
});
// Activate event - clean up old caches
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheName !== CACHE_NAME) {
console.log('Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
}).then(() => self.clients.claim())
);
});
// Fetch event - serve from cache or network
self.addEventListener('fetch', event => {
// Skip for non-GET requests
if (event.request.method !== 'GET') return;
// Skip for Chrome extension requests
if (event.request.url.startsWith('chrome-extension://')) return;
// Skip for browser-sync requests during development
if (event.request.url.includes('browser-sync')) return;
// Handle API requests differently (don't cache)
if (event.request.url.includes('/api/')) {
return fetch(event.request);
}
event.respondWith(
caches.match(event.request)
.then(cachedResponse => {
// Return cached response if available
if (cachedResponse) {
return cachedResponse;
}
// Otherwise fetch from network
return fetch(event.request)
.then(response => {
// Don't cache if not a valid response
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
// Clone the response as it can only be consumed once
const responseToCache = response.clone();
caches.open(CACHE_NAME)
.then(cache => {
cache.put(event.request, responseToCache);
});
return response;
})
.catch(error => {
console.error('Fetch failed:', error);
// If offline and requesting an HTML page, show fallback
if (event.request.headers.get('Accept').includes('text/html')) {
return caches.match('/index.html');
}
return new Response('Network error', {
status: 408,
headers: { 'Content-Type': 'text/plain' }
});
});
})
);
});
// Handle push notifications
self.addEventListener('push', event => {
if (!event.data) return;
const data = event.data.json();
const options = {
body: data.body || 'New notification',
icon: '/images/icons/icon-192x192.png',
badge: '/images/icons/badge-72x72.png',
data: {
url: data.url || '/'
}
};
event.waitUntil(
self.registration.showNotification(data.title || 'NodeTools', options)
);
});
// Handle notification click
self.addEventListener('notificationclick', event => {
event.notification.close();
const url = event.notification.data.url;
event.waitUntil(
clients.matchAll({ type: 'window' })
.then(windowClients => {
// If there is an open window, focus it
for (const client of windowClients) {
if (client.url === url && 'focus' in client) {
return client.focus();
}
}
// Otherwise open a new window
if (clients.openWindow) {
return clients.openWindow(url);
}
})
);
});