-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
111 lines (101 loc) · 3.96 KB
/
sw.js
File metadata and controls
111 lines (101 loc) · 3.96 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
// Linux Mastery Course - Service Worker
const CACHE_NAME = 'linux-mastery-v1';
const OFFLINE_URL = '/index.html';
// Assets to cache on install
const PRECACHE_ASSETS = [
'/',
'/index.html',
'/site.webmanifest',
'/android-chrome-192x192.png',
'/android-chrome-512x512.png',
'/apple-touch-icon.png',
'/favicon-32x32.png',
'/favicon-16x16.png',
'/favicon.ico',
'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=JetBrains+Mono:wght@400;500;600;700&display=swap',
'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/base16/tomorrow-night.min.css',
'https://cdn.jsdelivr.net/npm/marked/marked.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/bash.min.js'
];
// Install event - cache assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('📦 Caching app assets...');
return cache.addAll(PRECACHE_ASSETS);
})
.then(() => {
console.log('✅ Assets cached successfully');
return self.skipWaiting();
})
.catch((error) => {
console.error('❌ Cache failed:', error);
})
);
});
// 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('🗑️ Deleting old cache:', name);
return caches.delete(name);
})
);
})
.then(() => {
console.log('🚀 Service Worker activated');
return self.clients.claim();
})
);
});
// Fetch event - serve from cache, fallback to network
self.addEventListener('fetch', (event) => {
// Skip non-GET requests
if (event.request.method !== 'GET') return;
// Skip chrome-extension and other non-http requests
if (!event.request.url.startsWith('http')) return;
event.respondWith(
caches.match(event.request)
.then((cachedResponse) => {
if (cachedResponse) {
// Return cached version
return cachedResponse;
}
// Not in cache, fetch from network
return fetch(event.request)
.then((networkResponse) => {
// Check if valid response
if (!networkResponse || networkResponse.status !== 200 || networkResponse.type !== 'basic') {
return networkResponse;
}
// Clone and cache the response
const responseToCache = networkResponse.clone();
caches.open(CACHE_NAME)
.then((cache) => {
cache.put(event.request, responseToCache);
});
return networkResponse;
})
.catch(() => {
// Network failed, try to return offline page for navigation requests
if (event.request.mode === 'navigate') {
return caches.match(OFFLINE_URL);
}
return new Response('Offline', { status: 503, statusText: 'Service Unavailable' });
});
})
);
});
// Handle messages from the client
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});