-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
183 lines (159 loc) · 4.93 KB
/
sw.js
File metadata and controls
183 lines (159 loc) · 4.93 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Service Worker for PWA and Offline Support
const CACHE_NAME = 'pdf-image-editor-v1';
const urlsToCache = [
'/',
'/index.html',
'/css/styles.css',
'/js/app.js',
'/js/utils.js',
'/js/pdf-editor.js',
'/js/image-editor.js',
'/manifest.json'
];
// External CDN resources (cached with network-first strategy)
const cdnResources = [
'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js',
'https://unpkg.com/pdf-lib@1.17.1/dist/pdf-lib.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/fabric.js/5.3.0/fabric.min.js'
];
// Install event - cache resources
self.addEventListener('install', (event) => {
console.log('Service Worker: Installing...');
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('Service Worker: Caching files');
return cache.addAll(urlsToCache);
})
.then(() => {
console.log('Service Worker: Installed');
return self.skipWaiting();
})
.catch((error) => {
console.error('Service Worker: Cache failed', error);
})
);
});
// Activate event - clean up old caches
self.addEventListener('activate', (event) => {
console.log('Service Worker: Activating...');
event.waitUntil(
caches.keys()
.then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
console.log('Service Worker: Deleting old cache', cacheName);
return caches.delete(cacheName);
}
})
);
})
.then(() => {
console.log('Service Worker: Activated');
return self.clients.claim();
})
);
});
// Fetch event - serve from cache, fallback to network
self.addEventListener('fetch', (event) => {
const { request } = event;
const url = new URL(request.url);
// Skip cross-origin requests that are not CDN resources
if (url.origin !== location.origin && !cdnResources.some(cdn => request.url.startsWith(cdn))) {
return;
}
// Network-first strategy for CDN resources
if (cdnResources.some(cdn => request.url.startsWith(cdn))) {
event.respondWith(
fetch(request)
.then((response) => {
// Clone the response before caching
const responseClone = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(request, responseClone);
});
return response;
})
.catch(() => {
// Fallback to cache if network fails
return caches.match(request);
})
);
return;
}
// Cache-first strategy for app resources
event.respondWith(
caches.match(request)
.then((response) => {
if (response) {
return response;
}
return fetch(request)
.then((response) => {
// Don't cache if not a success response
if (!response || response.status !== 200 || response.type === 'error') {
return response;
}
// Clone the response before caching
const responseClone = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(request, responseClone);
});
return response;
});
})
.catch((error) => {
console.error('Service Worker: Fetch failed', error);
// Return offline page or error response
if (request.destination === 'document') {
return caches.match('/index.html');
}
})
);
});
// Handle messages from the client
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
if (event.data && event.data.type === 'CACHE_URLS') {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(event.data.urls);
})
);
}
});
// Background sync for future features
self.addEventListener('sync', (event) => {
console.log('Service Worker: Background sync', event.tag);
if (event.tag === 'sync-edits') {
event.waitUntil(
// Handle background sync operations
Promise.resolve()
);
}
});
// Push notification support for future features
self.addEventListener('push', (event) => {
console.log('Service Worker: Push notification received');
const options = {
body: event.data ? event.data.text() : 'New notification',
icon: '/assets/icons/icon-192.png',
badge: '/assets/icons/icon-72.png',
vibrate: [200, 100, 200]
};
event.waitUntil(
self.registration.showNotification('PDF & Image Editor', options)
);
});
// Notification click handler
self.addEventListener('notificationclick', (event) => {
console.log('Service Worker: Notification clicked');
event.notification.close();
event.waitUntil(
clients.openWindow('/')
);
});