Esta página ainda não está disponível em Português, por isso está a ler a versão em inglês.

Build a PWA from scratch: a complete tutorial with no framework

From index.html to an installable, offline app in seven steps.

Conceitos básicos Iniciante 3 min de leitura Revisado em 27 de nov. de 2026

This tutorial turns a plain static site into a complete PWA. No framework, no build step: just files you can read. It takes about an hour.

You need: a folder with an index.html, a local web server (for example npx serve or python3 -m http.server) and Chrome or Edge for DevTools. localhost counts as a secure origin, so you do not need HTTPS until you deploy.

Step 1: a page that works without JavaScript

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
  <title>Notes</title>
  <meta name="theme-color" content="#5A0FC8">
  <link rel="manifest" href="/manifest.webmanifest">
  <link rel="icon" href="/icons/icon.svg" type="image/svg+xml">
  <link rel="apple-touch-icon" href="/icons/apple-touch-icon.png">
  <link rel="stylesheet" href="/app.css">
</head>
<body>
  <header><h1>Notes</h1><button id="install" hidden>Install</button></header>
  <main id="app">...</main>
  <script src="/app.js" type="module"></script>
</body>
</html>

Step 2: the manifest

Create /manifest.webmanifest. Our manifest generator can write it for you.

{
  "id": "/",
  "name": "Notes",
  "short_name": "Notes",
  "description": "Quick notes that work offline.",
  "start_url": "/?source=pwa",
  "scope": "/",
  "display": "standalone",
  "background_color": "#F4F6FB",
  "theme_color": "#5A0FC8",
  "icons": [
    { "src": "/icons/icon-192.png", "sizes": "192x192", "type": "image/png" },
    { "src": "/icons/icon-512.png", "sizes": "512x512", "type": "image/png" },
    { "src": "/icons/maskable-512.png", "sizes": "512x512", "type": "image/png", "purpose": "maskable" }
  ]
}

Step 3: icons

Make a 512 x 512 PNG and a maskable version with the logo inside the central 80%. The icon generator makes every size from one image, and the maskable icon preview shows how it will be cropped.

Step 4: the service worker

Create /sw.js at the root of the site. It precaches the app shell, serves pages network-first and falls back to an offline page.

const VERSION = 'v1';
const SHELL = ['/', '/offline.html', '/app.css', '/app.js', '/icons/icon-192.png'];

self.addEventListener('install', (event) => {
  event.waitUntil(caches.open('shell-' + VERSION).then((c) => c.addAll(SHELL)));
});

self.addEventListener('activate', (event) => {
  event.waitUntil(caches.keys().then((keys) =>
    Promise.all(keys.filter((k) => !k.endsWith(VERSION)).map((k) => caches.delete(k)))));
  self.clients.claim();
});

self.addEventListener('fetch', (event) => {
  const req = event.request;
  if (req.method !== 'GET') return;
  if (req.mode === 'navigate') {
    event.respondWith(fetch(req).then((res) => {
      const copy = res.clone();
      caches.open('pages-' + VERSION).then((c) => c.put(req, copy));
      return res;
    }).catch(async () => (await caches.match(req)) || caches.match('/offline.html')));
    return;
  }
  event.respondWith(caches.match(req).then((hit) => hit || fetch(req)));
});

self.addEventListener('message', (e) => { if (e.data === 'SKIP_WAITING') self.skipWaiting(); });

The service worker generator builds a fuller version with the strategies you choose. Read caching strategies to understand the options.

Step 5: register it, and offer updates

// app.js
if ('serviceWorker' in navigator) {
  addEventListener('load', async () => {
    const reg = await navigator.serviceWorker.register('/sw.js');
    const ask = (worker) => {
      if (confirm('A new version is ready. Reload now?')) worker.postMessage('SKIP_WAITING');
    };
    if (reg.waiting) ask(reg.waiting);
    reg.addEventListener('updatefound', () => {
      const w = reg.installing;
      w.addEventListener('statechange', () => {
        if (w.state === 'installed' && navigator.serviceWorker.controller) ask(w);
      });
    });
    let reloaded = false;
    navigator.serviceWorker.addEventListener('controllerchange', () => {
      if (!reloaded) { reloaded = true; location.reload(); }
    });
  });
}

In a real app, use a small banner instead of confirm(). See service worker lifecycle and updates.

Step 6: an install button

let deferred;
const btn = document.getElementById('install');
addEventListener('beforeinstallprompt', (e) => { e.preventDefault(); deferred = e; btn.hidden = false; });
btn.addEventListener('click', async () => {
  btn.hidden = true;
  deferred.prompt();
  await deferred.userChoice;
  deferred = null;
});
addEventListener('appinstalled', () => { btn.hidden = true; });

This works in Chromium browsers. On iPhone and iPad, show short instructions instead: see install on iOS.

Step 7: test it

  1. Open DevTools, Application: check the manifest is parsed and the service worker is activated.
  2. Tick Offline in the Network panel and reload: you should see your page, or your offline page.
  3. Install the app from the address bar icon and open it.
  4. Change VERSION, reload, and check the update prompt appears.
  5. Run the PWA checker on your deployed URL.

You now have a real PWA. Next, read PWA performance and the PWA checklist.