Progressive web apps (PWA) are web applications that allow features like platform-specific installation and offline access to function. The ability to install something like a desktop application enhances the user experience, and offline access allows us to work on the go without internet access. These look daunting to implement, but thanks to wonderful architectural design, programmers can build progressive web applications by setting variables in configuration files and a few application calls.
In this blog, we will learn how to build progressive web applications in React.
Now, let’s take a simple HTML/CSS application and convert it into a PWA.
A manifest file is a JSON file that can be used to configure the PWA. The following manifest file can provide the basic parameters for the PWA:
These are simple attributes of the application, like name, short name, description, start URL, theme/background color, and display mode. The display mode can be browser, standalone, minimal, or full screen.
A service worker allows network requests, cache management, and data store access. The service worker file sets up the progressive web application to launch a service worker. It contains the installation, fetch, and activate event handlers, which allow the programmer to manage the cache. Cache management is essential while the PWA is being installed, raising a fetch request, or being activated.
The HTML file needs to know about the manifest file and register the service worker. The manifest file is added as a link in line 2, and the service worker is registered in line 7.
This completes the PWA for a simple HTML/CSS web application.
Now, we will see how we can build a PWA using popular the React framework.
React is the JavaScript library for building user interfaces. It comes with a lot of handy tools. One such tool is create-react-app, which can create boilerplate codes for React applications. It comes with a template for pwa and pwa-typescript to allow boilerplate code for PWA and TypeScript.
Also, there is a template for TypeScript:
This creates all required files for a React Progressive Web Application.
Essential files for PWA: manifest.json, service-worker.js, serviceWorkerRegistration.js.
File paths:public/manifest.json for the manifest file.src/service-worker.js for the service worker.src/serviceWorkerRegistration.js for service worker registration.
Necessary update in src/index.js: Change from unregister() to register().
This change activates the service worker, essential for PWA functionality.
Creating a progressive web application is very simple using the create-react-app tool. However, it’s important to understand the underlying concepts to be able to customize the application. Check out the following course on Educative to learn more:
Zero to Hero with Progressive Web Applications
Progressive Web Applications are the state of modern web solutions. With the proper strategies, it is possible to drastically improve the overall performance and providing data even without a network connection. In an increasingly mobile-first world, optimising the time to load web pages can be our business's success. We will start by describing what PWAs are and their advantages over native and plain web applications. We then go deeper analysing fundamental concepts like service workers, caching strategies and when to use them in different scenarios. Aside from generic, framework-agnostic code samples, there will be an entire "hands-on" section dedicated to Angular, where we will create a complete, real-world PWA project from scratch. Finally, we will learn how to deploy our PWAs to the cloud with different providers, describing in detail the approach with the Firebase platform. Thus, concluding our journey in the PWAs world.