public async Task AppendToServiceWorkerScript(StringBuilder sw, PwaOptions options, HttpContext context) { var cacheSuffix = await _workboxCacheSuffixProvider.GetWorkboxCacheSuffix(); //if (context.User.Identity.IsAuthenticated) //{ // sw.Append("workbox.core.setCacheNameDetails({"); // sw.Append("prefix: 'web-app-auth',"); // sw.Append("suffix: '" + cacheSuffix + "',"); // sw.Append("precache: 'auth-user-precache',"); // sw.Append("runtime: 'auth-user-runtime-cache'"); // sw.Append("});"); //} //else //{ // sw.Append("workbox.core.setCacheNameDetails({"); // sw.Append("prefix: 'web-app-anon',"); // sw.Append("suffix: '" + cacheSuffix + "',"); // sw.Append("precache: 'unauth-user-precache',"); // sw.Append("runtime: 'unauth-user-runtime-cache'"); // sw.Append("});"); //} //no longer need separate cahce for auth vs anonymous sw.Append("workbox.core.setCacheNameDetails({"); sw.Append("prefix: 'web-app',"); sw.Append("suffix: '" + cacheSuffix + "',"); sw.Append("precache: 'precache',"); sw.Append("runtime: 'runtime-cache'"); sw.Append("});"); //https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.core //Force a service worker to become active, instead of waiting. This is normally used in conjunction with clientsClaim() sw.Append("workbox.core.skipWaiting();"); sw.Append("workbox.core.clientsClaim();"); //https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.core //Force a service worker to become active, instead of waiting. This is normally used in conjunction with clientsClaim() //sw.Append("workbox.skipWaiting();"); //sw.Append("workbox.clientsClaim();"); //https://github.com/GoogleChrome/workbox/issues/1407 //cleanup old caches sw.Append("let currentCacheNames = Object.assign("); sw.Append("{ precacheTemp: workbox.core.cacheNames.precache + \"-temp\" },"); sw.Append("workbox.core.cacheNames"); sw.Append(");"); sw.Append("self.addEventListener(\"activate\", function(event) {"); sw.Append("event.waitUntil("); sw.Append("caches.keys().then(function(cacheNames) {"); sw.Append("let validCacheSet = new Set(Object.values(currentCacheNames));"); sw.Append("return Promise.all("); sw.Append("cacheNames"); sw.Append(".filter(function(cacheName) {"); sw.Append("return !validCacheSet.has(cacheName);"); sw.Append("})"); sw.Append(".map(function(cacheName) {"); sw.Append("return caches.delete(cacheName);"); sw.Append("})"); sw.Append(");"); sw.Append("})"); sw.Append(");"); if (options.EnableServiceWorkerConsoleLog) { sw.Append("console.log('activate event fired');"); } sw.Append("});"); //new in 4.0.x sw.Append("workbox.precaching.cleanupOutdatedCaches();"); }
public async Task AppendToServiceWorkerScript(StringBuilder sw, PwaOptions options, HttpContext context) { var cacheSuffix = await _workboxCacheSuffixProvider.GetWorkboxCacheSuffix(); sw.Append("const networkFirstHandler = new workbox.strategies.NetworkFirst({"); //sw.Append("cacheName: 'network-first-content-cache-" + cacheSuffix + "',"); //sw.Append("plugins: ["); //sw.Append("new workbox.expiration.Plugin({"); //sw.Append(" maxEntries: 2000,"); //sw.Append("maxAgeSeconds: 30 * 24 * 60 * 60,"); //30 days //sw.Append("})"); //sw.Append("]"); sw.Append("}); "); sw.Append("const networkFirstMatchFunction = ({url, event}) => {"); sw.Append("if(url.href.indexOf(\"serviceworkerinit\") > -1) {"); sw.Append("return false;"); sw.Append("}"); if (options.EnableServiceWorkerConsoleLog) { sw.Append("console.log('networkFirstMatchFunction returning true for ' + url.href);"); } // Return true if the route should match, we are basically matching all requests except to the initserviceworker script sw.Append("return true;"); sw.Append("};"); sw.Append("workbox.routing.registerRoute("); sw.Append("networkFirstMatchFunction,"); sw.Append("new workbox.strategies.NetworkFirst()"); sw.Append(");"); // var offlineUrl = _offlinePageUrlProvider.GetOfflineUrl(); //sw.Append("workbox.routing.registerRoute(networkFirstMatchFunction, args => {"); //sw.Append("return networkFirstHandler.handle(args).then(response => {"); //sw.Append("if (!response) {"); //sw.Append("const precacheCacheName = workbox.core.cacheNames.precache;"); //sw.Append("caches.open(precacheCacheName).then(function(cache) {"); //sw.Append("cache.match(event.request).then(function(response) {"); //sw.Append("return response;"); //sw.Append("}).catch(function() {"); //sw.Append("return caches.match('" + offlineUrl + "');"); //sw.Append("}) "); //sw.Append("})");//end cache open ////sw.Append("caches.match(event.request).then(function(response) {"); ////sw.Append("return response;"); ////sw.Append("}).catch(function() {"); ////sw.Append("return caches.match('" + offlineUrl + "');"); ////sw.Append("}) "); //sw.Append("}"); //end if not response ////sw.Append("console.log('network first returning response');"); ////sw.Append("console.log(response.url);"); //sw.Append("return response;"); //sw.Append("});"); //sw.Append("});"); }