/ src / GradeView / wwwroot / blazorServiceWorker.js
blazorServiceWorker.js
  1  const filesToCache = [
  2      // Blazor standard requirements
  3      '_framework/_bin/Microsoft.AspNetCore.Authorization.dll',
  4      '_framework/_bin/Microsoft.AspNetCore.Blazor.dll',
  5      '_framework/_bin/Microsoft.AspNetCore.Components.dll',
  6      '_framework/_bin/Microsoft.AspNetCore.Components.Browser.dll',
  7   
  8  
  9      '_framework/_bin/Microsoft.AspNetCore.Metadata.dll',
 10      '_framework/_bin/Microsoft.Bcl.AsyncInterfaces.dll',
 11      '_framework/_bin/Microsoft.Extensions.DependencyInjection.Abstractions.dll',
 12      '_framework/_bin/Microsoft.Extensions.DependencyInjection.dll',
 13  
 14      '_framework/_bin/Microsoft.Extensions.Logging.Abstractions.dll',
 15      '_framework/_bin/Microsoft.Extensions.Options.dll',
 16      '_framework/_bin/Microsoft.Extensions.Primitives.dll',
 17      '_framework/_bin/Microsoft.JSInterop.dll',
 18      '_framework/_bin/mscorlib.dll',
 19      '_framework/_bin/Mono.Security.dll',
 20      '_framework/_bin/Mono.WebAssembly.Interop.dll',
 21      '_framework/_bin/System.Buffers.dll',
 22      '_framework/_bin/System.ComponentModel.Annotations.dll',
 23      '_framework/_bin/System.Core.dll',
 24      '_framework/_bin/System.dll',
 25      '_framework/_bin/System.Memory.dll',
 26      '_framework/_bin/System.Net.Http.dll',
 27      '_framework/_bin/System.Numerics.Vectors.dll',
 28      '_framework/_bin/System.Runtime.CompilerServices.Unsafe.dll',
 29      '_framework/_bin/System.Text.Json.dll',
 30      '_framework/_bin/System.Threading.Tasks.Extensions.dll',
 31  
 32      //'/_framework/_bin/Microsoft.AspNetCore.Blazor.dll',
 33      //'/_framework/_bin/Microsoft.AspNetCore.Blazor.dll',
 34      //'/_framework/_bin/Microsoft.AspNetCore.Blazor.dll',
 35      //'/_framework/_bin/Microsoft.AspNetCore.Blazor.dll',
 36      //'/_framework/_bin/Microsoft.AspNetCore.Blazor.dll',
 37      //'/_framework/_bin/Microsoft.AspNetCore.Blazor.dll',
 38      //'/_framework/_bin/Microsoft.AspNetCore.Blazor.dll',
 39      //'/_framework/_bin/Microsoft.AspNetCore.Blazor.dll',
 40      //'/_framework/_bin/Microsoft.AspNetCore.Blazor.dll',
 41  
 42  
 43  
 44      //'/_framework/_bin/Microsoft.AspNetCore.Blazor.TagHelperWorkaround.dll',
 45  
 46  
 47  
 48  
 49  
 50  
 51      '_framework/wasm/mono.js',
 52      '_framework/wasm/mono.wasm',
 53      '_framework/blazor.boot.json',
 54      '_framework/blazor.server.js',
 55      '_framework/blazor.webassembly.js',
 56       
 57      // App specific requirements
 58      '_framework/_bin/GradeView.dll',
 59      //'/_framework/_bin/osisa.Smart.Webapp.pdb',
 60      'css/bootstrap/bootstrap.min.css',
 61      'css/open-iconic/font/css/open-iconic-bootstrap.min.css',
 62      'css/site.css',
 63      '/favicon.ico',
 64      '/icons/icon-192-192.png',
 65      '/icons/icon-512-512.png',
 66      'index.html',
 67   
 68      // Service Worker
 69      'blazorSWRegister.js',
 70   
 71      // Application Manifest (PWA)
 72      'manifest.json'
 73  ];
 74   
 75  const staticCacheName = 'blazor-cache-v3';
 76  
 77  self.addEventListener('install', event => {
 78      self.skipWaiting();
 79      event.waitUntil(
 80          caches.open(staticCacheName)
 81              .then(cache => {
 82                  return cache.addAll(filesToCache);
 83              })
 84      );
 85  });
 86  
 87  self.addEventListener('fetch', event => {
 88      var requestUrl = new URL(event.request.url);
 89   
 90      // First, handle requests for the root path - server up index.html
 91      if (requestUrl.origin === location.origin) {
 92          if (requestUrl.pathname === '/') {
 93              event.respondWith(caches.match('/index.html'));
 94              return;
 95          }
 96      }
 97      // Anything else
 98      event.respondWith(
 99          // Check the cache
100          caches.match(event.request)
101              .then(response => {
102                  // anything found in the cache can be returned from there
103                  // without passing it on to the network
104                  if (response) {
105                      console.log('Found ', event.request.url, ' in cache');
106                      return response;
107                  }
108                  // otherwise make a network request
109                  return fetch(event.request)
110                      .then(response => {
111                          // if we got a valid response 
112                          if (response.ok) {
113                              // and the request was for something rfom our own app url
114                              // we should add it to the cache
115                              if (requestUrl.origin === location.origin) {
116   
117                                  const pathname = requestUrl.pathname;
118                                  console.log("CACHE: Adding " + pathname);
119                                  return caches.open(staticCacheName).then(cache => {
120                                      // you can only "read" a response once, 
121                                      // but you can clone it and use that for the cache
122                                      cache.put(event.request.url, response.clone());
123                                  });
124                              }
125                          }
126                          return response;
127                      });
128              }).catch(error => {
129                  // handle this error - for now just log it
130                  console.log(error);
131              })
132      );
133  });