Skip to content
Snippets Groups Projects
service-worker.js 1.49 KiB
Newer Older
  • Learn to ignore specific revisions
  • self.addEventListener("install", (event) => {
      console.info("Service worker installed.", event)
    })
    
    self.addEventListener("activate", (event) => {
      console.info("Service worker activated.", event)
    })
    
    const CACHE_NAME = "APP-V0"
    
    Nayeem Rahman's avatar
    Nayeem Rahman committed
    const FILES_TO_CACHE = ["/index.html", "/js/app.js", "/manifest.json"]
    const FILE_ALIASES = new Map([["/", "/index.html"]])
    
    
    const normalizeUrl = (url) => {
      const url_ = new URL(url)
      url_.pathname = url_.pathname.replace(/\/+/g, "/").replace(/\/$/, "")
      if (FILE_ALIASES.has(url_.pathname)) {
        url_.pathname = FILE_ALIASES.get(url_.pathname)
      }
      return url_.href
    }
    
    
    Nayeem Rahman's avatar
    Nayeem Rahman committed
    self.addEventListener("install", async () => {
    
      const cache = await caches.open(CACHE_NAME)
      const additions = cache.addAll(FILES_TO_CACHE)
      await additions
      console.info(`Files cached: [\n  ${FILES_TO_CACHE.join(`,\n  `)}\n]`)
    })
    
    
    Nayeem Rahman's avatar
    Nayeem Rahman committed
    self.addEventListener("activate", async () => {
    
      const oldCacheKeys = (await caches.keys()).filter((key) => key != CACHE_NAME)
      oldCacheKeys.forEach((key) => caches.delete(key))
    })
    
    self.addEventListener("fetch", (event) => {
      const normalizedUrl = normalizeUrl(event.request.url)
      let response = fetch(event.request)
      if (FILES_TO_CACHE.includes(normalizedUrl)) {
        response = response
          .then(async (response) => {
            const cache = await caches.open(CACHE_NAME)
            await cache.put(normalizedUrl, response.clone())
            return response
          })
          .catch(() => caches.match(normalizedUrl))
    
    Nayeem Rahman's avatar
    Nayeem Rahman committed
          .catch(() => null)
    
    Nayeem Rahman's avatar
    Nayeem Rahman committed
      event.respondWith(response)