010101 Thanks
Awesome work 💯
Some Suggestion's
- caching front-end & ignore caching for admin/Api
- Offline Page - Create a Static page using Flarum static page extension - https://github.com/FriendsOfFlarum/pages (disallow this page on search engine via robots.txt)
PS: it's just an idea
Example
Taken from WordPress Superpwa plugin - https://github.com/SuperPWA/Super-Progressive-Web-Apps
const cacheName = 'forum.example.com-superpwa';
const startPage = 'https://forum.example.com';
const offlinePage = 'https://forum.example.com/offline';
const filesToCache = [startPage, offlinePage];
const neverCacheUrls = [/\/admin#/,/\/api/];
// Install
self.addEventListener('install', function(e) {
console.log('SuperPWA service worker installation');
e.waitUntil(
caches.open(cacheName).then(function(cache) {
console.log('SuperPWA service worker caching dependencies');
filesToCache.map(function(url) {
return cache.add(url).catch(function (reason) {
return console.log('SuperPWA: ' + String(reason) + ' ' + url);
});
});
})
);
});
// Activate
self.addEventListener('activate', function(e) {
console.log('SuperPWA service worker activation');
e.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if ( key !== cacheName ) {
console.log('SuperPWA old cache removed', key);
return caches.delete(key);
}
}));
})
);
return self.clients.claim();
});
// Fetch
self.addEventListener('fetch', function(e) {
// Return if the current request url is in the never cache list
if ( ! neverCacheUrls.every(checkNeverCacheList, e.request.url) ) {
console.log( 'SuperPWA: Current request is excluded from cache.' );
return;
}
// Return if request url protocal isn't http or https
if ( ! e.request.url.match(/^(http|https):\/\//i) )
return;
// Return if request url is from an external domain.
if ( new URL(e.request.url).origin !== location.origin )
return;
// For POST requests, do not use the cache. Serve offline page if offline.
if ( e.request.method !== 'GET' ) {
e.respondWith(
fetch(e.request).catch( function() {
return caches.match(offlinePage);
})
);
return;
}
// Revving strategy
if ( e.request.mode === 'navigate' && navigator.onLine ) {
e.respondWith(
fetch(e.request).then(function(response) {
return caches.open(cacheName).then(function(cache) {
cache.put(e.request, response.clone());
return response;
});
})
);
return;
}
e.respondWith(
caches.match(e.request).then(function(response) {
return response || fetch(e.request).then(function(response) {
return caches.open(cacheName).then(function(cache) {
cache.put(e.request, response.clone());
return response;
});
});
}).catch(function() {
return caches.match(offlinePage);
})
);
});
// Check if current url is in the neverCacheUrls list
function checkNeverCacheList(url) {
if ( this.match(url) ) {
return false;
}
return true;
}
```