Create Your First Ever Offline-First Progressive Web App

Why do businesses prefer to have native apps even though they have fully responsive websites? Native apps load faster, give full-screen experience, have offline capabilities, support push notifications, and significantly add to user convenience. What if your mobile website had all of these benefits? That’s what a Progressive Web App is all about—the rich experience of an app and the full functionality of a website.

Progressive Web Apps (PWA) make use of the new capabilities of browsers to provide a full screen, feature-rich experience to users regardless of space and bandwidth constraints. If you use Progressive Web Apps, your users will get new features as soon as you push your changes to production. No more painful app updates. Now, you get an idea why businesses are going gaga over Progressive Web Apps.

Steps to Create an Offline-First Progressive Web App

Let’s build an offline-first Progressive Web App that can deliver the details of upcoming cricket matches. Push notifications are not covered in this blog post for the sake of brevity.

1. Create index.html File

You might have noticed two things in the above code: ‘manifest.json’ and ‘serviceWorker.js’. The manifest.json file provides the metadata of your application to the browser. Service workers are web workers that can live outside DOM and make requests on behalf of your app. Service workers need to be served through HTTPS protocol for security reasons. Localhost is also supported for the sake of development.

2. Create manifest.json File

In the manifest file, you can define the app’s name, theme color, icon, orientation, and so on. I have added a few in this example. You can find the complete list of manifest variables here.

3. Install Service Worker

Now that the manifest file is in place, we need to install the service worker.

The above code makes the Progressive Web App installable. Let’s check our website on a browser. Fire up a server. I’ve used Python SimpleHTTPServer. You can use any server of your choice. Use the command below to start a server at http://localhost:8000.

You can see the “service worker installed” message on the console.

 

Progressive Web App

 

Let’s inspect it in Chrome DevTools. Open Application tab in Chrome DevTools. The service worker is activated and running. To check the Manifest file, click Manifest. There is an Add to homescreen link on the right side of the screen. You can use it to test whether the app is installable or not. 

 

Progressive Web App

 

You can inspect the service worker on the Service Workers tab.

 

Progressive Web App

4. Bring Data to the App

Create a new file, app.js, with the following lines of code. Link it with your index.html file.

You can find the complete code here. Fetch API provides a way to make network requests similar to XMLHttpRequest. Unlike XMLHttpRequest, Fetch API returns a promise.

Progressive Web App

Now you have an installable app that can live outside the browser. However, your app does not have offline support yet. The service worker comes in handy in this situation. The service worker can act as a proxy between your application and the network and return cached responses when offline. The only thing the service worker can’t do is update DOM. But it has access to Cache API, IndexedDB, and so on. We can make use of Cache to access our static assets.

5. Cache Static Assets

Remember that we have already added a listener for the install event above. Update the listener to cache the static content.

Let’s walk through the above code. We defined our cache name and provided URL for every static asset. We also provided third-party URLs so that we have everything cached and ready when the app goes offline. Then we opened the cache and added every single file to cache. waitUntil() will make sure this happens only after service worker installation is complete.

6. Serve Cached Response

Though we have cached the site, we haven’t told the service worker to serve the cached response. Let’s register another listener for every fetch event.

The above code listens for any fetch event. If a match is found in the cache, it serves the cached response; otherwise, it fires external requests. Refresh the browser and check whether the service worker is working.

 

Progressive Web App

 

The Size column on the screen shows that requests are served from service worker, which means our service worker is working fine. Our Progressive Web App with offline support is now ready. Test your app after disconnecting the network. You can also test it by using the offline mode in Chrome developer tools.

Additional Requirements

What I described here is a simple Progressive Web App. In a real-life scenario, you need a database to store multiple values against a key. You can use IndexedDB for that. The service worker code I’ve provided is not suitable for production use. You need to add a cache busting mechanism to clear out the cache. You can use sw-precache and sw-toolbox in production. They are actively supported libraries in GitHub and can be easily incorporated in your build process.