Progressive Web Apps (PWAs) are web applications that provide a native-like experience on the web. PWAs combine the best features of web and mobile apps by being fast, reliable, and engaging. They can be installed on devices, work offline, and send push notifications, making them a great solution for building modern applications.In this chapter, we will cover how to integrate React with PWAs from basic to advanced, focusing on key features like service workers, offline functionality, and app manifest. By the end, you’ll have a thorough understanding of how to create a PWA using React and enhance it with advanced features.
A Progressive Web App (PWA) is a web application that behaves like a native app. It leverages modern web technologies to deliver enhanced user experiences. PWAs are built to work on any platform with a standards-compliant web browser, and they use features like service workers, web app manifests, and HTTPS to provide users with fast, secure, and engaging experiences.
A JavaScript file that runs in the background and handles caching, push notifications, and offline support. Service workers are crucial for providing offline capabilities.
A JSON file that defines how the app behaves when installed on a device. It specifies the app’s icon, name, and splash screen.
PWAs are secure because they are served over HTTPS, ensuring that all network traffic is encrypted.
PWAs adapt to different screen sizes and orientations, providing an optimal user experience on mobile, tablet, and desktop devices
To create a PWA with React, we can use create-react-app
, which comes with built-in support for service workers and PWA capabilities.
create-react-app
with PWA template:
npx create-react-app my-pwa-app --template cra-template-pwa
cd my-pwa-app
npm start
src/serviceWorkerRegistration.js
file. It registers the service worker by default, which handles offline functionality.serviceWorkerRegistration.js
: Handles the registration of the service worker.manifest.json
: Contains metadata for the PWA like app name, icons, and theme color.Service workers enable caching and allow your app to work offline. React’s create-react-app
includes a service worker by default, but you can customize it to add advanced caching strategies.
const CACHE_NAME = 'my-app-cache-v1';
const urlsToCache = ['/', '/index.html', '/static/js/main.js'];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
return response || fetch(event.request);
})
);
});
In this example, the service worker caches the specified URLs during the install phase and serves the cached content during the fetch phase when the user is offline.
Caching strategies determine how your app handles network requests and cached content. Some common strategies include:
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
return response || fetch(event.request);
})
);
});
When the user navigates to a page without internet, the offline page is displayed.
The Web App Manifest is a JSON file that provides essential information about the PWA, including its name, icons, and theme color. This file enables the app to be installed on a user’s device.
public/manifest.json
):
{
"short_name": "MyPWA",
"name": "My Progressive Web App",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
Push notifications help keep users engaged with your app by sending real-time messages even when the app is not open. You can use service workers to handle push notifications.
self.addEventListener('push', (event) => {
const title = 'Notification Title';
const options = {
body: event.data.text(),
icon: '/icons/icon-192x192.png',
};
event.waitUntil(self.registration.showNotification(title, options));
});
When deploying a PWA, ensure that your app is served over HTTPS and the service worker is properly registered.
1.Build the React app:
npm run build
2.Deploy to a hosting service (such as Netlify, Vercel, or Firebase Hosting).
Verify that the service worker is registered, and the manifest is correctly configured by running a Lighthouse audit in Chrome DevTools
Allows you to defer tasks until the user has a stable internet connection.
self.addEventListener('sync', (event) => {
if (event.tag === 'sync-tag') {
event.waitUntil(syncData());
}
});
Explanation: Here, we use the useState
hook to create a state variable called count
. Clicking the button updates the state, which triggers a re-render.
Progressive Web Apps are powerful tools for improving user experiences in React applications. By integrating service workers, caching strategies, and the web app manifest, you can create applications that are fast, reliable, and installable. PWAs combine the best of the web and native apps, making them a great solution for modern web development. Happy coding !❤️