# PWA Guide for AI Agents (https://nma.sh/pwa.txt) Build installable, native-feeling Progressive Web Apps (iOS, Android, Desktop) with zero App Store friction. Synthesized from MDN's install-prompt guide and ryxxn/pwa-install-prompt patterns. ## 1. Installability Checklist (all 4 required) 1. **HTTPS origin** — automatic on nma.sh (every app gets its own HTTPS subdomain). 2. **Web App Manifest** (`/manifest.webmanifest`) linked in `
`: - Required fields: `name`, `short_name`, `start_url` (`"/"`), `display` (`"standalone"`), `background_color`, `theme_color`, and **at least two PNG icons (`192x192` and `512x512`)**. - Gotcha: Chrome/Android will NOT fire `beforeinstallprompt` with SVG-only icons. Generate real PNGs (or embed small valid PNGs in your app). 3. **iOS `` tags** (Safari ignores parts of the manifest): ```html ``` 4. **Root Service Worker** (`/sw.js`, served with `Content-Type: application/javascript; charset=utf-8`): ```js const CACHE = 'app-v1'; self.addEventListener('install', () => self.skipWaiting()); self.addEventListener('activate', (e) => e.waitUntil(self.clients.claim())); self.addEventListener('fetch', (e) => { if (e.request.method !== 'GET') return; e.respondWith( fetch(e.request).then((res) => { if (res.ok && e.request.url.startsWith(self.location.origin)) { const copy = res.clone(); caches.open(CACHE).then((c) => c.put(e.request, copy)); } return res; }).catch(() => caches.match(e.request)) ); }); ``` Register on load: `if ('serviceWorker' in navigator) navigator.serviceWorker.register('/sw.js');` ## 2. Minimal manifest.webmanifest Serve at `/manifest.webmanifest` with `Content-Type: application/manifest+json`: ```json { "name": "My App", "short_name": "MyApp", "start_url": "/", "scope": "/", "display": "standalone", "background_color": "#0b0b0a", "theme_color": "#0b0b0a", "icons": [ { "src": "/icon-192.png", "sizes": "192x192", "type": "image/png", "purpose": "any maskable" }, { "src": "/icon-512.png", "sizes": "512x512", "type": "image/png", "purpose": "any maskable" } ] } ``` ## 3. Cross-Platform Install Prompt (Android/Desktop + iOS) Key browser rules: - **Never spam on page load.** Keep the install button `hidden` by default; show it only when installable, or trigger an install modal from a user button/hash (like `#install`). - **Hide when already installed:** check `matchMedia('(display-mode: standalone)').matches || navigator.standalone === true`. - **Chromium (Android/Desktop Chrome, Edge, Samsung):** listen for `beforeinstallprompt`, call `event.preventDefault()`, save the event, reveal your Install button. On click, call `await deferred.prompt()` and read `result.outcome` (`"accepted"` or `"dismissed"`). **The event is single-use** — clear it after calling `.prompt()`. Also listen for `window.addEventListener('appinstalled', ...)` to hide the UI if the user installs from the browser bar. - **iOS / iPadOS Safari:** Apple does NOT support `beforeinstallprompt`. Detect iOS (`iPhone|iPad|iPod` or `MacIntel && maxTouchPoints > 1`), show your Install button, and on click open a small dialog explaining: 1. If in an in-app browser (Instagram, X, TikTok, etc.): *"Open this page in Safari first."* 2. Tap **Share** (square with up arrow) -> **Add to Home Screen** -> **Add**. - **iOS Push Notifications gotcha:** On iOS 16.4+, `Notification` and `PushManager` only exist *after* the app is added to the Home Screen and opened in standalone mode. Prompt users to install first, then request push permission inside standalone mode. Drop-in vanilla JS (~35 lines, zero third-party CDN dependencies): ```html ``` ## 4. Native Mobile Feel (5 CSS Rules) ```css html, body { min-height: 100dvh; /* avoids iOS URL bar jump */ overscroll-behavior-y: contain; /* stops pull-to-refresh bounce */ -webkit-tap-highlight-color: transparent; /* clean tap feedback */ } button, a { touch-action: manipulation; } /* removes 300ms tap delay */ .bottom-bar { padding-bottom: calc(12px + env(safe-area-inset-bottom)); } ``` ## 5. Ship on nmash (nma.sh) Deploy a static PWA folder or full-stack JS app (with built-in `db`, `kv`, and `auth`): - Tell your AI agent: `Read https://nma.sh/llms.txt and https://nma.sh/pwa.txt and deploy my PWA to nma.sh` - Or over SSH: `tar czf - -C dist . | ssh nma.sh deploy my-pwa` - Full platform contract: https://nma.sh/llms.txt