Mobile internet usage represents over 55% of global web traffic. To engage mobile users, web applications must load quickly and feel responsive. Progressive Web Apps (PWAs) close the gap between web pages and native mobile applications.

What is a Progressive Web App (PWA)?

A Progressive Web App is a website that utilizes modern browser capabilities to deliver an app-like user experience. PWAs can be installed on mobile devices, work offline, send push notifications, and access native device capabilities.


Core Building Blocks of a PWA

To convert a standard responsive web application into a PWA, you must implement three core components:

1. Web App Manifest (manifest.json)

The manifest is a JSON file that provides information about the application (name, description, theme colors, icons, start URL) to the browser. This file enables the "Add to Home Screen" prompt on mobile devices.

{
  "short_name": "umakantdev",
  "name": "umakantdev Solutions",
  "icons": [
    {
      "src": "/favicon-192x192.png",
      "type": "image/png",
      "sizes": "192x192"
    },
    {
      "src": "/favicon-512x512.png",
      "type": "image/png",
      "sizes": "512x512"
    }
  ],
  "start_url": "/",
  "background_color": "#030712",
  "theme_color": "#4f46e5",
  "display": "standalone",
  "orientation": "portrait"
}

2. The Service Worker

A Service Worker is a JavaScript file that runs in the browser background, separate from the main web thread. It acts as a network proxy, allowing you to intercept network requests, cache resources, and enable offline functionality.

3. HTTPS Encrypted Protocol

Service workers can intercept network requests, meaning they require secure origins. PWAs must be served over HTTPS to ensure user data remains secure.


Writing a Service Worker Caching Script

Here is an implementation of a basic Service Worker that uses a **Stale-While-Revalidate** caching strategy. This strategy serves cached resources instantly for speed, while checking the network for updates in the background.

const CACHE_NAME = 'umakantdev-cache-v1';
const ASSETS = [
  '/',
  '/css/style.css',
  '/js/main.js',
  '/about',
  '/contact'
];

// Install Event: Cache essential assets
self.addEventListener('install', event => {
  event.waitUntil(
    caches.open(CACHE_NAME).then(cache => {
      return cache.addAll(ASSETS);
    })
  );
});

// Fetch Event: Stale-While-Revalidate strategy
self.addEventListener('fetch', event => {
  event.respondWith(
    caches.match(event.request).then(cachedResponse => {
      if (cachedResponse) {
        // Fetch network resource in background to update cache
        fetch(event.request).then(networkResponse => {
          caches.open(CACHE_NAME).then(cache => {
            cache.put(event.request, networkResponse);
          });
        });
        return cachedResponse;
      }
      return fetch(event.request);
    })
  );
});

Registering the Service Worker

To run your service worker, register it in your application's main JavaScript file (e.g. main.js):

// Register service worker in main.js
if ('serviceWorker' in navigator) {
  window.addEventListener('load', () => {
    navigator.serviceWorker.register('/sw.js')
      .then(registration => {
        console.log('ServiceWorker registered successfully with scope:', registration.scope);
      })
      .catch(error => {
        console.error('ServiceWorker registration failed:', error);
      });
  });
}

Conclusion

PWAs provide a mobile-first experience that combines the reach of the web with the utility of native apps. Implementing these elements improves performance and user engagement, supporting your traffic and monetization goals.