Home About Skills Products
Work
Projects Services Experience
Learn
Tutorials Courses Blogs Resources
Contact
Blog · JavaScript

Building a Progressive Web App (PWA) from Scratch

Building a Progressive Web App (PWA) from Scratch
Advertisement

A Progressive Web App is a normal website that also works offline, installs to a home screen, and can send push notifications — all through standard web APIs, no app store submission required. Here's how to build one from an existing site.

The Web App Manifest

// manifest.json
{
  "name": "My App",
  "short_name": "MyApp",
  "start_url": "/",
  "display": "standalone",
  "background_color": "#ffffff",
  "theme_color": "#16a34a",
  "icons": [
    { "src": "/icon-192.png", "sizes": "192x192", "type": "image/png" },
    { "src": "/icon-512.png", "sizes": "512x512", "type": "image/png" }
  ]
}
<link rel="manifest" href="/manifest.json">

"display": "standalone" is what makes the installed app open without the browser's URL bar and tabs — visually indistinguishable from a native app on the home screen.

The Service Worker: Offline Capability

// sw.js
const CACHE_NAME = 'app-v1';
const ASSETS = ['/', '/styles.css', '/app.js', '/offline.html'];

self.addEventListener('install', (event) => {
  event.waitUntil(
    caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS))
  );
});

self.addEventListener('fetch', (event) => {
  event.respondWith(
    caches.match(event.request).then((cached) => cached || fetch(event.request))
  );
});
// Registering it
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js');
}

The service worker sits between your app and the network — the fetch handler above serves a cached response when one exists, falling back to a real network request otherwise, which is what keeps the app usable with no connection at all.

Cache Strategies

// Network-first (good for frequently changing content, e.g. an API)
async function networkFirst(request) {
  try {
    const response = await fetch(request);
    const cache = await caches.open(CACHE_NAME);
    cache.put(request, response.clone());
    return response;
  } catch {
    return caches.match(request);
  }
}

Cache-first (shown in the basic example above) suits static assets that rarely change. Network-first suits anything where showing stale data would actually mislead the user, falling back to cache only when there's genuinely no connection.

Making It Installable

window.addEventListener('beforeinstallprompt', (e) => {
  e.preventDefault();
  deferredPrompt = e; // save it, show your own "Install" button, call deferredPrompt.prompt() later
});

Chrome and Edge fire this event automatically once a site has a valid manifest and a registered service worker — capturing it lets you show your own styled install button instead of relying on the browser's default, often easy-to-miss prompt.

What a PWA Can't Do

PWAs still can't access every native API (deep Bluetooth integration, certain background processing) and iOS Safari's PWA support, while much improved, still lags behind Chrome/Android in some areas. For a content-driven app, an ecommerce storefront, or most business web apps, a PWA today closes the gap with a native app for a fraction of the development cost.

JavaScript Fundamentals Every Beginner Should Master

JavaScript Fundamentals Every Beginner Should Master

Variables, functions, arrays, objects, and the DOM — the plain JavaScript every framework is built on top of.

Async/Await and Promises in JavaScript: A Practical Guide

How Promises actually work, why async/await is just readable syntax on top of them, and the parallel-fetching mistake almost everyone makes.

Modern JavaScript (ES6+) Features You Should Be Using

Modern JavaScript (ES6+) Features You Should Be Using

Template literals, destructuring, optional chaining, and modules — the JavaScript you actually see in every modern codebase.

Advertisement
Esc