-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsw.js
More file actions
137 lines (128 loc) · 4.15 KB
/
sw.js
File metadata and controls
137 lines (128 loc) · 4.15 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
const CACHE_NAME = 'web-workshop-v1.2';
const urlsToCache = [
'/web_workshop/',
'/web_workshop/index.html',
'/web_workshop/manifest.json',
'/web_workshop/resources/resource-manifest.json',
'/web_workshop/resources/css/styles.css',
'/web_workshop/resources/js/main.js',
'/web_workshop/resources/js/codemirror-bundle.js'
];
// Function to discover and cache all files in directories
async function cacheDirectoryFiles(cache, directories) {
for (const dir of directories) {
try {
// Try to fetch directory listing
const response = await fetch(dir);
if (response.ok) {
const html = await response.text();
// Extract file links from directory listing
const links = html.match(/href="([^"]*\.(png|jpg|jpeg|gif|svg|webp|html|css|js|json|txt|md))"/gi);
if (links) {
const files = links.map(link => {
const match = link.match(/href="([^"]*)"/);
return match ? dir + match[1] : null;
}).filter(Boolean);
// Cache each file
for (const file of files) {
try {
await cache.add(file);
} catch (e) {
console.log(`Failed to cache ${file}:`, e);
}
}
}
}
} catch (e) {
console.log(`Failed to cache directory ${dir}:`, e);
}
}
}
// Function to cache all resources using the generated manifest
async function cacheResources(cache) {
try {
const response = await fetch('/web_workshop/resources/resource-manifest.json');
if (response.ok) {
const manifest = await response.json();
console.log(`Caching ${manifest.images.length} images and ${manifest.resources.length} resources from manifest`);
// Cache images
for (const imagePath of manifest.images) {
try {
await cache.add(imagePath);
console.log(`Cached image: ${imagePath}`);
} catch (e) {
console.log(`Failed to cache image ${imagePath}:`, e);
}
}
// Cache resources
for (const resourcePath of manifest.resources) {
try {
await cache.add(resourcePath);
console.log(`Cached resource: ${resourcePath}`);
} catch (e) {
console.log(`Failed to cache resource ${resourcePath}:`, e);
}
}
} else {
console.log('No resource manifest found, skipping resource caching');
}
} catch (e) {
console.log('Failed to load resource manifest:', e);
}
}
// Install service worker and cache resources
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(async cache => {
// Cache all core files
await cache.addAll(urlsToCache);
// Cache all resources programmatically
await cacheResources(cache);
})
.then(() => self.skipWaiting())
);
});
// Activate service worker and clean up old caches
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
}).then(() => self.clients.claim())
);
});
// Fetch strategy: Network first, then cache (for updates when online)
self.addEventListener('fetch', event => {
event.respondWith(
fetch(event.request)
.then(response => {
// If we got a response, add it to the cache
if (response.status === 200) {
const responseClone = response.clone();
caches.open(CACHE_NAME)
.then(cache => cache.put(event.request, responseClone));
}
return response;
})
.catch(() => {
// Network failed, try cache
return caches.match(event.request)
.then(response => {
if (response) {
return response;
}
// If not in cache and it's a navigation request, serve index.html
if (event.request.mode === 'navigate') {
return caches.match('/index.html');
}
throw new Error('No cached version available');
});
})
);
});