-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
107 lines (102 loc) · 3.52 KB
/
sw.js
File metadata and controls
107 lines (102 loc) · 3.52 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
// CZEditor Service Worker — Enables PWA install + offline caching
const CACHE_NAME = 'czeditor-v2.4.0';
const ASSETS = [
'./',
'./index.html',
'./style.css',
'./themes/dark.css',
'./themes/light.css',
'./script.js',
'./engine.js',
'./editor-ui.js',
'./editor-view.js',
'./editor-view.css',
'./file-icons.css',
'./editor-model.js',
'./editor-features.js',
'./filesystem.js',
'./i18n.js',
'./manifest.json',
'./icon-192.png',
'./icon-512.png',
// i18n translations
'./i18n/en.json',
'./i18n/id.json',
// Language registry (individual lang files are cached dynamically)
'./lang/registry.json',
// Fonts
'./font/config.json',
'./font/MapleMono-Regular.ttf.woff2',
'./font/MapleMono-Italic.ttf.woff2',
'./font/MapleMono-Thin.ttf.woff2',
'./font/MapleMono-ThinItalic.ttf.woff2',
'./font/MapleMono-ExtraLight.ttf.woff2',
'./font/MapleMono-ExtraLightItalic.ttf.woff2',
'./font/MapleMono-Light.ttf.woff2',
'./font/MapleMono-LightItalic.ttf.woff2',
'./font/MapleMono-Medium.ttf.woff2',
'./font/MapleMono-MediumItalic.ttf.woff2',
'./font/MapleMono-SemiBold.ttf.woff2',
'./font/MapleMono-SemiBoldItalic.ttf.woff2',
'./font/MapleMono-Bold.ttf.woff2',
'./font/MapleMono-BoldItalic.ttf.woff2',
'./font/MapleMono-ExtraBold.ttf.woff2',
'./font/MapleMono-ExtraBoldItalic.ttf.woff2'
];
// Install: cache core assets + dynamically cache all lang files from registry
self.addEventListener('install', e => {
e.waitUntil(
caches.open(CACHE_NAME).then(async cache => {
await cache.addAll(ASSETS);
// Dynamically cache all language config files from registry
try {
const resp = await fetch('./lang/registry.json');
if (resp.ok) {
const registry = await resp.json();
const langUrls = registry.map(lang => `./lang/${lang.id}.json`);
await cache.addAll(langUrls);
}
} catch (e) { /* registry fetch failed, lang files will be cached on demand */ }
})
);
self.skipWaiting();
});
// Activate: clean old caches
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k)))
)
);
self.clients.claim();
});
// Fetch: network-first for dynamic, cache-first for static assets
self.addEventListener('fetch', e => {
const url = new URL(e.request.url);
// manifest.json: always from network (never cache)
if (url.pathname.endsWith('manifest.json')) {
e.respondWith(fetch(e.request));
return;
}
// Lang configs, fonts, and icons: cache-first
if (url.pathname.includes('/lang/') || url.pathname.includes('/font/') || url.pathname.includes('/icons/')) {
e.respondWith(
caches.match(e.request).then(cached =>
cached || fetch(e.request).then(resp => {
const clone = resp.clone();
caches.open(CACHE_NAME).then(c => c.put(e.request, clone));
return resp;
})
)
);
return;
}
// Core assets: network-first with cache fallback
e.respondWith(
fetch(e.request).then(resp => {
const clone = resp.clone();
caches.open(CACHE_NAME).then(c => c.put(e.request, clone));
return resp;
}).catch(() => caches.match(e.request))
);
});