Making a web app installable, without an app store
What a website needs to land on the home screen like an app, start in its own window and open offline too – and where to be careful.
Why bother
For many applications an app store listing is too expensive and too slow: every change has to be reviewed first, and users have to update. An installable web app still gets an icon on the home screen and opens without the browser bar.
We use this in our browser game and in internal tools. A new version is live as soon as it is on the server.
The manifest
A small JSON document describes name, icons, colours and the start address. Every page links it in its <head>
with <link rel="manifest" href="/site.webmanifest">
. display: standalone
gives the app its own window.
{
"name": "Forge & Pray",
"short_name": "Forge & Pray",
"start_url": "/play",
"display": "standalone",
"background_color": "#fdf6e3",
"theme_color": "#b45309",
"icons": [
{ "src": "/icons/icon-192.png", "sizes": "192x192", "type": "image/png" },
{ "src": "/icons/icon-512.png", "sizes": "512x512", "type": "image/png" }
]
}
The service worker
A service worker is a script that sits between page and network. On the first visit it puts the needed files into a cache and serves them when the network is gone. That way the app starts even without signal.
A version number in the cache name matters: every new version changes the name, and the service worker clears out the old cache. Otherwise users see an outdated version for weeks.
const CACHE = 'app-v42';
self.addEventListener('install', e =>
e.waitUntil(caches.open(CACHE).then(c => c.addAll(['/play', '/game/index.html']))));
self.addEventListener('activate', e =>
e.waitUntil(caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k))))));
self.addEventListener('fetch', e =>
e.respondWith(fetch(e.request).catch(() => caches.match(e.request))));
Your own install button
Chrome, Edge and Android report with the beforeinstallprompt
event that the page can be installed. Catch it, show your own button and call prompt()
on click. iPhone and iPad do not know the event; there a short hint about "Add to Home Screen" in the share menu helps.
Careful with signed-in areas
Whatever is in the cache is on the device. For pages with personal data we therefore leave the service worker out entirely and use only the manifest: the app opens in its own window, but nothing signed-in is stored.
- Cache only public, unchanging files (scripts, images, game data).
- Never store responses to signed-in requests in the cache.
- Serve the service worker over HTTPS; the browser will not register it otherwise.