# beforeinstallprompt and missing install banners

> Install banner not shown? Save beforeinstallprompt, call prompt() on a user click, handle single-use events, and offer a manual install path on iOS Safari.

import Figure from '@components/Figure.astro';
import installFlowDiagram from '@assets/diagrams/install-flow.svg';

`beforeinstallprompt` is a `window` event that lets you defer Chromium's install prompt until a user clicks your install button. The browser fires it once the page meets its installability criteria. The handler calls `preventDefault()` to hold back the browser's own banner, keeps the `BeforeInstallPromptEvent`, and later calls `prompt()` from a click so the install dialog opens at a moment the page chooses. Browser-compat-data lists it from Chrome 44 (Android included), Edge 79, and Samsung Internet 5.0; Safari (iOS and macOS) and Firefox do not, and install only from their own menus (BCD `api.BeforeInstallPromptEvent`).

<Figure src={installFlowDiagram} alt="Flow diagram of the install prompt: once a site meets the installability criteria a Chromium browser fires beforeinstallprompt; the page calls preventDefault(), keeps the event and shows its own install button. On a user gesture it calls prompt(), the browser shows its install dialog and userChoice resolves to accepted, which fires appinstalled, or dismissed. Safari and Firefox never fire the event and install from a browser menu instead." caption="From installability criteria to appinstalled: the beforeinstallprompt flow in Chromium browsers." />

## Syntax

```js
window.addEventListener('beforeinstallprompt', (event) => {
  event.preventDefault();          // keep the browser's banner from showing now
  deferred = event;                // one-shot: usable for a single prompt()
});

await deferred.prompt();                                // from a user gesture
const { outcome, platform } = await deferred.userChoice;

window.addEventListener('appinstalled', () => { /* installed from any UI */ });
```

BCD `api.BeforeInstallPromptEvent.prompt` records the `{ outcome, platform }` return value from Chrome 76. In Chrome 44 through 75, `prompt()` resolves without a value, while `userChoice` is recorded from Chrome 44. Read `userChoice` when code covers the full event support range. `appinstalled` fires on `window` whether the install came from `prompt()`, from the address-bar icon, or from the browser menu.

## Members

`BeforeInstallPromptEvent` extends `Event` with two members; `appinstalled` is a plain `Event`.

| Member | Type | Description |
|---|---|---|
| `platforms` | `readonly string[]` | The install targets the browser offers, for example `["web"]` on desktop or `["web", "play"]` on Android when the manifest lists a Play app with `prefer_related_applications`. |
| `userChoice` | `Promise<{ outcome, platform }>` | Resolves after the dialog closes. `outcome` is `"accepted"` or `"dismissed"`; `platform` is the chosen entry of `platforms`, or `""` when dismissed. |
| `prompt()` | `Promise<void \| { outcome, platform }>` | Shows the browser's install dialog. BCD records the result object from Chrome 76 and an empty resolution in Chrome 44 through 75. Allowed once per event and only with transient user activation. |
| `preventDefault()` | inherited | Stops the browser from showing its own install banner (the Android mini-infobar) for this installability check. |

## When the event fires

The page must be served over HTTPS (or from `localhost`) and link a manifest with `name` or `short_name`, `start_url`, a `display` other than `browser`, and icons that include 192 px and 512 px sizes. Chrome dropped the service-worker requirement for installing from the browser menu in Chrome 108 on Android and Chrome 112 on desktop, supplying a default offline page; the Chrome team's post on that change notes that the heuristic behind `beforeinstallprompt` still looked for a `fetch` handler, so a page without a service worker can be installable from the menu and still not receive the event. Chrome also withholds the event while the app is already installed and until its engagement heuristics are met.

## Exceptions

`prompt()` rejects; nothing else in this API throws.

- `NotAllowedError`: `prompt()` was called without transient user activation, for example on page load or from a timer. Chromium's message is `The prompt() method must be called with a user gesture`.
- `InvalidStateError`: `prompt()` was called a second time on the same event, or the event's underlying banner request is no longer valid (the page navigated, or the browser already showed its own UI).

In Safari and Firefox the event does not fire, so there is no event object to misuse; code that assumes a prompt will come simply leaves its button hidden.

