Supercharge Your Website Using PWA: Deferred Installation
Tags: pwa, webdev, ux, javascript
Published: 2021-04-25 03:04:32

This is continuation of the [previous blog](https://dev.to/ruppysuppy/supercharge-your-website-using-pwa-installable-website-32i3) on making a website installable, you are highly encouraged to check it out before continuing.

# What is `deferred installation`?

`Installation Prompt`, makes it easy for users to install a **Progressive Web App** (`PWA`) on their mobile or desktop device. Installing a `PWA` adds it to a user's launcher, allowing it to be run like any other installed app. `Deferred installation` allows the developer to display the `installation prompt` only when the user performs certain action, like clicking a button or hitting the bottom of the page (something to indicate that they are **engaging with your site**, makes it more likely that the user will install the `PWA` when prompted to do so).

# Getting Started

We will be continuing from where we left off in the [previous blog](https://dev.to/ruppysuppy/supercharge-your-website-using-pwa-installable-website-32i3). But we require a small change in `index.html`

```html
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>PWA: Installable website</title>
    </head>
    <body>
        <button id="btn">Click Me</button> <!-- CHANGE: BUTTON ADDED -->
    </body>
    <script>
        if ('serviceWorker' in navigator) {
            navigator.serviceWorker.register('/service-worker.js');
        } else {
            console.log("Service worker is not supported");
        }
    </script>
</html>
```

# Setting up `deferred installation`

To set up `deferred installation` we need to add a script.

`script.js`

```javascript
let deferredPrompt;

// Storing the installation prompt
window.addEventListener("beforeinstallprompt", (event) => {
	deferredPrompt = event;
});

// Displaying the prompt on button click
const btn = document.getElementById('btn');
btn.addEventListener("click", () => {
	if (!deferredPrompt) return
	deferredPrompt.prompt();
});
```

Link the script in `index.html`.

```html
<script src="script.js"></script>
```

Voila! That's all you need to defer the `Installation Prompt`.

**NOTE:** You can display the `Installation Prompt` only on user interaction like: *click*, *scroll*, *form submit*, etc.

# Project using this Implementation

**Smartsapp** (`deferred prompt` on **Google OAuth** button click, **login** and **registration** )

Web-app: [https://smartsapp-ba40f.firebaseapp.com](https://smartsapp-ba40f.firebaseapp.com/)

{% github https://github.com/ruppysuppy/SmartsApp %}

Finding **personal finance** too intimidating? Checkout my **Instagram** to become a [**Dollar Ninja**](https://www.instagram.com/the.dollar.ninja/)

# Thanks for reading

Reach out to me on:

-   [GitHub](https://github.com/ruppysuppy)
-   [LinkedIn](https://www.linkedin.com/in/tapajyoti-bose-429a601a0)
-   [Portfolio](https://tapajyoti-bose.vercel.app) (For freelance projects, reach out through the contact form)