Registering our first service worker
Remember the distinction about service workers--they are a piece of our site, but run outside our application code. With that in mind, our service worker will live inside public/ folder
, and not in src/ folder
.
Then, create a file called sw.js
in your public/ folder
. We'll keep it simple for now; just add a single console.log
inside:
console.log("Service worker running!");
The real work (registering the service worker) will be done inside our index.html
. For this process, we want to do the following:
- Check whether the browser supports service workers.
- Wait for the page to load.
- Register the service worker.
- Log out the result.
Let's move through the steps one by one. First, let's create an empty script
tag below our Firebase initialization, inside public/index.html
:
<body> <div id="root"></div> <script src="/secrets.js"></script> <script src="https://www.gstatic.com/firebasejs/4.1.2/firebase.js"></script> <script...