pwa-install README

The project's README from github.com/khmyznikov/pwa-install. As one markdown file: README.md.

Published on NPM npm

<pwa-install>

New to PWAs? Unsure how to create a Web App? Check out these resources for a quick start: PWA Mozilla Docs, PWA Google Docs, PWA Builder - test and publish your PWA

Installation dialog for Progressive Web Application (PWA) and Add to Home Screen/Dock dialog for Web Apps. This offers an enhanced user experience and addresses the absence of native dialogs in certain browsers (Safari, Firefox, Opera, etc.). 39kB brotli compressed bundle. Translation/localization is supported.

✨ In-app browsers instruction support!
✨ iOS/iPadOS/MacOS 27+ support for native look and feel!
✨ Web Install API support for chromium browsers v155+

Use it as Web Component with any modern framework. No polyfill is required.

⚡Should work with any other modern framework or just vanila js as web component.

Demo

      iOS default       Install instruction       App gallery       
iOS example default iOS example install instruction iOS example gallery
MacOS 14-27+
macos_default
    iPadOS     Instruction
iPadOS example default iPadOS install instruction
In-App Detection Instagram/etc
iOS Instagramm browser iOS Telegram browser
Android Firefox/ Opera/ Others   App gallery  
Android example default Firefox Opera and others Android gallery
Chrome  App Gallery  
Chrome example default Chrome example gallery

Install

npm i @khmyznikov/pwa-install

Alternatively, you can use unpkg or esm.sh.

Import

import '@khmyznikov/pwa-install';

TS Config

"compilerOptions": {
  "moduleResolution": "Bundler",
  "types": ["dom-chromium-installation-events", "web-app-manifest"]
}

Use

<pwa-install></pwa-install>

Demo


Supported params

<pwa-install
  manual-apple
  manual-chrome
  disable-chrome
  disable-close
  use-local-storage

  install-description="Custom call to install text"
  disable-install-description
  disable-screenshots
  disable-screenshots-apple
  disable-screenshots-chrome
  manual-how-to

  disable-android-fallback

  manifest-url="/manifest.json"
  manifest-id="https://example.com/app-id"
  name="PWA"
  description="Progressive web application"         
  icon="/icon.png">
</pwa-install>

Parameter behavior

Make a good manifest file and don't use name/descr/icon params. Boolean attributes needs to be removed to act like "false"

On supported Chromium browsers, the component uses the Web Install API automatically after fetching the configured manifest. When manifest-url matches the current document's linked manifest, manifest-id is omitted, and the fetched manifest declares an id, the component uses the current-document navigator.install() signature. A different manifest or an explicit manifest-id uses navigator.install({ manifest, manifestId }). Relative manifest URLs are resolved against the current document. manifest-id is optional when the manifest declares a non-empty id.

The component continues to intercept and retain beforeinstallprompt as a legacy fallback. If both manifest-id and the fetched manifest's id are missing, the component skips Web Install and uses the retained prompt on the same click. If no compatible legacy prompt exists, pwa-install-fail-event reports a DataError. Other technical Web Install failures still require the next user click to use the retained prompt. A legacy prompt cannot install a different app.

Custom Styles

Only the Apple template supports styling, and only the tint color option is available as of today. More to come.

<!-- As attribute (JSON string) -->
<pwa-install styles='{"--tint-color": "#6366f1"}'></pwa-install>
// As property (object)
const pwaInstall = document.querySelector('pwa-install');
pwaInstall.styles = { '--tint-color': '#6366f1' };

// Or as attribute via JavaScript
pwaInstall.setAttribute('styles', JSON.stringify({ '--tint-color': '#6366f1' }));

Supported events

<script type="text/javascript">
  var pwaInstall = document.getElementsByTagName('pwa-install')[0];

  pwaInstall.addEventListener('pwa-install-success-event', (event) => {console.log(event.detail.message)});
  pwaInstall.addEventListener('pwa-install-fail-event', (event) => {
    console.log(event.detail.errorName, event.detail.fallbackAvailable);
  });
  pwaInstall.addEventListener('pwa-install-backend-event', (event) => {
    console.log(event.detail.backend, event.detail.reason);
  });
</script>

pwa-install-backend-event is dispatched immediately before an install backend is called. Its backend is web-install or beforeinstallprompt. When the legacy backend is selected, reason can be missing-manifest-id, web-install-unavailable, or web-install-failed. ⚠️ success/fail/choice events is Chromium only, iOS don't have them.

