Turn Your Boring Web App Into a Progressive One

118 VIEWS

The mobile web still doesn’t compare to a native mobile application—You just can’t do smooth transitions, push notifications, or offline accessibility and still be blazing fast. But recently, due to a new technology called Progressive Web Apps (PWA), we can aspire to have native app-like experiences in our websites, too. If you don’t know what a Progressive Web App is, it’s used to make your web app feel more like a native app, and you use service workers to achieve this. A service worker is a script that runs in the background, and is separate from a webpage. It can be used to make amazing stuff like:

  1. Background Sync
  2. Push Notifications
  3. Great offline UX
  4. Great page speed with any connectivity
    1. As its name suggests, a Progressive Web App will progressively improve user experience. However, when users are on unsupported browsers, it will gracefully ignore these optimizations. That’s the power of PWAs. There are no set goals to achieve. You can just improve and increase the features of your site, and it will only make an impact on those web browsers which support it.

      I’m going to take my site and make it a Progressive Web App just for fun. You can see the final code here.

      Secure Your Website

      HTTPS is the gold standard of web security. But it’s not just about security. Without HTTPS, service workers will not work, since Progressive Web Apps can not be served from an insecure origin.

      Add the Manifest

      Once you are sure about HTTPS, you can move on to adding a manifest file. This manifest file tells the browser about all the meta information that will be needed for the PWA.

      First, add the file to all of your HTML files with a link tag like this:

      This will link you to your manifest.json file, which looks like this:

      You can learn more about the manifest file in the Web Fundamentals guide by Google.

      Finally, the Service Worker

      A service worker is basically a Javascript file that acts as an intermediary between the browser and the network. This gives us access to control the network flow and assign which request goes where. There are lots of other benefits to this service worker file, like push notifications, offline support, aggressive caching, and more!

      Let’s now focus on the lifecycle of a service worker file:

      When the website first fires up, there will be no service worker installed, obviously. However, after the page loads, it gets installed along with two markers—The current page and the default value for state (‘Activated’). As the user navigates the website, the requests get piled up in the service worker, which then decides if it should go to the network, or fetch from local cache! It’s as simple as that. Here are a few resources I found helpful when researching service workers:

      1. Service Workers: an Introduction
      2. Service workers explained
      3. Caching with service workers, the easy way
      4. Making a Simple Site Work Offline with ServiceWorker

      Let’s now add the final bit:

      In index.html:

      In service-worker.js:

      So, in the service worker file you can cache, redirect network requests, and even specify the offline pages. A PWA is essentially a really powerful service worker.

      Let’s see how this would be handled in a mobile browser:


      When the user first loads the website, the browser will ask to add it to their home page. If the user agrees, they will go through custom splash screens before the app (‘website’) loads. You can also set the home screen icon!

      To learn more about service workers and their awesomeness, I’d recommend checking out:

      1. The Service Workers API
      2. W3C Service Worker
      3. Many Recipes and good practices for Service Workers
      4. Even more examples by Google
      5. A Simple Service Worker App
      6. Offline Recipes for Service Worker

      And that’s it! You now have a site that is considerably faster and will act as a stand-alone app in mobile devices.

      You can use the Audits tab in Chrome DevTools (which uses Lighthouse) to validate and measure your implementation.

      To compare, this is before installing the service worker:

      And after:

      And that’s how you make a Progressive Web App! Though the implementation varies when you’re trying to turn a dynamic website into a PWA, this should help you get started.

      So get cracking and turn your site into a PWA today!

      Do you think you can beat this Sweet post?

      If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Swaathi Kakarla is the co-founder and CTO at Skcript She enjoys talking and writing about code efficiency, performance and startups. In her free time she finds solace in yoga, bicycling and contributing to open source.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu