mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 17:25:39 +01:00
Add a simple service worker for offline shell
cgen-1bb0676720c6412bade128fbb38b3686
This commit is contained in:
parent
465722ea9a
commit
1066a32f98
1 changed files with 26 additions and 0 deletions
26
website/public/sw.js
Normal file
26
website/public/sw.js
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
const CACHE = 'switch-cache-v1';
|
||||||
|
const ASSETS = [
|
||||||
|
'/',
|
||||||
|
'/index.html',
|
||||||
|
'/playground.html',
|
||||||
|
'/monarch.html',
|
||||||
|
'/switch.html'
|
||||||
|
];
|
||||||
|
self.addEventListener('install', (e) => {
|
||||||
|
e.waitUntil(caches.open(CACHE).then(c=>c.addAll(ASSETS)).then(()=>self.skipWaiting()));
|
||||||
|
});
|
||||||
|
self.addEventListener('activate', (e) => {
|
||||||
|
e.waitUntil(caches.keys().then(keys=>Promise.all(keys.filter(k=>k!==CACHE).map(k=>caches.delete(k)))).then(()=>self.clients.claim()));
|
||||||
|
});
|
||||||
|
self.addEventListener('fetch', (e) => {
|
||||||
|
const url = new URL(e.request.url);
|
||||||
|
if (url.origin === location.origin) {
|
||||||
|
e.respondWith(caches.match(e.request).then(r=> r || fetch(e.request).then(resp=>{
|
||||||
|
if (e.request.method==='GET' && resp.ok && resp.type==='basic') {
|
||||||
|
const clone = resp.clone();
|
||||||
|
caches.open(CACHE).then(c=>c.put(e.request, clone));
|
||||||
|
}
|
||||||
|
return resp;
|
||||||
|
}).catch(()=>caches.match('/index.html'))));
|
||||||
|
}
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue