ServiceWorker.js 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. const cacheName = "4dage-RacingCar-0.1.0"
  2. const contentToCache = [
  3. "Build/Build.loader.js",
  4. "Build/Build.framework.js.unityweb",
  5. "Build/Build.data.unityweb",
  6. "Build/Build.wasm.unityweb",
  7. "TemplateData/style.css"
  8. ]
  9. self.addEventListener('install', function (e) {
  10. console.log('[Service Worker] Install')
  11. e.waitUntil((async function () {
  12. const cache = await caches.open(cacheName)
  13. console.log('[Service Worker] Caching all: app shell and content')
  14. await cache.addAll(contentToCache)
  15. })())
  16. })
  17. self.addEventListener('fetch', function (e) {
  18. e.respondWith((async function () {
  19. let response = await caches.match(e.request)
  20. console.log(`[Service Worker] Fetching resource: ${e.request.url}`)
  21. if (response) { return response }
  22. response = await fetch(e.request)
  23. const cache = await caches.open(cacheName)
  24. console.log(`[Service Worker] Caching new resource: ${e.request.url}`)
  25. // cache.put(e.request, response.clone())
  26. return response
  27. })())
  28. })