
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js')
.then(function(registration) {
// Registration was successful
console.log('[success] scope: ', registration.scope);
}, function(err) {
// registration failed 😞
console.log('[fail]: ', err);
});
}
</script>importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.0.0/workbox-sw.js');
// when we use CacheFirst strategy, the file is loaded from network if it is not there in cache and is // then fetched from cache for every subsequent GET requests.
// NetworkFirst strategy will always get it from the network.
// cache javascript type files
workbox.routing.registerRoute(
new RegExp('.*\.js'),
new workbox.strategies.CacheFirst({
cacheName: "jsCache",
plugins: [
new workbox.expiration.Plugin({
maxEntries: 30,
// Cache for a maximum of 3 days
maxAgeSeconds: 3 * 24 * 60 * 60
})
]
})
);
workbox.routing.registerRoute(
new RegExp('.*\.properties'),
new workbox.strategies.CacheFirst({
cacheName: "msgPropCache",
plugins: [
new workbox.expiration.Plugin({
// Cache only 60 images
maxEntries: 60,
// Cache for a maximum of 3 days
maxAgeSeconds: 3 * 24 * 60 * 60
})
]
})
);



importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.0.0/workbox-sw.js');
// when we use CacheFirst strategy, the file is loaded from network if it is not there in cache and is // then fetched from cache for every subsequent GET requests.
// NetworkFirst strategy will always get it from the network.
var cacheId = "development-cache";
// var cacheId = "1.0.1"; //app release version
var jsCache="jsCache-" + cacheId;
var msgPropCache="msgPropCache-" + cacheId;
var cacheList = [jsCache, msgPropCache];
// cache javascript type files
workbox.routing.registerRoute(
new RegExp('.*\.js'),
new workbox.strategies.CacheFirst({
cacheName: jsCache,
plugins: [
new workbox.expiration.Plugin({
maxEntries: 30,
// Cache for a maximum of 3 days
maxAgeSeconds: 3 * 24 * 60 * 60
})
]
})
);
workbox.routing.registerRoute(
new RegExp('.*\.properties'),
new workbox.strategies.CacheFirst({
cacheName: msgPropCache,
plugins: [
new workbox.expiration.Plugin({
// Cache only 60 images
maxEntries: 60,
// Cache for a maximum of 3 days
maxAgeSeconds: 3 * 24 * 60 * 60
})
]
})
);
self.addEventListener('install', function(event) {
self.skipWaiting();
});
// Call Activate Event to remove old cache
self.addEventListener('activate', function(e) {
// Remove unwanted caches
e.waitUntil(
caches.keys().then(function(cacheNames){
return Promise.all(
cacheNames.map(function(cache) {
// If cache Id is set to 'development-cache', it is deleted with every reload, when 'update on reload' is checked in Service Workers, under the 'Application' tab in debugger console
if(cacheId.indexOf("development-cache") > -1 || cacheList.indexOf(cache) === -1) {
console.log("cache deleted");
return caches.delete(cache);
}
})
);
})
);
});module.exports = function(grunt) {
"use strict";
grunt.loadNpmTasks("@sap/grunt-sapui5-bestpractice-build");
grunt.loadNpmTasks("@sap/grunt-sapui5-bestpractice-test");
grunt.registerTask('cache-version', function(key, value) {
// reads the service worker file from dist folder and updates the version of cache and writes it back.
var serviceWorkerFilePath = "dist/sw.js";
if (!grunt.file.exists(serviceWorkerFilePath)) {
grunt.log.error("file " + serviceWorkerFilePath + " not found");
return true;
}
var file = grunt.file.read(serviceWorkerFilePath);
var result = file.replace("development-cache", Date.now() + "");
grunt.file.write("dist/sw.js", result);
});
grunt.registerTask("default", [
"clean",
"lint",
"build",
"cache-version"
]);
};{
"name": "sw-cache",
"version": "0.0.1",
"description": "",
"private": true,
"devDependencies": {
"@sap/grunt-sapui5-bestpractice-build": "1.3.64",
"@sap/grunt-sapui5-bestpractice-test": "^2.0.1",
"eslint": "^5.16.0",
"grunt": "^1.0.4",
"vscode-uri": "1.0.6"
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 48 | |
| 47 | |
| 37 | |
| 32 | |
| 29 | |
| 23 | |
| 22 | |
| 22 | |
| 22 | |
| 21 |