spanish-quizzer

An app to quiz you on Spanish vocabulary and verb conjugations
git clone https://git.ashermorgan.net/spanish-quizzer/
Log | Files | Refs | README

service-worker.js (2515B)


      1 // Initialize constants
      2 const version = "spanish-quizzer-7";
      3 const resources = [
      4     "./css/app.css",
      5     "./css/filtersPage.css",
      6     "./css/global.css",
      7     "./css/quizzer.css",
      8     "./css/reference.css",
      9     "./css/settingsPage.css",
     10     "./data/verbs.csv",
     11     "./data/vocab.csv",
     12     "./images/arrow-left.svg",
     13     "./images/favicon-32.png",
     14     "./images/favicon-180.png",
     15     "./images/favicon-192-maskable.png",
     16     "./images/favicon-192.png",
     17     "./images/favicon-512-maskable.png",
     18     "./images/favicon-512.png",
     19     "./images/plus.svg",
     20     "./images/settings.svg",
     21     "./images/sound.svg",
     22     "./images/trash.svg",
     23     "./images/x.svg",
     24     "./js/app.js",
     25     "./js/filters.js",
     26     "./js/filtersPage.js",
     27     "./js/global.js",
     28     "./js/quizzer.js",
     29     "./js/reference.js",
     30     "./js/settingsPage.js",
     31     "./vendor/diff.js",
     32     "./vendor/data-table.min.css",
     33     "./vendor/data-table.min.js",
     34     "./vendor/papaparse.js",
     35     "./vendor/vue-router.js",
     36     "./vendor/vue.js",
     37     "./index.html",
     38     "./",
     39 ];
     40 
     41 
     42 
     43 self.addEventListener("install", function(event) {
     44     event.waitUntil(async function() {
     45         // Cache resources
     46         const cache = await caches.open(version);
     47         await cache.addAll(resources);
     48     }());
     49 });
     50 
     51 
     52 
     53 self.addEventListener("fetch", function(event) {
     54     // Ignore non-GET requests
     55     if (event.request.method !== "GET") return;
     56 
     57     event.respondWith(async function() {
     58         // Look for cached response
     59         const cache = await caches.open(version);
     60         const cachedResponse = await cache.match(event.request);
     61 
     62         if (cachedResponse) {
     63             // Update cache in the background
     64             event.waitUntil(cache.add(event.request));
     65 
     66             // Returned cached response
     67             return cachedResponse;
     68         }
     69         else {
     70             // Fall back to network
     71             const response = await fetch(event.request);
     72 
     73             // Add response to cache
     74             cache.put(event.request, response.clone());
     75 
     76             // Return response
     77             return response;
     78         }
     79     }());
     80 });
     81 
     82 
     83 
     84 self.addEventListener("activate", function(event) {
     85     event.waitUntil(
     86         // Remove outdated caches
     87         caches.keys().then(function (keys) {
     88             return Promise.all(
     89                 keys.filter(function (key) {
     90                     return key != version;
     91                 })
     92                 .map(function (key) {
     93                     return caches.delete(key);
     94                 })
     95             );
     96         })
     97     );
     98 });