npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

add-to-homescreen-control

v0.6.0

Published

Library that allows you to control new Add to Home Screen prompt behavior

Downloads

25

Readme

Add to Home Screen banner control

This library allows you to programatically show Add to Home Screen banner. This feature is available from Chrome 68 but the library works on the older versions aswell. It's recomennded to read useful information section before using this library to avoid common problems and gotchas.

How to use this library

Available methods

  • enable(): void - enables capturing of beforeinstallprompt event and all the librarys behavior. You need to invoke this function as fast as you can to use all the other parts of this library.

  • prompt(): Promise - shows the Add to Home Screen banner (if the criteria are met) and resolves when user decides either to install or decline on Add to Home Screen. The resolved Promise value is an object with two values:

    • outcome: string - outcome of the homescreen installation. Contains accepted if the app was succesfully installed, otherwise contains dismissed string.
    • platform: string - platform used for installation
  • canPrompt(): boolean - returns true if the ATHS criteria are met and prompt() method can be fired

Installation and usage example

  1. Install the library
npm i add-to-homescreen-control -d
  1. Enable it as soon as you can
import ATHS from 'add-to-homescreen-control'
  
ATHS.enable()
  1. Show the Add to Home Screen banner whenever you want to with prompt() method. The banner will appear only if the criteria are met. You can handle unmet criteria in two ways:
  • make use of the fact that prompt() returns a Promise (recommended):
ATHS.prompt()
  .then(({ outcome }) => console.log('user interacted with ATHS banner with outcome of', outcome))
  .catch(err => console.log(err))
  • or you can use canPrompt property to check if the prompt() method is available:

Useful information

Browser support

Add to Home Screen behavior is supported by most of the modern browsers but the banner encouraging users to install the app will be displayed only in Google Chrome. The process of adding website to the homescreen is different for every browser and this library is focused mostly on Chrome since the ATHS banner is present only there.

Add to Home Screen criteria (Chrome)

Before ATHS prompt can be shown the following criteria needs to be met:

  • The web app is not already installed
  • The user has interacted with the domain for at least 30 seconds
  • Your index.html includes Web App Manifest with at least following propertiesshort_name, name, start_url, icons (at least 192px and 512px), display (standalone, fullscreen, minimal-ui)
  • You have registered Service Worker with a fetch event handler (can't be a dummy one)

You can succesfully use librarys prompt() method only if this criteria are met. Otherwise it'll end up as a rejected promise.

You can use canPrompt variable to detect if the criteria are already met.

This criteria are different for other browsers.

Good practices and gotchas

  • It's a good practice to show ATHS prompt in a context and as a response to user gesture (for example button click). Showing it right after it's available is an anti-pattern.
  • banner can be shown only once per navigation route.
  • Once user click x on a banner it can't be shown again for a significant amount of time (currently ~3 months)