
The Role of Progressive Web Apps in Modern Web Design
The Role of Progressive Web Apps in Modern Web Design In today’s fast-paced digital world, users expect fast, reliable, and engaging web experiences. To meet…
Web Development

LET’S BUILD SOMETHING USEFUL
Talk to Ataraxy Developers about your website, app, or custom software.
In today’s fast-paced digital world, users expect applications that are not only functional but also provide a seamless experience, regardless of their internet connection or device. Progressive Web Apps (PWAs) are a solution that bridges the gap between web and mobile applications, offering features like offline access, push notifications, and app-like performance. In this blog, we will explore how to create Progressive Web Apps using React, one of the most popular front-end libraries.
Progressive Web Apps leverage modern web capabilities to deliver an app-like experience to users. They are built using standard web technologies such as HTML, CSS, and JavaScript, but also incorporate specific features that make them stand out:
Before you start building your PWA, ensure you have a React environment set up. You can create a new React application using Create React App, which provides a solid foundation for developing PWAs.
Run the following command in your terminal:
npx create-react-app my-pwa --template cra-template-pwa
This command creates a new React application named my-pwa with PWA support preconfigured.
Change into your project directory:
cd my-pwa
Once you create your PWA, take a look at the project structure. Key files related to the PWA functionality include:
manifest.json:{
"short_name": "MyPWA",
"name": "My Progressive Web Application",
"icons": [
{
"src": "icon-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "icon-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
Service workers are scripts that run in the background and manage caching, enabling offline access. Create React App automatically registers a service worker for you. However, you can customize its behavior by editing src/service-worker.js.
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open('v1').then((cache) => {
return cache.addAll([
'/',
'/index.html',
'/static/js/bundle.js',
'/static/css/main.css',
'/icon-192x192.png',
'/icon-512x512.png',
]);
})
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
return response || fetch(event.request);
})
);
});
In this example, the service worker caches essential files upon installation and serves them when the app is offline.
One of the main advantages of PWAs is their ability to work offline. You can enhance your service worker to cache dynamic content and serve cached responses when the network is unavailable.
Modify your service worker to handle dynamic content caching:
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((cachedResponse) => {
return (
cachedResponse ||
fetch(event.request).then((response) => {
return caches.open('dynamic-cache').then((cache) => {
cache.put(event.request, response.clone());
return response;
});
})
);
})
);
});
This code snippet checks if a request is in the cache and fetches it if available. If it’s not cached, it fetches it from the network and caches it for future use.
Push notifications are an essential feature for engaging users in your PWA. To implement push notifications, you need to integrate the Push API and the Notifications API.
Request permission from the user to send push notifications:
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
console.log('Notification permission granted.');
} else {
console.error('Notification permission denied.');
}
});
After obtaining permission, subscribe the user to push notifications:
navigator.serviceWorker.ready.then((registration) => {
registration.pushManager
.subscribe({
userVisibleOnly: true,
applicationServerKey: urlB64ToUint8Array('YOUR_PUBLIC_VAPID_KEY'),
})
.then((subscription) => {
console.log('User is subscribed:', subscription);
// Send subscription to your server for push notifications
})
.catch((error) => {
console.error('Failed to subscribe the user: ', error);
});
});
Make sure to replace 'YOUR_PUBLIC_VAPID_KEY' with your actual VAPID public key.
In your service worker, listen for push events and display notifications:
self.addEventListener('push', (event) => {
const data = event.data ? event.data.json() : {};
const title = data.title || 'Default Title';
const options = {
body: data.body || 'Default notification body.',
icon: 'icon-192x192.png',
};
event.waitUntil(
self.registration.showNotification(title, options)
);
});
After implementing the essential features of your PWA, it’s crucial to test its functionality. You can use Chrome DevTools to test and debug your PWA:
Once you have tested your PWA and confirmed its functionality, it’s time to deploy it. You can host your PWA on various platforms, including Vercel, Netlify, or your own server.
npm run build
build folder to your preferred hosting provider.To ensure your PWA provides the best user experience, consider the following best practices:
Creating Progressive Web Apps using React allows you to deliver a superior user experience while leveraging the power of modern web technologies. By following the steps outlined in this blog, you can build a PWA that offers offline capabilities, push notifications, and an app-like experience. As you continue to develop your skills, remember to stay updated with the latest trends and best practices in web development.
With the right tools and techniques, you can create engaging and responsive web applications that keep users coming back for more.
LET’S BUILD SOMETHING USEFUL
Talk to Ataraxy Developers about your website, app, or custom software.