We have deployed our Spartacus PWA application in the cloud envrionment and the deployed js storefront is working perfectly fine. But, the offline mode is not working. We have some pure angular routing pages (not communication with commerce) and we want to make these pages working in offline mode.
written new custom service worker to show custom offline fallback page
const OFFLINE_VERSION = 1;
const CACHE_NAME = 'offline';
// Customize this with a different URL if needed.
const OFFLINE_URL = '/assets/offline.html';
self.addEventListener('install', (event) => {
event.waitUntil((async () => {
const cache = await caches.open(CACHE_NAME);
// Setting {cache: 'reload'} in the new request will ensure that the response
// isn't fulfilled from the HTTP cache; i.e., it will be from the network.
await cache.add(new Request(OFFLINE_URL, {cache: 'reload'}));
})());
});
self.addEventListener('activate', (event) => {
event.waitUntil((async () => {
// Enable navigation preload if it's supported.
// See https://developers.google.com/web/updates/2017/02/navigation-preload
if ('navigationPreload' in self.registration) {
await self.registration.navigationPreload.enable();
}
})());
// Tell the active service worker to take control of the page immediately.
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
// We only want to call event.respondWith() if this is a navigation request
// for an HTML page.
if (event.request.mode === 'navigate' && event.request.url.indexOf("smart") === -1
&& event.request.url.indexOf("item-details") === -1 && event.request.url.indexOf("ssitemscan") === -1
&& event.request.url.indexOf("shipto-items") === -1) {
event.respondWith((async () => {
try {
// First, try to use the navigation preload response if it's supported.
const preloadResponse = await event.preloadResponse;
if (preloadResponse) {
return preloadResponse;
}
const networkResponse = await fetch(event.request);
return networkResponse;
} catch (error) {
// catch is only triggered if an exception is thrown, which is likely
// due to a network error.
// If fetch() returns a valid HTTP response with a response code in
// the 4xx or 5xx range, the catch() will NOT be called.
console.log('Fetch failed; returning offline page instead.', error);
const cache = await caches.open(CACHE_NAME);
const cachedResponse = await cache.match(OFFLINE_URL);
return cachedResponse;
}
})());
}
// If our if() condition is false, then this fetch handler won't intercept the
// request. If there are any other fetch handlers registered, they will get a
// chance to call event.respondWith(). If no fetch handlers call
// event.respondWith(), the request will be handled by the browser as if there
// were no service worker involvement.
});
// use all the magic of the Angular Service Worker
importScripts('./ngsw-worker.js');
registered in app module ts file
ServiceWorkerModule.register('custom-service-worker.js', {
enabled: environment.production,
// Register the ServiceWorker as soon as the app is stable
// or after 30 seconds (whichever comes first).
registrationStrategy: 'registerImmediately'
}),
ngsw-config.json
{
"index": "/index.html",
"assetGroups": [
{
"name": "app",
"installMode": "prefetch",
"resources": {
"files": [
"/favicon.ico",
"/index.html",
"/manifest.webmanifest",
"/*.css",
"/*.js",
"/assets/offline.html"
],
"urls": [
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css",
"https://cdnjs.cloudflare.com/ajax/libs/prism/1.22.0/themes/prism.css",
"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
]
}
},
{
"name": "assets",
"installMode": "lazy",
"updateMode": "prefetch",
"resources": {
"files": [
"/assets/**",
"/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)"
]
}
}
],
"navigationUrls": [
"/*/*/*/",
"/*/*/*/scanner",
"/*/*/*/smartstockscan",
"/*/*/*/item-details",
"/*/*/*/ssitemscan",
"/*/*/*/shipto-items",
"/*/*/*/edit-smartstock-item",
"/*/*/*/smart-stock-cart",
"/*/*/*/smart-stock-checkout",
"/*/*/*/smart-stock-cart-list"
]
}
The service worker is registered in browser correctly and also js and css are cached.
The custom offline page is working but other angular routes are not working, giving 504 error. Any help would be appreciated very much.