1

I am working on a PWA (with very basic vanilla JS) and i try to make a unity webGL launch in it even when the user is offline. I have put all of my files on cache but it still does not work. I'm a begginer in this so i'll be very thankfull for any help on this. Here is the content of my service-worker file :

const cacheName = "static"
const appFiles = [
    "./",
    "xmlhttprequest-length-computable.min.js?v=1.5.1.22060315",
    "images/favicon.ico",
    "images/icon.png",
    "style.css",
    "responsiveMinimalTemplateStyles.css",
    "responsiveMinimalTemplateLoader.js",
    "manifest.json",
    "Build/WebGL.loader.js",
    "Build/WebGL.framework.js",
    "Build/WebGL.data",
    "Build/WebGL.wasm"
]
self.addEventListener("install", e => {
    console.log('Install')
    e.waitUntil(
        caches.open(cacheName).then(cache => {
            return cache.addAll(appFiles)
        })
    )
})

self.addEventListener("fetch", e => {
    e.respondWith(
        caches.match(e.request).then(response => {
            return response || fetch(e.request)
        })
    )
}) 

Any ideas ?

Kass
  • 11
  • 1

1 Answers1

0

Ok i'm new to this so i'll be answering myself here, if it can help anybody another time. Finally found my answer here (and of course had to adapt it to my own code) : Progressive web app Uncaught (in promise) TypeError: Failed to fetch

Had to complete my self.addEventListener("fetch", request :

self.addEventListener("fetch", e => {
 e.respondWith(
     caches.match(e.request).then(response => {
         if(response){
             console.log('[Service Worker] Récupération de la ressource: '+e.request.url);
             return response
         } else {
             return fetch(e.request).then((response) => {
                 return caches.open(cacheName).then((cache) => {
                     console.log('[Service Worker] Mise en cache de la nouvelle ressource: '+e.request.url)
                     cache.put(e.request, response.clone())
                     return response
                 })
             })
         }
     })
 )
})      
Kass
  • 11
  • 1