feat: add PWA support - manifest, service worker, offline page, icons
This commit is contained in:
71
public/sw.js
Normal file
71
public/sw.js
Normal file
@@ -0,0 +1,71 @@
|
||||
const CACHE = 'sm-v1';
|
||||
const STATIC_ASSETS = [
|
||||
'/assets/css/style.css',
|
||||
'/assets/js/app.js',
|
||||
'/assets/js/main.js',
|
||||
'/assets/img/server.svg',
|
||||
'/assets/img/icon-192.png',
|
||||
'/assets/img/icon-512.png',
|
||||
'/manifest.json',
|
||||
'/offline',
|
||||
];
|
||||
|
||||
self.addEventListener('install', function(e) {
|
||||
e.waitUntil(
|
||||
caches.open(CACHE).then(function(cache) {
|
||||
return cache.addAll(STATIC_ASSETS);
|
||||
}).then(function() {
|
||||
return self.skipWaiting();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('activate', function(e) {
|
||||
e.waitUntil(
|
||||
caches.keys().then(function(keys) {
|
||||
return Promise.all(
|
||||
keys.filter(function(k) { return k !== CACHE; }).map(function(k) { return caches.delete(k); })
|
||||
);
|
||||
}).then(function() {
|
||||
return self.clients.claim();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', function(e) {
|
||||
var url = new URL(e.request.url);
|
||||
|
||||
if (url.pathname.startsWith('/api/')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
url.pathname.startsWith('/assets/') ||
|
||||
url.pathname === '/manifest.json'
|
||||
) {
|
||||
e.respondWith(
|
||||
caches.open(CACHE).then(function(cache) {
|
||||
return cache.match(e.request).then(function(cached) {
|
||||
var fetchPromise = fetch(e.request).then(function(response) {
|
||||
cache.put(e.request, response.clone());
|
||||
return response;
|
||||
});
|
||||
return cached || fetchPromise;
|
||||
});
|
||||
})
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
e.respondWith(
|
||||
fetch(e.request).catch(function() {
|
||||
return caches.match(e.request).then(function(cached) {
|
||||
if (cached) return cached;
|
||||
if (e.request.mode === 'navigate') {
|
||||
return caches.match('/offline');
|
||||
}
|
||||
return new Response('Offline', { status: 503 });
|
||||
});
|
||||
})
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user