PWA-Ready App
Which case you are in
| App | Do this |
|---|---|
| No notifications yet. Recommended. | Follow this skill. Ship the service worker now, even empty: notifications go into it later. |
| Already sends Internet Identity notifications | Follow this skill, then “Installing an app that already notifies”. |
Hosting is the static-site skill, sign-in the user-sign-in skill.
Mistakes That Break Your Build
/sw.jsanswered by the SPA fallback. A missing or misnamed file is served asindex.htmlby/* /index.html 200, and registration fails withThe script has an unsupported MIME type ('text/html'). The file must exist in the deployeddirat exactly the registered path.- Service worker in a subfolder, or with a hashed name. A worker controls only its own path and below. Emit
/sw.jsat the root. importin a service worker. Bundle it into one IIFE file;type: 'module'service workers are not supported everywhere PWAs install.- Installing from more than one origin. An installed app belongs to the origin it was installed from:
<id>.icp.netand your custom domain are two apps. Link each frontend’s manifest only on the one origin users should install it from. - Expecting the sign-in to carry into an iOS Home Screen app. Apps installed from Safari keep their own storage, so the user signs in again inside them. Chromium installed apps stay signed in.
- Storing something for the Chromium installed app alone. An app installed from Chrome or Edge shares storage with the site: whatever it stores also shows in a browser tab on that origin, and stays after the app is uninstalled.
The manifest
public/manifest.webmanifest:
{
"name": "Example App",
"short_name": "Example",
"id": "/",
"start_url": "/",
"scope": "/",
"display": "standalone",
"background_color": "#0f1115",
"theme_color": "#0f1115",
"description": "One sentence on what the app does.",
"screenshots": [
{ "src": "/screenshots/phone.png", "sizes": "1080x2340", "type": "image/png", "form_factor": "narrow" },
{ "src": "/screenshots/desktop.png", "sizes": "2560x1600", "type": "image/png", "form_factor": "wide" }
],
"shortcuts": [
{ "name": "New message", "url": "/new", "icons": [{ "src": "/icons/new-96.png", "sizes": "96x96" }] }
],
"icons": [
{ "src": "/icons/192.png", "sizes": "192x192", "type": "image/png" },
{ "src": "/icons/512.png", "sizes": "512x512", "type": "image/png" },
{ "src": "/icons/maskable-512.png", "sizes": "512x512", "type": "image/png", "purpose": "maskable" }
]
}
In index.html:
<link rel="manifest" href="/manifest.webmanifest" />
<link rel="apple-touch-icon" href="/icons/180.png" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<meta name="theme-color" content="#0f1115" media="(prefers-color-scheme: dark)" />
<meta name="theme-color" content="#ffffff" media="(prefers-color-scheme: light)" />
- Use your app’s own colors, not white.
#0f1115above stands for your app’s background. With a dark and a light mode, thetheme-colormeta tags and the page’s CSS follow the user’s scheme, but the manifest takes one value: give it the background of the scheme most users see. It colors the splash screen and, in an app installed from Chrome on Android, the navigation bar, in both schemes. - Pick the manifest colors before users install. An app installed from Chrome on Android keeps them until Chrome updates it on its own schedule; to see a change at once, uninstall and install again.
- iOS takes the Home Screen icon from
apple-touch-icon(180x180, no transparency), not the manifest. - Keep
idfixed: it is the installed app’s identity. - A maskable icon keeps its content in the central 80%.
descriptionandscreenshotsmake Chrome’s install dialog richer, on Android and desktop. Give each screenshot aform_factorofnarrow(phone) orwide(desktop), as PNG or JPEG, 320 to 3840 pixels per side; Chrome uses at most 8.shortcutsare the app’s entry points from its icon: long-press on Android, right-click on the Dock or taskbar on desktop. The chat example’s “New message” is one. Keep them to the app’s few most common actions.
The service worker
src/sw.ts:
/// <reference lib="webworker" />
declare const self: ServiceWorkerGlobalScope;
export {}; // a module, so `self` above does not clash with the global (TS2451)
self.addEventListener('install', () => {
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(self.clients.claim());
});
Bundle it with the page’s bundler, so it reads the same configuration, such as the AuthClient options from a module the page shares. With Vite, a second build in library mode puts it at the root of dist:
// vite.sw.config.ts, run after the page's build: vite build && vite build -c vite.sw.config.ts
import { defineConfig } from 'vite';
export default defineConfig({
build: {
lib: { entry: 'src/sw.ts', formats: ['iife'], name: 'sw', fileName: () => 'sw.js' },
outDir: 'dist',
emptyOutDir: false,
},
});
Register it once from the page:
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js');
}
Offline (optional)
Add a fetch handler when the app should launch without a network. Fetch index.html from the network first, so a deploy is never hidden behind a stale shell, cache the hashed assets, and leave canister calls alone. The first page load happens before the worker exists, so cache the shell and the files it loads when the worker installs, or the first launch offline after installing finds nothing. This finds only the files index.html names: a route chunk loaded lazily is cached the first time it loads under the worker. To have every chunk offline from the first launch, build with build.manifest: true, write the files Vite’s manifest lists into the worker at build time, and precache that list instead:
const CACHE = 'app-shell';
async function keep(request: Request | string, response: Response) {
if (response.ok) await (await caches.open(CACHE)).put(request, response);
}
self.addEventListener('install', (event) => {
event.waitUntil((async () => {
const shell = await fetch('/index.html');
const html = await shell.clone().text();
const assets = [...new Set(html.match(/\/assets\/[^"'\s)]+/g) ?? [])]; // Vite's hashed files
const cache = await caches.open(CACHE);
await cache.put('/index.html', shell);
await cache.addAll(assets);
})());
});
self.addEventListener('fetch', (event) => {
const { request } = event;
const url = new URL(request.url);
if (request.method !== 'GET' || url.origin !== self.location.origin) return; // canister calls go to the network
if (request.mode === 'navigate') {
event.respondWith(
fetch(request)
.then((response) => {
// Only the app's page is the shell: not a JSON file or the manifest opened in a tab.
if (response.headers.get('Content-Type')?.startsWith('text/html')) {
event.waitUntil(keep('/index.html', response.clone()));
}
return response;
})
.catch(async () => (await caches.match('/index.html')) ?? Response.error()),
);
} else if (url.pathname.startsWith('/assets/')) { // Vite's hashed file names
event.respondWith(
caches.match(request).then((cached) => cached ??
fetch(request).then((response) => { event.waitUntil(keep(request, response.clone())); return response; })),
);
}
});
An app whose users write while offline queues those writes and sends them when the network returns: on the online event, or with the Background Sync API in Chromium.
Make it feel native
Do all of these. An installed app that skips them looks like a web page in a frame: a white flash at launch, white bars around a dark app, and no way in but the browser menu.
Open fast: show the last data first
Canister calls are the slow part of starting an ICP app. Draw what the user saw last from a local cache, then refresh from the canister. For a chat app, that is the last open conversation:
// IndexedDB keeps a canister's answer as it is (bigint, Uint8Array, records); JSON cannot.
function lastViews(run) {
return new Promise((resolve, reject) => {
const open = indexedDB.open('last-views', 1);
open.onupgradeneeded = () => open.result.createObjectStore('views');
open.onerror = () => reject(open.error);
open.onsuccess = () => {
const db = open.result;
const tx = db.transaction('views', 'readwrite');
const request = run(tx.objectStore('views'));
tx.oncomplete = () => { db.close(); resolve(request.result); };
tx.onerror = () => { db.close(); reject(tx.error); };
};
});
}
async function openConversation(conversationId) {
const principal = (await authClient.getIdentity()).getPrincipal().toText();
const key = `${principal}:${conversationId}`;
const cached = await lastViews((views) => views.get(key));
if (cached) render(cached);
const fresh = await actor.get_conversation(conversationId);
render(fresh);
await lastViews((views) => views.put(fresh, key));
}
async function signOut() {
const principal = (await authClient.getIdentity()).getPrincipal().toText();
// Every conversation this user viewed.
await lastViews((views) => views.delete(IDBKeyRange.bound(`${principal}:`, `${principal}:\uffff`)));
// ...then sign out as usual.
}
Key the cache by principal and conversation: by conversation, so a link to one conversation never draws another first; by principal, and cleared on sign-out, so the next user of a shared browser never sees the previous user’s data at launch. Keep secrets out of it. A service worker can read and write the same store, for example after an inline reply.
Splash screen
- Android draws it from the manifest:
name,background_colorand the 512x512 icon. Setbackground_colorto the color of the app’s first screen, so launch flows into it without a flash. - iOS shows an
apple-touch-startup-imagethat matches the device’s screen size exactly, and a blank white screen without one. Generate one per iPhone and iPad screen size, portrait and landscape: the app’s background with its icon centered. Take the sizes from Apple’s current device list (https://developer.apple.com/design/human-interface-guidelines/layout#Specifications), not from memory, since new devices add sizes. Link each with its media query:
<link rel="apple-touch-startup-image" href="/splash/1179x2556.png"
media="(device-width: 393px) and (device-height: 852px) and (-webkit-device-pixel-ratio: 3) and (orientation: portrait)" />
Save them as palette PNGs: a flat background with an icon stays a few kilobytes each, which matters for a canister that embeds its assets in its wasm.
Edge to edge
Draw behind the system bars, so they show your app instead of a white strip. viewport-fit=cover (above) extends the page under them; then give the root your background and keep content clear of the bars with the safe area insets:
html { background: #ffffff; }
@media (prefers-color-scheme: dark) { html { background: #0f1115; } }
.top-bar { padding-top: env(safe-area-inset-top); }
.composer { padding-bottom: env(safe-area-inset-bottom); }
This covers iOS, browser tabs and the status bar. In an app installed from Chrome on Android, the page does not draw under the navigation bar: it shows the manifest’s theme_color, which is why that color must not be white for a dark app.
Install button
Chrome and Edge let the app offer its own Install button:
let installPrompt;
window.addEventListener('beforeinstallprompt', (event) => {
event.preventDefault();
installPrompt = event;
installButton.hidden = false;
});
installButton.addEventListener('click', async () => {
// A prompt can be shown once: hide the button until the next beforeinstallprompt.
const prompt = installPrompt;
installPrompt = undefined;
installButton.hidden = true;
await prompt.prompt();
});
Chrome fires beforeinstallprompt only once it considers the app installable, so no button in a fresh or automated profile is expected.
Safari has no such event, so show steps instead:
- iOS: open the menu (⋯), tap Share, tap View More, tap Add to Home Screen.
- macOS: File → Add to Dock.
Tell them apart from the user agent; iPadOS reports itself as a Mac with touch:
const isIOS = /iPhone|iPad|iPod/.test(navigator.userAgent) ||
(/Macintosh/.test(navigator.userAgent) && navigator.maxTouchPoints > 1);
const isMacSafari = !isIOS && /Macintosh/.test(navigator.userAgent) &&
/Safari/.test(navigator.userAgent) && !/Chrome|Edg|Firefox/.test(navigator.userAgent);
Hide both once the app runs installed:
const isInstalled = !matchMedia('(display-mode: browser)').matches || navigator.standalone === true;
Title bar (desktop Chrome and Edge)
Put the app’s own header in the title bar with the Window Controls Overlay API: add "display_override": ["window-controls-overlay"] to the manifest and lay out the bar with the titlebar-area-* CSS environment variables. It takes effect only in the installed app; in a browser tab nothing changes. Keep the header clear of the window controls, which sit left on macOS and right on Windows, and make it draggable except its buttons:
header {
padding-inline-start: env(titlebar-area-x, 0);
padding-inline-end: calc(100vw - env(titlebar-area-x, 0) - env(titlebar-area-width, 100vw));
app-region: drag;
}
header button { app-region: no-drag; }
A header split into panes pads its first pane at the start and its last at the end.
- Match the window controls. They take the
theme-color, so it must be the color of the pane behind them. When a wide layout puts a different pane there, such as a side rail, addtheme-colormeta tags with a width media query for that layout. - Follow the controls’ side. Where the layout depends on whether the controls sit left or right, read
navigator.windowControlsOverlay.getTitlebarAreaRect()and listen for itsgeometrychangeevent, which also fires when the user turns the overlay off.
Look it up as needed: https://developer.mozilla.org/en-US/docs/Web/API/Window_Controls_Overlay_API.
Sign-in from the installed desktop app
In an installed desktop app, Internet Identity opens as a browser tab behind the app’s window. Open it as a popup centered on the app instead, only when the app runs installed. Work out the position in the click handler, since the user may have moved the window since load. windowOpenerFeatures is fixed per AuthClient, so build a client for this one click, right before signIn() or requestNotificationConsent(), and dispose it afterwards:
// In the sign-in click handler:
const width = 600;
const height = 820;
const left = Math.round(screenX + (outerWidth - width) / 2);
const top = Math.round(screenY + (outerHeight - height) / 2);
const popupClient = new AuthClient({
...authClientOptions,
...(isInstalled && { windowOpenerFeatures: `popup,width=${width},height=${height},left=${left},top=${top}` }),
});
try {
await popupClient.signIn();
} finally {
popupClient.dispose();
}
Building a client is cheap and synchronous, so the popup still opens from the user’s click. Every client on the origin shares the sign-in, so the page’s other clients, and their subscribe() listeners, see it without being replaced. isInstalled is defined under “Install button”.
App badge
Show unread counts on the installed app’s icon, the counterpart of notifications for when the app is in view:
if (unreadCount > 0) navigator.setAppBadge?.(unreadCount);
else navigator.clearAppBadge?.();
In Safari, and on macOS in Chrome, the badge needs the app’s own notification permission: without it the call succeeds and no badge appears.
Headers
public/_headers, for the static-site canister. A canister serving its own assets sets these content types in its own code:
/sw.js
Content-Type: text/javascript
/manifest.webmanifest
Content-Type: application/manifest+json
Installing an app that already notifies
Users who do not install the app keep receiving notifications as before. In the installed app:
- Installed from Safari (iOS, macOS): Internet Identity’s own delivery does not reach the app. Its notifications show in Safari or, on iOS, in ICP Alerts where the user has it, and tapping one opens the site in Safari. They arrive in the app only once its own service worker subscribes.
- Installed from Chromium: notifications keep arriving; subscribing is optional.
- Installed from Chrome on macOS: notifications mostly show as Google Chrome’s, with Chrome’s icon. Where Chrome shows them under the app, macOS asks the user to allow the app’s notifications on its first one. Only notifications shown by the app’s own service worker can show under the app; Internet Identity’s stay Chrome’s.
The patch is “Delivering through your own service worker” in the user-notifications skill (@icp-sdk/auth 11.3.0 or later):
- Service worker:
pushshows whatauthClient.fetchNotificationPayload(event)answers,notificationclickcallsauthClient.markNotificationOpened(event), andpushsubscriptionchangecallsauthClient.subscribeToNotifications({ interactive: false }). - Page: consent at sign-in, then a setup button (also the banner) whose click handler makes the page’s first
subscribeToNotifications()call, and afterwards a switch for this app on this device. The service worker only renews that subscription, onpushsubscriptionchange. - Never call
pushManager.subscribeor make VAPID keys yourself.