Install

The Install controller handles the installation flow of a Progressive Web App (PWA). It listens for browser installation events, detects if the app is already installed, and provides an easy way to trigger the "Add to Home Screen" prompt when supported.

This component is essential for:

  • Providing a custom install button or banner

  • Detecting whether the app is already installed

  • Tracking installation events and user behavior

  • Creating a seamless installation experience

  • Showing/hiding install prompts based on installation status

Browser Support

The installation prompt API (beforeinstallprompt event) is supported in Chromium-based browsers (Chrome, Edge, Opera). Other browsers may use different installation mechanisms. The controller gracefully handles all scenarios.

circle-info

For the PWA to be installable, it must meet PWA installation criteria: valid manifest, HTTPS, service worker, and appropriate icons.

Usage

Basic Install Button

<div {{ stimulus_controller('@pwa/install') }}>
    <button
        id="install-btn"
        {{ stimulus_action('@pwa/install', 'install', 'click') }}
        style="display:none;"
    >
        Install App
    </button>
</div>

<script>
    document.addEventListener('pwa--install:not-installed', () => {
        document.getElementById('install-btn').style.display = 'block';
    });

    document.addEventListener('pwa--install:installed', () => {
        document.getElementById('install-btn').style.display = 'none';
    });
</script>

Install Banner with Animation

Install Card in App Settings

Smart Install Prompt with User Tracking

Parameters

None

Actions

install

Triggers the PWA installation prompt. This action will:

  1. Show the browser's native "Add to Home Screen" dialog

  2. Emit the installing event when the prompt is shown

  3. Emit either installed or cancelled based on user choice

circle-exclamation

Targets

None

Events

pwa--install:not-installed

Dispatched when the PWA is not currently installed but can potentially be installed.

No payload

Use this event to show install buttons or banners:

pwa--install:installed

Dispatched when the PWA is detected as already installed or running in standalone mode.

No payload

This event is emitted:

  • On page load if the app is running in standalone mode

  • After a successful installation

Use this to hide install prompts and show thank you messages:

pwa--install:installing

Dispatched when the user triggers the install() action and the installation prompt is shown.

No payload

Use this to track when users see the install prompt:

pwa--install:cancelled

Dispatched when the user dismisses the install prompt or an error occurs during installation.

No payload

Use this to handle installation cancellation:

Best Practices

  1. Don't be pushy: Show install prompts after users have engaged with your app

  2. Provide value: Explain benefits of installation (offline access, faster loading, etc.)

  3. Respect dismissals: Track how many times users dismiss the prompt and back off

  4. Check installation status: Always listen for the installed event to hide prompts

  5. Use appropriate timing: Show install prompts at natural break points in the user journey

  6. Make it optional: Never force installation; let users discover and install when ready

  7. Track metrics: Monitor installation rates and prompt dismissals to optimize your approach

Detection of Already Installed Apps

The controller automatically detects if your PWA is already installed by checking if the app is running in standalone mode. This detection happens immediately on page load, so you can hide install buttons for users who have already installed your app.

Common Scenarios

E-commerce Site

Show install prompt after user adds first item to cart:

Content Site

Show install prompt after user reads 3 articles:

Gaming App

Show install prompt after first game completed:

Last updated

Was this helpful?