/**
* @param {String} de-resourceUrl Absolute URL to the application, e.g. https://acme:444/3dspace/
* @param {String} mymodule-version current version for my Application (set in Settings Provider)
*/
var MyModuleSW = (function() {
var STATIC_RESOURCES_ENDINGS =
'\\.(js|css|png|bmp|gif|eot|svg|ttf|woff|woff2|json|handlebars)(\\?.*)?';
var urlParams;
var CACHE_NAMES = {};
/**
* Service Worker initialization
*/
var init = function(searchParams) {
urlParams = searchParams;
CACHE_NAMES = {
MY_MODULE_STATIC:
'mymodule-static-v' + urlParams.get('mymodule-version')
};
};
/**
* Return list of cache names. These will be cleared out when registering a new Service Worker version
*/
var getValidCacheNames = function() {
return CACHE_NAMES.MY_MODULE_STATIC;
};
/**
* List of files along with versions that should be pre-cached, i.e. when service worker installs.
*
*/
var precache = function(baseUrl, workbox) {
// Typically populated by build process
const preCacheList = [
{
url: 'mydomain/scripts/product.js', // should probably be absolute path
revision: 'cdb0ad00d71eff5be6ca7305cff83ce9'
}
];
workbox.precaching.precacheAndRoute(preCacheList);
};
/**
* Register routes for caching content using different workbox strategies
*/
var registerRoutes = function(workbox) {
var baseUrl = urlParams.get('de-resourceUrl');
var myModulePattern = baseUrl + 'mydomain/' + '.*' + STATIC_RESOURCES_ENDINGS + '$';
// Define caching strategy for our routes. For more information, see Workbox JS documentation
workbox.routing.registerRoute(
new RegExp(myModulePattern),
workbox.strategies.cacheFirst({
cacheName: CACHE_NAMES.TVC_STATIC
})
);
};
return {
init: init,
precache: precache,
registerRoutes: registerRoutes,
getValidCacheNames: getValidCacheNames
};
})();
// Register My Module's Service Worker fragment with top level service worker
if (typeof registerSubServiceWorker !== 'undefined') {
registerSubServiceWorker(MyModuleSW);
}