[FEAT] Re-enable basic PWA support without customisation options

This commit is contained in:
2023-02-01 23:29:43 +00:00
parent 50f5363b69
commit d66e0ea21a
3 changed files with 83 additions and 27 deletions

View File

@@ -1,24 +1,61 @@
var staticCacheName = "djangopwa-v1";
var staticCacheName = "django-pwa-v" + new Date().getTime();
var filesToCache = [
'/offline',
'/css/django-pwa-app.css',
'/media/icon-72x72.png',
'/media/icon-96x96.png',
'/media/icon-128x128.png',
'/media/icon-144x144.png',
'/media/icon-152x152.png',
'/media/icon-192x192.png',
'/media/icon-384x384.png',
'/media/icon-512x512.png',
'/static/media/splash-640x1136.png',
'/static/media/splash-750x1334.png',
'/static/media/splash-1242x2208.png',
'/static/media/splash-1125x2436.png',
'/static/media/splash-828x1792.png',
'/static/media/splash-1242x2688.png',
'/static/media/splash-1536x2048.png',
'/static/media/splash-1668x2224.png',
'/static/media/splash-1668x2388.png',
'/static/media/splash-2048x2732.png'
];
self.addEventListener("install", function (event) {
event.waitUntil(
caches.open(staticCacheName).then(function (cache) {
return cache.addAll([""]);
})
);
// Cache on install
self.addEventListener("install", event => {
this.skipWaiting();
event.waitUntil(
caches.open(staticCacheName)
.then(cache => {
return cache.addAll(filesToCache);
})
)
});
self.addEventListener("fetch", function (event) {
var requestUrl = new URL(event.request.url);
if (requestUrl.origin === location.origin) {
if (requestUrl.pathname === "/") {
event.respondWith(caches.match(""));
return;
}
}
event.respondWith(
caches.match(event.request).then(function (response) {
return response || fetch(event.request);
})
);
// Clear cache on activate
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames
.filter(cacheName => (cacheName.startsWith("django-pwa-")))
.filter(cacheName => (cacheName !== staticCacheName))
.map(cacheName => caches.delete(cacheName))
);
})
);
});
// Serve from Cache
self.addEventListener("fetch", event => {
event.respondWith(
caches.match(event.request)
.then(response => {
return response || fetch(event.request);
})
.catch(() => {
return caches.match('offline');
})
)
});