0

My application is based on subdomains and I am trying to use service workers in my application.

  1. How do I run service worker on abc.localhost.com . I understand google has not whitelisted subdomains on localhost to use service workers but is there a work around?

  2. I cannot get service worker to work in development mode.

There is app folder that has the application content.

If I register service-worker in app folder, I cannot cache bower components and then app does not work offline.

If I register service worker outside of app folder, index.html which is in app folder, is never able to find path of servie-worker.js which is outside of app folder, in the root directory.

I have tried this in index.html:

if( 'serviceWorker' in navigator ) {
    navigator.serviceWorker
      .register( '../service-worker.js' , { scope : ' ' } )
      .then( function( ) {
        console.log('Congratulations!!Service Worker Registered');
      })
      .catch( function( err) {
        console.log(`Aagh! Some kind of Error :- ${err}`);
      });
  } else {
    console.log("SW NOT SUPPORTED");
    //still not supported
  }

But I only get 404.

I have also tried using absolute path i.e

  .register( '/service-worker.js' , { scope : ' ' } )
systemdebt
  • 4,589
  • 10
  • 55
  • 116

1 Answers1

0

Since service worker only work for localhost and https/ssl url

My flow for service worker + subdomain on local machine is create https url for my development env, i use https://myapp.local and https://subdomain.myapp.local point to 127.0.0.1:

  1. Install webserver on your local machine (Nginx/Apache/etc)
  2. Setup your local app with that webserver
  3. edit /etc/hosts file for local domain host config (myapp.local, subdomain.myapp.local)
  4. Setup OpenSSL for that webserver (now we able to use https)
  5. trust your open certificates (example ref for mac https://tosbourn.com/getting-os-x-to-trust-self-signed-ssl-certificates, not tested, just googling, forgot my real reference) so you able access with green ssl on your browser.
  6. take a coffee.
Agung Yuliaji
  • 494
  • 3
  • 19