Progressive web apps
Laurens Duin - June 2023In today's digital landscape, with more devices, platforms, and browsers than ever, it keeps getting harder to build a seamless user experience for all users. Progressive Web Apps (PWAs) have emerged as a powerful solution, combining the best of web- and native applications. In this article, we will discuss the benefits and features of PWAs, like offline capabilities, push notifications, and app-like experiences.
Benefits of PWAs:
- Offline Capabilities: One of the standout features of PWAs is their ability to function offline or with limited connectivity. PWAs can leverage service workers to cache essential resources, allowing users to access content and perform tasks even when offline. This feature significantly enhances user convenience, especially in areas with unreliable internet access.
- Improved performance: Though this is not a feature exclusive to PWAs, it is worth mentioning that PWAs are designed to be fast and responsive, providing a smooth user experience. By leveraging techniques like caching, preloading, and efficient resource management, PWAs reduce loading times and offer a snappy interface, rivaling native applications.
- Cross-Platform Compatibility: Unlike native apps, which require separate development for each platform, PWAs are built using web technologies such as HTML, CSS, and JavaScript. This enables developers to create a single codebase that works seamlessly across multiple platforms, including desktops, smartphones, tablets and anything that runs a (modern) browser.
To show a simple example of how service workers can be used to implement offline functionality, take a look at the code below.
const offlineCacheName = 'offline';
const offlineCacheFiles = [
'/main.css',
'/offline.html',
'/manifest.json',
'/fonts/pokemon-fire-red.woff2',
'/img/favicon.png',
];
self.addEventListener('install', (event) => {
event.waitUntil(
(async () => {
const cache = await caches.open(offlineCacheName);
await cache.addAll(offlineCacheFiles);
})()
);
});
self.addEventListener('fetch', async (event) => {
event.respondWith(
fetch(event.request)
.then((response) => {
return response;
})
.catch(async () => {
// If there's no network, serve the cached response
const response = await caches.match(event.request);
if (response) {
return response;
} else {
// If there's no cached response, serve the fallback HTML page
return caches.match('/offline.html');
}
})
);
});
Now, this code is a pretty simple example which serves a static fallback page if there's no connectivity. However, much more advanced service-workers can be written. Certain websites could even be almost entirely functional without an internet connection.
Features of PWAs:
- App-Like User Interface: PWAs emulate the look and feel of native applications, offering users a familiar and intuitive interface. They can be added to the home screen, launched with a single tap, and run in full-screen mode, providing an immersive app-like experience.
- Push Notifications: PWAs can use the Push API to send push notifications to users, even when the PWA is not actively running. Though I would find it very annoying to get push notifications from a website, it's still a feature greedy corporations like.
- Offline Data Synchronization: PWAs can synchronize data in the background, ensuring that user data is up to date and consistent across devices. When the device comes back online, any changes made offline are automatically synced, providing a seamless transition between online and offline modes.
Conclusion:
Progressive Web Apps have opened up new possibilities for interacting on the web, blurring the lines between web and native experiences. With their offline capabilities, push notifications, and app-like interfaces, PWAs offer users an engaging and reliable experience, regardless of their internet connectivity. As more businesses and developers embrace PWAs, we can expect to see a new era of web applications that combine the best features of both worlds. After a smaller assignment I did using PWAs, I wasn't fully convinced of the benefits of PWAs. However, after writing this article and diving into the subject a bit more, I have come to appreciate the benefits of PWAs. I think this is a technology that will become more and more important in the future.