⚠️ If you see this message in the console:
Banner not shown: beforeinstallpromptevent.preventDefault() called. The page must call beforeinstallpromptevent.prompt() to show the banner.
This is not a error and not a bug. This means that the component successfully intercepted the beforeinstallprompt event.


Supported properties (readonly)

<script type="text/javascript">
  var pwaInstall = document.getElementsByTagName('pwa-install')[0];

  console.log(pwaInstall.isUnderStandaloneMode);
</script>

Supported methods

<script type="text/javascript">
  var pwaInstall = document.getElementsByTagName('pwa-install')[0];

  await pwaInstall.install();
</script>

The install method prefers navigator.install() whenever it is available and a manifest ID is configured or declared. A missing ID is detected before Web Install is called, allowing a retained beforeinstallprompt fallback to run on the same click. After a runtime Web Install failure, fallback still requires a second user click because both browser APIs consume transient user activation.

getInstalledRelatedApps is Chromium only, always empty on iOS.


Async mode

If you need to target Chromium browsers but you want to postpone component mounting, you can do it! But, need to capture beforeinstallprompt manually and pass it to the component's externalPromptEvent property(not attribute).

// capture event asap, better right in index.html script tag
window.addEventListener('beforeinstallprompt', (e) => {
  e.preventDefault();
  e.stopPropagation();
  e.stopImmediatePropagation();

  // save it somewhere
  window.promptEvent = e;
});

// later render the component on demand and pass event
document.getElementById("pwa-install").externalPromptEvent = window.promptEvent;

Supported localization

Translations available: EN, RU, TR, DE, ES, NL, EL, FR, SR, PL, ZH-CN/ZH-HK/ZH-TW, IT, UK, CS, NO/NB, PT, JA, SV, KO, KM, DA, VI, FA, HU, SK, CA-ES, HE, BG, AR, RO, TH

Language should change automatically based on browser settings. Please create the pull-request if you want to help with translation to your language. It's an easy process.

Contribution Guidelines


ROADMAP

buy me a coffee QR PayPal QR

One-time Backers ❤️

Patrick Voigt
Darren Debono
Angelo Fan
Chris Cherniakov
Moddy
Pavlo Hromadchuk
Leek Duck

pwa-install (khmyznikov/pwa-install)

Searching Database…

这是什么?一个给渐进式 Web 应用(PWA)用的安装对话框:它是一个 Web Component,在本身没有原生安装提示的浏览器(比如 Safari、Firefox 或 Opera)里显示“安装应用”或“添加到主屏幕/程序坞”的提示。
它是框架还是库?是库,不是框架:它以单个 Web Component 的形式提供,可以放进任何页面或框架(文档里提供了 React 的用法),而且不需要 polyfill。整个包压缩后只有 39kB(brotli 压缩)。
怎么把它加到我的网站上?通过 npm 安装这个包并作为模块导入,或者直接从 unpkg 或 esm.sh 这类 CDN 加载,然后把组件加到页面里,并让它指向你的 web app manifest。
我的应用需要准备什么才能让对话框正常工作?需要一份合格的 web app manifest——作者建议好好编写 manifest 文件,而不是通过组件参数去覆盖名称、描述或图标。不需要 polyfill,也不依赖某个特定框架。
它在所有浏览器里表现都一样吗?不一样。在 Chromium 内核浏览器上,它使用 Web Install API(v155+)或拦截 beforeinstallprompt 事件;在苹果设备上,它会在 iOS/iPadOS/macOS 27+ 上以原生样式显示手动安装步骤;而成功/失败事件只在 Chromium 上才会触发。
我能控制对话框什么时候出现吗?可以。像 manual-apple 和 manual-chrome 这样的参数让你可以用 showDialog() 自己触发它;其他参数还能禁用 Chrome 的自动拦截、让对话框无法关闭,或者把用户“不再询问”的选择记在 local storage 里。
我能调整它的样式来配合我的应用吗?目前基本不行:只有苹果模板支持样式定制,而且到今天为止也只提供了一个主题色选项。
它能显示我的用户的语言吗?能——内置了 30 多种翻译(EN、RU、DE、ES、ZH、JA、KO、AR 等),会根据浏览器的语言设置自动选择。
我的代码怎么知道应用装好了?组件会触发 pwa-install-success-event、pwa-install-fail-event 和 pwa-user-choice-result-event 这样的 DOM 事件,还会暴露 isInstallAvailable 这类只读状态;其中成功/失败/选择事件只在 Chromium 上有效。
Creator
khmyznikov
Size
11.4 MB
Stars
956
Age
5y
Commits · 90 Days