2

I have a problem with getting service worker to work
I tried a lot of solutions and nothing worked
What i'm trying to do is to cache the files for offline use
BTW this is the first time i work with service workers

main.js:

if ('serviceWorker' in navigator) {
   navigator.serviceWorker
   .register('/sw.js')
   .catch(function(err) {
   console.error(err);
});
}

sw.js:

const files = [
  '/',
  '/index.html',
  '/restaurant.html',
  '/css/styles.css',
  '/js/dbhelper.js',
  '/js/main.js',
  '/js/restaurant_info.js',
  '/data/restaurants.json',
  '/img/1.jpg',
  '/img/2.jpg',
  '/img/3.jpg',
  '/img/4.jpg',
  '/img/5.jpg',
  '/img/6.jpg',
  '/img/7.jpg',
  '/img/8.jpg',
  '/img/9.jpg',
  '/img/10.jpg'
];

self.addEventListener('install', function(e) {
  e.waitUntil(
      caches.open('v1').then(function(cache) {
          return cache.addAll(files);
      })
  );
});

self.addEventListener('fetch', function(e) {
  e.respondWith(
      caches.match(e.request).then(function(response) {
          if (response) {
          console.log('Found ', e.request, ' in cache');
          return response;
      }
      else {
          console.log('Could not find ', e.request, ' in cache, FETCHING!');
          return fetch(e.request)
          .then(function(response) {
              const clonedResponse = response.clone(); 
              caches.open('v1').then(function(cache) {
                  cache.put(e.request, clonedResponse);
              })
              return response;
          })
          .catch(function(err) {
              console.error(err);
          });
      }
  })
  );
});

These files were served using python -m http.server

my console output

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
AlaaJw
  • 29
  • 3
  • Better paste text message as text here. – Til Mar 17 '19 at 14:47
  • How do you host `sw.js` ? – Jonas Wilms Mar 17 '19 at 14:49
  • @JonasWilms localhost, python server – AlaaJw Mar 17 '19 at 15:00
  • Well "unsupported MIME type" means that the server sends a wrong response. – Jonas Wilms Mar 17 '19 at 15:02
  • 1
    Service worker needs HTTPS, do have HTTPS enabled in your development environment? https://developers.google.com/web/fundamentals/primers/service-workers/#you_need_https – Alexander Staroselsky Mar 17 '19 at 15:05
  • @JonasWilms do you know how can i fix it? – AlaaJw Mar 17 '19 at 15:05
  • @AlexanderStaroselsky i know it need HTTPS but as far as i know it works on localhost without any problems, or am i wrong? – AlaaJw Mar 17 '19 at 15:07
  • We definetly need your serverside code as this seems to be a problem on the serverside, and I'd add the [python] tag to get the right people here :) – Jonas Wilms Mar 17 '19 at 15:10
  • Related: https://stackoverflow.com/questions/37355890/neterr-insecure-response-in-chrome – Jonas Wilms Mar 17 '19 at 15:12
  • @JonasWilms actually i dont know how to show you this, all i did was running "python -m http.server" in the terminal – AlaaJw Mar 17 '19 at 15:16
  • @AlaaJw Yes it needs HTTPS otherwise you will get the insecure error. Maybe frameworks that generate service worker files do a simple check of whether HTTPS is present before activating the worker. If you want to see an example of this, create-react-app generates a service work file and has that type of check. – Alexander Staroselsky Mar 17 '19 at 15:22
  • @AlaaJw Service workers requires `https` but can be run over `localhost` as well. Make sure you use `http://localhost:PORT` instead of `http://0.0.0.0:PORT` or `http://127.0.0.1:PORT` – Ahmad Santarissy Mar 18 '19 at 18:12

0 Answers0