silverstripe-progressivewebapp/client/src/js/types/sw.js

134 lines
4.0 KiB
JavaScript
Raw Normal View History

2020-04-02 01:41:49 +02:00
// caches polyfill because it is not added to native yet!
2020-11-28 12:38:07 +01:00
var log = require('../lib/log');
2022-05-21 23:42:33 +02:00
var caches = require('../../thirdparty/serviceworker-caches');
2020-04-02 01:41:49 +02:00
2020-04-02 02:07:24 +02:00
if (debug) {
log('SW: debug is on');
log(`SW: CACHE_NAME: ${CACHE_NAME}`);
log(`SW: appDomain: ${appDomain}`);
log(`SW: lang: ${lang}`);
}
2020-04-02 01:41:49 +02:00
if (typeof self.CACHE_NAME !== 'string') {
throw new Error('Cache Name cannot be empty');
}
self.addEventListener('fetch', (event) => {
2020-04-02 21:44:18 +02:00
// skip non-get
if (event.request.method !== 'GET') {
return;
}
//Parse the url
const requestURL = new URL(event.request.url);
//Check for our own urls
/*if (requestURL.origin !== location.origin) {
log('SW: skip external ' + event.request.url);
return;
}*/
2024-03-11 19:08:26 +01:00
// skip captchas
if (
requestURL.pathname.indexOf('turnstile') >= 0
){
log(`SW: skip captcha ${event.request.url}`);
}
2020-04-02 21:44:18 +02:00
//Skip admin url's
if (
requestURL.pathname.indexOf('admin') >= 0 ||
requestURL.pathname.indexOf('Security') >= 0 ||
requestURL.pathname.indexOf('dev') >= 0
) {
2020-11-28 12:38:07 +01:00
log(`SW: skip admin ${event.request.url}`);
2020-04-02 21:44:18 +02:00
return;
}
//Test for images
/*if (/\.(jpg|jpeg|png|gif|webp)$/.test(requestURL.pathname)) {
log('SW: skip image ' + event.request.url);
//For now we skip images but change this later to maybe some caching and/or an offline fallback
return;
}*/
2020-04-02 01:41:49 +02:00
// Clone the request for fetch and cache
// A request is a stream and can be consumed only once.
2020-04-02 21:44:18 +02:00
const fetchRequest = event.request.clone(),
2020-04-02 01:41:49 +02:00
cacheRequest = event.request.clone();
// Respond with content from fetch or cache
event.respondWith(
// Try fetch
fetch(fetchRequest)
// when fetch is successful, we update the cache
.then((response) => {
// A response is a stream and can be consumed only once.
// Because we want the browser to consume the response,
// as well as cache to consume the response, we need to
// clone it so we have 2 streams
2020-04-02 21:44:18 +02:00
const responseToCache = response.clone();
2020-04-02 01:41:49 +02:00
// and update the cache
caches.open(self.CACHE_NAME).then((cache) => {
// Clone the request again to use it
// as the key for our cache
2020-04-02 21:44:18 +02:00
const cacheSaveRequest = event.request.clone();
2020-04-02 01:41:49 +02:00
cache.put(cacheSaveRequest, responseToCache);
});
// Return the response stream to be consumed by browser
return response;
})
// when fetch times out or fails
.catch((err) => {
2020-04-02 02:07:24 +02:00
log('SW: fetch failed');
2020-04-02 01:41:49 +02:00
// Return the promise which
// resolves on a match in cache for the current request
// or rejects if no matches are found
return caches.match(cacheRequest);
}),
);
});
// Now we need to clean up resources in the previous versions
// of Service Worker scripts
self.addEventListener('activate', (event) => {
2020-04-02 02:07:24 +02:00
log(`SW: activated: ${version}`);
2020-04-02 01:41:49 +02:00
// Destroy the cache
event.waitUntil(caches.delete(self.CACHE_NAME));
});
2020-04-02 02:07:24 +02:00
2022-05-21 23:42:33 +02:00
self.addEventListener('fetch', (event) => {
const request = event.request;
event.respondWith(
caches.match(request)
.then((response) => {
return response || fetch(request)
.then((response) => {
return response;
})
.catch(() => {
// Offline fallback image
if (request.url.match(/\.(jpe?g|png|gif|svg)$/)) {
return new Response(
'<svg role="img" aria-labelledby="offline-title" viewBox="0 0 400 225" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice"><title id="offline-title">Offline</title><path fill="rgba(145,145,145,0.5)" d="M0 0h400v225H0z" /><text fill="rgba(0,0,0,0.33)" font-family="Helvetica Neue,Arial,sans-serif" font-size="27" text-anchor="middle" x="200" y="113" dominant-baseline="central">offline</text></svg>',
{
headers: {
'Content-Type': 'image/svg+xml',
},
}
);
}
});
})
);
});
2020-04-02 02:07:24 +02:00
self.addEventListener('install', (e) => {
log(`SW: installing version: ${version}`);
});