Your cart is currently empty!
Products & Solutions
Software Development
Embedded Development
Product Engineering
Robotics & Automation
A Progressive Web App (PWA) is a type of application built using standard web technologies like HTML, CSS, and JavaScript, but with the capability to work offline and deliver a more native app-like experience. PWAs are designed to provide fast, reliable, and engaging user experiences across all devices, regardless of network conditions.
PWAs work through a combination of modern web technologies such as Service Workers, Web App Manifests, and HTTPS.
Service workers are JavaScript files that run in the background, separate from the main browser thread. They allow PWAs to handle caching, push notifications, and background data syncs, even when the app is offline.
The web app manifest is a JSON file that defines how the PWA should appear when added to a device’s home screen. It includes settings like the app name, icons, start URL, theme color, and display properties.
PWAs require HTTPS to ensure a secure connection. This is important because service workers can only work on secure contexts to prevent malicious attacks.
Let’s walk through the process of building a simple Progressive Web App.
Start by creating a new directory for your project and navigating to it:
bashCopy codemkdir my-pwa
cd my-pwa
You need at least an index.html
file, a style.css
file for styling, and a main.js
for the app’s functionality.
index.html:
htmlCopy code<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Progressive Web App</title>
<link rel="manifest" href="manifest.json">
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Welcome to My PWA!</h1>
<button id="installBtn">Install App</button>
<script src="main.js"></script>
</body>
</html>
style.css:
cssCopy codebody {
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
color: #333;
}
main.js:
javascriptCopy code// Check if the browser supports service workers
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/service-worker.js')
.then((registration) => {
console.log('Service Worker registered with scope:', registration.scope);
})
.catch((error) => {
console.log('Service Worker registration failed:', error);
});
});
}
// Install event for PWA
let deferredPrompt;
const installBtn = document.getElementById('installBtn');
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
deferredPrompt = e;
installBtn.style.display = 'block';
});
installBtn.addEventListener('click', () => {
if (deferredPrompt) {
deferredPrompt.prompt();
deferredPrompt.userChoice.then((result) => {
if (result.outcome === 'accepted') {
console.log('PWA installed');
} else {
console.log('PWA installation declined');
}
});
}
});
The manifest file defines how your app will appear when added to a home screen.
manifest.json:
jsonCopy code{
"name": "My Progressive Web App",
"short_name": "My PWA",
"description": "An awesome Progressive Web App!",
"start_url": "/",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": "#000000",
"icons": [
{
"src": "icons/icon-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "icons/icon-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}
Now, create a service worker to enable offline functionality.
service-worker.js:
javascriptCopy codeconst cacheName = 'pwa-cache-v1';
const cacheAssets = [
'/',
'/index.html',
'/style.css',
'/manifest.json',
'/icons/icon-192x192.png',
'/icons/icon-512x512.png'
];
// Install Service Worker
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open(cacheName)
.then((cache) => {
console.log('Service Worker: Caching Files');
cache.addAll(cacheAssets);
})
);
});
// Activate Service Worker
self.addEventListener('activate', (e) => {
console.log('Service Worker: Activated');
});
// Fetch event
self.addEventListener('fetch', (e) => {
e.respondWith(
caches.match(e.request)
.then((cacheResponse) => {
return cacheResponse || fetch(e.request);
})
);
});
Now, to test your app, run a local server since service workers only work on secure contexts (HTTPs). You can use a simple tool like Live Server or http-server for this:
bashCopy codenpm install -g http-server
http-server
Visit your app at http://localhost:8080
and you should see your PWA in action. It will be installable, work offline, and cache assets.
Progressive Web Apps are revolutionizing the way we think about web applications, combining the best of both web and native apps. By following the steps above, you can create your own PWA that’s fast, reliable, and engaging. PWAs not only provide a seamless experience for users but also offer developers a more cost-effective way to build apps that work across all platforms.
By following this tutorial, you can start building your own Progressive Web Apps and tap into the future of web technology!