:::note
MDN states that `prompt()` must be called from the handler of a user action such as a button click, and that it may be called only once on a given `BeforeInstallPromptEvent` ([BeforeInstallPromptEvent.prompt()](https://developer.mozilla.org/en-US/docs/Web/API/BeforeInstallPromptEvent/prompt)). Chrome's [add to home screen changes](https://developer.chrome.com/blog/a2hs-updates) post adds that, from Chrome 68, the mini-infobar appears when the site meets the criteria whether or not the page calls `preventDefault()` on the event. The demo at `/demo/#install` defers the event, shows its button only after the event, and reads `userChoice` after the dialog closes.
:::

## Why the install banner is not shown

Calling `preventDefault()` does not display an install dialog. In a custom install flow, save the event, reveal your own install button, and call the saved event's `prompt()` from that button's click handler. This is the sequence in [Chrome's custom install guidance](https://web.dev/articles/customize-install). If you cancel the default handling but never call `prompt()`, the custom flow has no step that opens the dialog.

Separate these cases before changing the manifest:

| What you see | What to check | Next step |
| --- | --- | --- |
| The event fired, but the banner is not shown after `preventDefault()` | Is the event saved, and does a visible button call its `prompt()`? | Connect the saved event to the click handler in the example below. |
| `prompt()` rejects | Was it called from a user action, and has this event already been used? | Call it directly from the click handler; do not reuse a consumed event. |
| The event never fires | Does this browser implement the API, and does the site meet its installability requirements? | Check [installability criteria](/reference/installation/installability-criteria/) and [browser support](/compatibility/install-prompt/). |
| The visitor is using iOS Safari | Safari does not implement this event | Offer [Add to Home Screen instructions](/reference/installation/ios-add-to-home-screen/) instead of waiting for `beforeinstallprompt`. |

[MDN's `prompt()` reference](https://developer.mozilla.org/en-US/docs/Web/API/BeforeInstallPromptEvent/prompt) documents the user-action and single-use requirements. When the user dismisses a dialog, keep the install button hidden until a new event arrives. A rejected promise should be handled in your UI; retrying the same event in a timer is not a replacement for a new user action and a usable event.

The [install demo](/demo/#install) exposes the flow for comparison. Check that your page receives the event and reaches the click handler independently: an install button hidden by application state is a different problem from a browser that never supplies the event.

## Examples

Both examples run in the page; the service worker and manifest are assumed to be in place.

### Deferring the event and prompting from a button

The handler stores the event and reveals a button. The click handler calls `prompt()`, reads the outcome, and clears the stored event because it is single-use. The `appinstalled` listener hides the button for installs that came from the browser's own UI.

```js
let deferred = null;
const button = document.querySelector('#install');

window.addEventListener('beforeinstallprompt', (event) => {
  event.preventDefault();
  deferred = event;
  button.hidden = false;
});

button.addEventListener('click', async () => {
  if (!deferred) return;
  const promptEvent = deferred;
  deferred = null;                       // prevent a second call while awaiting
  await promptEvent.prompt();
  const { outcome, platform } = await promptEvent.userChoice;
  button.hidden = true;
  if (outcome === 'accepted') analytics.track('install_accepted', { platform });
});

window.addEventListener('appinstalled', () => {
  deferred = null;
  button.hidden = true;
});
```

Chrome applies a cool-down after dismissal before firing `beforeinstallprompt` again for the same site. Showing the button after a completed task keeps the page's single event for a moment when acceptance is more likely than at first paint.

### Detecting support and guiding Safari users instead

Safari does not fire the event. The page can still detect whether it is already running installed (`display-mode: standalone`, or the legacy `navigator.standalone` on iOS) and, if not, show Share-sheet instructions in place of a button that would not appear.

```js
const installed =
  window.matchMedia('(display-mode: standalone)').matches ||
  navigator.standalone === true;                        // iOS Safari only

const supportsPrompt = 'onbeforeinstallprompt' in window;

if (installed) {
  hideAllInstallUi();
} else if (!supportsPrompt) {
  // Safari and Firefox: no event will come. Explain the manual route.
  showHint(/iPhone|iPad/.test(navigator.userAgent)
    ? 'Tap Share, then "Add to Home Screen".'
    : 'Use your browser menu to install this app.');
}
// Chromium: wait for beforeinstallprompt before showing anything.
```

`'onbeforeinstallprompt' in window` is `true` in every Chromium browser that implements the event, so the hint branch runs only where no event will come; the Chromium branch keeps the install UI hidden until the event confirms installability.

## See also

- [Web Manifest Application Information and incubations: BeforeInstallPromptEvent](https://wicg.github.io/manifest-incubations/) (wicg.github.io)
- [Changes to the installability criteria](https://developer.chrome.com/blog/update-install-criteria) (developer.chrome.com)
- [Install prompt browser support](/compatibility/install-prompt/)
- [Installability criteria](/reference/installation/installability-criteria/)
- [Install prompt UX](/reference/installation/install-prompt-ux/)
- [iOS Add to Home Screen](/reference/installation/ios-add-to-home-screen/)
- [WebAPK](/reference/installation/webapk/)

- [Custom install banner state and fallback](/reference/installation/beforeinstallprompt-banner/)