Home Coding The Ultimate Guide to PWA Fundamentals

The Ultimate Guide to PWA Fundamentals

Published By: Scott Green

Published On: March 3, 2021

Published In: Coding

The Ultimate Guide to PWA Fundamentals

Possibility to reach millions of Internet users both on app stores and search engines, on any device using only a single codebase sounds too wonderful to be true? Yet this is a veritable truth with Progressive Web Application (PWA), an emerging trend in modern web development. This is a highly-promising technology designed to transform the way users interact on the web.

We will introduce you to the concept and benefits of PWA solutions, which are a buzz topic today. We also will share three steps that will help you enhance the existing platform with PWA features.

The general idea of PWA

PWA is a standard website but enriched with features that allow creating the native app-like experience. This type of web apps is optimized to run on mobile phones like native Android and IOS applications, supporting such functionalities as mobile notifications, offline work, app installation. At the same time, PWAs still look and behave as responsive web pages, indexed by Google and accessible through URLs.

PWA core features & benefits

PWA is a robust tech that blends the capabilities of the website and mobile software enabling creating an immersive user experience. Some of the most fascinating features that make PWAs the preferred choice for many are: 

  • The offline capability allows users to view website content, interact with it and browse even with a low internet connection or in offline mode
  • Fast loading times. PWA pages load several-time faster than responsive websites
  • Platform and device agnostic. PWA works everywhere and delivers a uniform user experience to everyone across all devices.
  • Push notifications. The marketers will keep the users engaged by motivating them to visit the app by sending them messages. 
  • SEO-friendliness. Just like classic websites, PWAs have URLs. They are optimized and indexed by Google.
  • Always up to date. PWAs don’t require manual updates instead they are updating in real-time like usual websites.  

It looks like everyone wins with PWA.

Here are the main requirements you should meet to unlock PWA features:

  • Be served over HTTPS
  • Work offline 
  • Have manifest.json file
  • Load fast with a slow internet connection
  • Be responsive

We have outlined three steps you can take to extend the functionality of your existing site and enable some simple PWA features, capable of improving the site performance and enhancing UX. Before proceeding next, make sure your platform is served over HTTPS. It’s a recommended security protocol and a new standard for the web. The emergence of PWA technology made it more mainstream since it relies on service workers, which run only over HTTPS due to security reasons.

Step 1. Initiate a Web App Manifest file

This file is meant for describing your website and providing all the data about it, including the source and size of icons, app name, colors, starting URLs. You should create a manifest.json file at the same level as index.html. Here is an example of the JSON structure:

You can leverage a generator that will save you time as it requires filling only certain fields to generate a manifest automatically for you. After adding this file to your project’s main folder, you have to connect it with your web application by adding a link-tag between the head tags in index.html.

This script will identify your platform as an application to the browser. You can verify whether you successfully set up a manifest in the Chrome DevTools → Application tab → Manifest section.

Step 2. Introduce a service worker

This is the core technology of the PWA that enriches your app with extended functionality. It’s essentially a JS file that sits between the network & the app and enables controlling network requests, performing advanced caching, and supporting offline mode through the cash. With it, you have control over your app behavior in any network situation.  

Registration. First of all, your application must register a service worker. To do this, create a file with the name serviceWorker.js at the root of your project. Then go to the main JS file, which in our case is index.js, and insert the script below.

This code verifies the browser support of the server worker and its availability in the navigator. If it’s available it will be registered soon after the page reload. With the help of the same Dev Tools, you used for checking manifest, will show your service worker listed in the ApplicationTab → ServiceWorkers section.

Step 3. Service Worker configuration

  • Installation & activation are the first phases of a service worker’s life cycle. Add install and activate event using self.addEventListener and reload the page to check the logs. If it detects an active service worker that controls clients, you will get only the installation log. In the DevTools you will see your new service worker waiting for activation. Click the skipWaiting link for your previous service worker to disappear and a new one to activate.
  • Caching and fetching app assets. The best time for precaching is during the installation phase. Use caches.open() and cache.addAll() to add all scripts, styling files, and HTML pages to the serviceWorker.js file you created in the previous step. Pass the Promise returned by cache.addAll as an argument to the event.waitUntil() to activate the ServiceWorker after the precaching completion.

You can look at the cached files also in the Developer Tools → Cache Storage. Now your service worker can intercept requests and you have to complete one more code manipulation to make your web app operating offline – fetching. To fetch the assets, define a fetch event in your serviceWorker.js to get the data back.

The caches.match() function allows you to check whether the URL matches anything in your current cache. If there is anything, you return the cached resources and if there are no needed resources, the service worker fetches the requested ones.

Congrats, you made your application available offline, the most sought-after & valuable PWA feature.   

If you have successfully implemented these steps, you can check your PWA performance and accessibility in the Google Lighthouse tool. The instrument assesses your app and provides suggestions on improvements. 

That’s all. You have completed the transformation of a regular site a PWA!

Final thoughts

Many well-known brands and leading businesses have increased the revenue and engagement rates by leveraging the power of PWA technology. Here are the most prominent examples of robust PWA solutions implemented by Pinterest and Tinder.

More and more companies are getting interested in this tech for ease of development, instant gains, and lightweight web app solution that reaches a wider userbase and increases engagements. 

This guide will help you turn a simple website into a PWA with appealing user experiences and improved performance. It requires some knowledge of the core web technologies, but it can be achieved. If you want to unlock the full power of PWA technology or your site is large and more complex, you may face challenges. In case of any issues with your PWA project, contact our specialists and we will be happy to help you build a robust and reliable solution that will insanely convert new customers.

SHARE:
About the author
Scott Green

Scott Green

author

Scott Green is a tech-savvy writer who is excited about describing complex topics in the web and mobile software development field in simple words. Having hands-on experience in JavaScript & PHP engineering, he shares tips and best practices.