In a UI5 application, I've registered my service worker in index.html :
$(document).ready(function() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('service-worker.js', { scope: './' })
.then(function(registration) {
console.log("Service Worker Registered");
})
.catch(function(err) {
console.log("Service Worker Failed to Register", err);
})
}
});
Also, I have the file service-worker.js in the root folder so that the service worker can cache all the elements below it. This file contains the following code:
// Set a name for the current cache
var cacheName = 'v2';
// Default files to always cache
var cacheFiles = [
'./',
'./inicio.html?I_APLICACION=CAPCGENA02',
'./resources/sap-ui-core.js',
'./resources/sap/ui/core/themes/base/fonts/SAP-icons.ttf',
'./resources/sap-ui-core.js',
'./resources/sap/ui/core/library-preload.js',
'./resources/sap/m/library-preload.js',
'./resources/sap/ui/unified/library-preload.js',
'./resources/sap/ui/comp/library-preload.js',
'./resources/sap/ui/table/library-preload.js',
'./resources/sap/ui/fl/library-preload.js',
'./resources/sap/tnt/library-preload.js',
'./resources/sap/ui/unified/themes/sap_belize/library-parameters.json',
'./resources/sap/ui/table/themes/sap_belize/library-parameters.json',
'./resources/sap/ui/comp/themes/sap_belize/library-parameters.json',
'./resources/sap/tnt/themes/sap_belize/library-parameters.json',
'./resources/sap/ui/layout/library-preload.js',
'./resources/sap/ui/unified/themes/sap_belize/library.css',
'./resources/sap/ui/table/themes/sap_belize/library.css',
'./resources/sap/ui/comp/themes/sap_belize/library.css',
'./resources/sap/tnt/themes/sap_belize/library.css',
'./css/style.css',
'./css/fonts/ttf/CMEEMEN18.ttf',
'./images/temporal/chart.PNG',
'./Component.js',
'./Component-preload.js',
'./manifest.json',
'./api/api.reformation.data.js',
'./api/ApiManager.js',
'./api/ApiStructure.js',
'./api/ibd.api.login.data.js',
'./controller/Home.controller.js',
'./controller/Login.controller.js',
'./controller/Menu.controller.js',
'./controller/fragment/ManagePartes.js',
'./controller/fragment/FilterDialogController.js',
'./controller/structure/Container.controller.js',
'./controller/structur
'./database/apiBBDD.js',
'./database/bbdd.js',
'./i18n/i18n.properties',
'./i18n/i18n_en.properties',
'./i18n/i18n_es.properties',
'./i18n/i18n_en_US.properties',
'./i18n/i18n_es_ES.properties',
'./manager/dataManager/DataManager.js',
'./manager/functionalityManager/FunctionalityManager.js',
'./manager/mappingManager/MappingManager.js',
'./manager/parserManager/ReformationParser.js',
'./manager/sessionManager/SessionManager.js',
'./manager/structureManager/ElementManager.js',
'./manager/structureManager/StructureManager.js',
'./manager/viewManager/ViewManager.js',
'./singleton/Singleton.js',
'./utils/Consts.js',
'./utils/Funcionality.js',
'./utils/FunctionalityEvent.js',
'./utils/MaskedPassword.js',
'./utils/Utils.js',
'./view/Home.view.xml',
'./view/Login.view.xml',
'./view/Menu.view.xml',
'./view/fragment/DialogConfirm.fragment.xml',
'./view/fragment/DialogEnd.fragment.xml',
'./view/fragment/DialogError.fragment.xml',
'./view/fragment/DialogTag.fragment.xml',
'./view/fragment/DialogWaiting.fragment.xml',
'./view/fragment/HistoryDialog.fragment.xml',
'./view/fragment/HistoryNoticeDialog.fragment.xml',
]
self.addEventListener('install', function(e) {
console.log('[ServiceWorker] Installed');
// e.waitUntil Delays the event until the Promise is resolved
e.waitUntil(
// Open the cache
caches.open(cacheName).then(function(cache) {
// Add all the default files to the cache
console.log('[ServiceWorker] Caching cacheFiles');
//return cache.addAll(cacheFiles);
return cache.addAll(cacheFiles.map(url => new Request(url, {credentials: 'same-origin'})));
})
); // end e.waitUntil
});
self.addEventListener('activate', function(e) {
console.log('[ServiceWorker] Activated');
e.waitUntil(
// Get all the cache keys (cacheName)
caches.keys().then(function(cacheNames) {
return Promise.all(cacheNames.map(function(thisCacheName) {
// If a cached item is saved under a previous cacheName
if (thisCacheName !== cacheName) {
// Delete that cached file
console.log('[ServiceWorker] Removing Cached Files from Cache - ', thisCacheName);
return caches.delete(thisCacheName);
}
}));
})
); // end e.waitUntil
});
self.addEventListener('fetch', function(e) {
console.log('[ServiceWorker] Fetch', e.request.url);
// e.respondWidth Responds to the fetch event
e.respondWith(
// Check in cache for the request being made
caches.match(e.request)
.then(function(response) {
// If the request is in the cache
if ( response ) {
console.log("[ServiceWorker] Found in Cache", e.request.url, response);
// Return the cached version
return response;
}
// If the request is NOT in the cache, fetch and cache
var requestClone = e.request.clone();
fetch(requestClone)
.then(function(response) {
if ( !response ) {
console.log("[ServiceWorker] No response from fetch ")
return response;
}
var responseClone = response.clone();
// Open the cache
caches.open(cacheName).then(function(cache) {
// Put the fetched response in the cache
cache.put(e.request, responseClone);
console.log('[ServiceWorker] New Data Cached', e.request.url);
// Return the response
return response;
}); // end caches.open
})
.catch(function(err) {
console.log('[ServiceWorker] Error Fetching & Caching New Data', err);
});
}) // end caches.match(e.request)
); // end e.respondWith
});
With all this, I get my service worker installed and activated correctly. And if I go to the Cache Storage tab, I have the information cached:
My connection is secure (HTTPS) but when I put in offline mode, the requests come back from cache-disk instead of from service-worker.
I guess it's because it is taking it from the automatic caching of browsers, but if I click on the "Disable cache" option in Chrome, it does not load the offline page.
Does anyone know why this happens?
Here is an answer to "Has anyone worked on Service Worker Implementation in SAPUI5?"
And the quote from MDN :
[Service Worker] is designed to be fully async ; as a consequence, APIs such as synchronous XHR [...] can't be used inside a service worker.
So currently, Service Worker doesn't intercept sync XHRs and UI5 does send many sync XHRs which explains why those files are not intercepted .
Therefore, try to reduce sync XHRs as much as possible as explained in here .
Set everywhere async: true
whenever possible. Eg
For components
sap.ui.component({ manifestUrl: "...", async: true, })
Root View (in app descriptor)
rootView : { viewName: "...", type: "XML", async: true, ... },
Load libraries asynchronously via the bootstrap config preload
with the value "async"
. Eg:
data-sap-ui-preload="async"
Preload libraries with enabled xx-waitForTheme
. Eg:
data-sap-ui-libs="sap.m, sap.ui.core, sap.f" data-sap-ui-xx-waitForTheme="true"
I also see you have some fragments . Avoid loading controls lazily via fragment factory since fragments are fetched synchronously by this way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.