Create an empty js file; service-worker.js in your source root. In the
empty source service
worker file, say ./src/service-worker.js
; add the following code
snippet: Go through
the comments.
Checkout for the latest workbox release here and update the
importScripts below say 3.6.1 to 4.3.1 if it's available.
importScripts("https://storage.googleapis.com/workbox-cdn/releases/5.0.0/workbox-sw.js");
if (workbox) {
console.log(`Yay! Workbox is loaded ! Cheers to PWA Fire 🐹`);
workbox.precaching.precacheAndRoute([]);
} else {
console.log(`Oops! Workbox didn't load 👺`);
}
The importScripts call imports the workbox-service-worker.js
library from a Content
Delivery Network (CDN).
Once the library is loaded, the workbox object gives our service worker
access to all the
Workbox modules.
The precacheAndRoute method of the precaching module takes a precache
"manifest" (a list of
file URLs with
"revision hashes") to cache on service worker installation.
Create an empty sw-config.js file in your project's root folder.
Add the code snippet
below to it; configure to add which files you want to precache.
module.exports = {
"globDirectory": "build/", // The base directory you wish to match globPatterns against,
// relative to the current working directory.
"globPatterns": [
// edit to add all file to cache; configure for your project below
"**/*.css", // eg cache all css files, images etc in the root folder
"index.html",
"images/*.jpg"
],
"swSrc": "./src/service-worker.js", // The path and filename of the Service Worker file that will be created by the build process.
"swDest": "build/service-worker.js", // The path to the source service worker file that can contain your own customized code,
// in addition to containing a match for injectionPointRegexp.
"globIgnores": [
"../sw-config.js"
]
};
Open your package.json and update the build script to run the
Workbox
injectManifest command.
Add workbox injectManifest sw-config.js
infront of
your-build-process-task
. The updated package.json
should look like the
following:
{
"scripts":
{
"build": "your-build-process-task && workbox injectManifest sw-config.js"
}
}
NOTE : workbox-cli
; is a command-line tool that
allows us to inject a
file manifest into a source service worker file. Add it to your
devDependencies
as
in the code below:
{
"devDependencies": {
"workbox-cli": "^3.0.1"
}
}
The precacheAndRoute call in build/service-worker.js
has been updated. In
your text editor,
open build/service-worker.js
and observe that your files
to cache are
included in the file manifest.
Done! Now you will have your production-ready service workers when you
build your web app!
Let's build! If you get any bug, report it here
OPTIONAL : Want offline analytics for your offline PWA? No
problem. Add the code snippet
below to ./src/service-worker.js
workbox.googleAnalytics.initialize();
OPTIONAL : Wish you could rely on Google Fonts being
available offline after the
user has visited your site? Add a quick rule to serve them from the
cache.
workbox.routing.registerRoute(
new RegExp('^https://fonts.(?:googleapis|gstatic).com/(.*)'),
workbox.strategies.cacheFirst(),
);