Link to Codelab
In this codelab, we'll retrace those steps but this time we'll use a tool called Sw-precache to add offline functionality With only six lines of code. It's never been easier to add service worker support to an existing app, and we'll show you what in this codelab.
Learning:
- What's the Sw-precache tool is and how it can help you being more productive
- How to add a basic service worker to an existing project using Sw-precache
Install:
git clone https://github.com/googlechrome/airhorn.git
CD Airhorngit Checkout code-Lab
NPM Install
3000
Gulp:
//Generate sw.jsGulp.task ('Generate-service-worker',function(callback) {var path= Require ('Path'); var Swprecache= Require ('Sw-precache'); var RootDir='app'; Swprecache.Write(Path.Join(RootDir,'Sw.js'), {staticfileglobs: [RootDir+'/**/*. {Js,html,css,png,jpg,gif,mp3}'], Stripprefix:rootdir}, callback);});
Run:
Gulp Generate-service-worker
Then in the app folder, you should see Sw.js file generated.
HTML:INISDE index.html:
<Script> if ('Serviceworker' inchNavigator) {Navigator.serviceWorker.register ('/sw.js'). Then (function() {Console.log ("Service Worker Registered"); }); } </Script>
Verify Your App:
3000
Open in chrome://serviceworker-internals/
Chrome. This would show you a list of all the registered service workers, which you can use to verify your service worker have Indee D properly registered.
Kill the serve and reload the page, everything should work as the same.
[PWA] Sw-precache