One PWA Install Flow for Chrome 158's Install API and Legacy beforeinstallprompt
One install button has to survive two eras: the legacy beforeinstallprompt event that Chromium ships today, and a reported direct install API in Chrome 158. This walkthrough builds a capability layer around both. You get the documented legacy lifecycle — the event firing once per page load, preventDefault keeping the token, prompt showing the dialog, userChoice resolving with accepted or dismissed, appinstalled firing on success — plus the failure modes that make install buttons lie: iOS Safari never fires the event at all, already-installed apps go silent, a dismissed dialog starts a suppression window, and duplicate listeners on SPA route changes overwrite the stored event with an already-spent one. The direct path gets the same treatment, with an important caveat stated up front: the Chrome 158 direct install claim comes from a single secondary report and is unverified. No method name, signature, or permission model is documented. So detection walks a short ordered list of candidate probes instead of hardcoding a name, and every code sample is shaped to survive an unknown signature rather than describing an API that has been called. Everything above the adapters sees one normalized outcome enum — accepted, dismissed, unavailable, error — so the button cannot get stuck in the wrong state, and an unreadable return value never gets claimed as a success. Unavailable covers already installed, cooldown active, unsupported environment, and unknown shape, and it never surfaces as an error toast. Full code and reasoning: https://hackyjs.com/posts/building-one-pwa-install-flow-for-chrome-158s-install-api-and-legacy-beforeinstallprompt-browsers