feat: Add service worker for offline install
This commit is contained in:
+1
-1
File diff suppressed because one or more lines are too long
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "Pascar Text Editor",
|
||||
"short_name": "Pascar",
|
||||
"start_url": "index.html",
|
||||
"display": "standalone",
|
||||
"background_color": "#1c1c1c",
|
||||
"theme_color": "#295991",
|
||||
"icons": [
|
||||
{
|
||||
"src": "icon-192.png",
|
||||
"sizes": "192x192",
|
||||
"type": "image/png"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1325,6 +1325,15 @@
|
||||
}
|
||||
});
|
||||
|
||||
// -- Service Worker to make this application work offline
|
||||
if ('serviceWorker' in navigator) {
|
||||
window.addEventListener('load', () => {
|
||||
navigator.serviceWorker.register('sw.js')
|
||||
.then(reg => console.log('Service Worker Registered'))
|
||||
.catch(err => console.log('Registration Failed', err));
|
||||
});
|
||||
}
|
||||
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
const CACHE_NAME = 'pascar';
|
||||
const ASSETS = [
|
||||
'index.html',
|
||||
'manifest.json'
|
||||
];
|
||||
|
||||
// Install: Cache the files
|
||||
self.addEventListener('install', (e) => {
|
||||
e.waitUntil(
|
||||
caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS))
|
||||
);
|
||||
});
|
||||
|
||||
// Fetch: Serve from cache if offline
|
||||
self.addEventListener('fetch', (e) => {
|
||||
e.respondWith(
|
||||
caches.match(e.request).then((response) => {
|
||||
return response || fetch(e.request);
|
||||
})
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user