Free Tool

beforeinstallprompt Boilerplate

Catching the beforeinstallprompt event is the only way to trigger a custom Add to Home Screen (A2HS) prompt. Select your framework below to instantly generate the required event listener boilerplate.

1. Configure Stack
2. Copy Boilerplate

Add to Home Screen FAQ

What is the beforeinstallprompt event?

The beforeinstallprompt event is fired by the browser right before it shows the default "Add to Home Screen" mini-infobar. By intercepting this event and calling preventDefault(), you cancel the default browser behavior and save the event object. You can then use this saved object to trigger the install prompt at a better time (e.g., when a user clicks a custom "Install App" button in your UI).

Why doesn't this code work on iOS Safari?

Apple has not implemented the beforeinstallprompt event in iOS Safari. Users can still install your PWA to their iPhone home screen, but they must do it manually by tapping the "Share" icon and selecting "Add to Home Screen". You cannot programmatically trigger the prompt on iOS via JavaScript. You must build a custom tooltip instructing them to use the share menu.

Can I trigger the install prompt automatically on page load?

No. Modern browsers strictly require a user gesture (like a click or tap) to trigger the .prompt() method on the saved event. If you try to fire it automatically on page load without user interaction, the browser will block your A2HS attempt and throw an error in the console to prevent spammy popups.

Why is the event not firing on desktop Chrome?

The event only fires if your Progressive Web App meets the browser's installability criteria. This usually requires a valid manifest.json file (with a name, icons, and start_url), a registered Service Worker with a fetch handler, and the app must be served over HTTPS. Additionally, if the app is already installed on the device, the event will not fire again.

What happens if the user dismisses the prompt?

When you call deferredPrompt.prompt(), you can await deferredPrompt.userChoice to check the user's decision. If the outcome === 'dismissed', they chose not to install it. Keep in mind that a beforeinstallprompt event object can only be used once. Regardless of whether they accepted or dismissed it, you must throw that specific event away. The browser will fire a new one on subsequent visits based on its own internal heuristics.

How does Progressier handle Add to Home Screen?

Instead of managing state variables and writing fallback UI components for iOS users, Progressier handles the entire installation lifecycle automatically. It provides beautiful, cross-platform install widgets, detects the user's browser, and even provides visual step-by-step instructions for iOS Safari users, ensuring maximum conversion rates without writing any boilerplate code.

Your app here

Tired of writing manual A2HS logic?

Progressier automatically handles cross-platform install prompts (yup, including iOS).

Generate PWA See plans