-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsw.js
More file actions
140 lines (129 loc) · 3.72 KB
/
sw.js
File metadata and controls
140 lines (129 loc) · 3.72 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
// Service Worker for WebUtils PWA
// Updated: 700+ tools version with performance optimizations
const CACHE_NAME = 'webutils-v2';
const OFFLINE_URL = './offline.html';
// Assets to cache immediately on install
const PRECACHE_ASSETS = [
'/',
'/index.html',
'/offline.html',
'/favicon.svg',
'/favicon-32x32.png',
'/favicon-16x16.png',
'/apple-touch-icon.png',
'/manifest.json'
];
// Install event - precache core assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches
.open(CACHE_NAME)
.then((cache) => {
console.log('[SW] Precaching core assets');
return cache.addAll(PRECACHE_ASSETS);
})
.then(() => self.skipWaiting())
);
});
// Activate event - clean old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches
.keys()
.then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => name !== CACHE_NAME)
.map((name) => {
console.log('[SW] Deleting old cache:', name);
return caches.delete(name);
})
);
})
.then(() => self.clients.claim())
);
});
// Fetch event - Network first, fallback to cache
self.addEventListener('fetch', (event) => {
const { request } = event;
const url = new URL(request.url);
// Skip non-GET requests
if (request.method !== 'GET') return;
// Skip cross-origin requests (like fonts from Google)
if (url.origin !== location.origin) {
// For external resources, try network first, then cache
event.respondWith(
fetch(request)
.then((response) => {
// Cache successful responses
if (response.ok) {
const responseClone = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(request, responseClone);
});
}
return response;
})
.catch(() => caches.match(request))
);
return;
}
// For HTML pages - Network first strategy
if (request.headers.get('accept')?.includes('text/html')) {
event.respondWith(
fetch(request)
.then((response) => {
// Cache successful HTML responses
if (response.ok) {
const responseClone = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(request, responseClone);
});
}
return response;
})
.catch(async () => {
// Try cache first
const cachedResponse = await caches.match(request);
if (cachedResponse) return cachedResponse;
// If not in cache, show offline page
return caches.match(OFFLINE_URL);
})
);
return;
}
// For other assets (JS, CSS, images) - Cache first strategy
event.respondWith(
caches.match(request).then((cachedResponse) => {
if (cachedResponse) {
// Return cached response and update cache in background
fetch(request)
.then((response) => {
if (response.ok) {
caches.open(CACHE_NAME).then((cache) => {
cache.put(request, response);
});
}
})
.catch(() => {});
return cachedResponse;
}
// Not in cache, fetch from network
return fetch(request).then((response) => {
if (response.ok) {
const responseClone = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(request, responseClone);
});
}
return response;
});
})
);
});
// Handle messages from clients
self.addEventListener('message', (event) => {
if (event.data === 'skipWaiting') {
self.skipWaiting();
}
});