如何使 Angular PWA 可安装?

How to make an Angular PWA installable?

我正在尝试使用 Angular 制作可安装的 PWA。

docs 之后,我 运行 ng add @angular/pwa 使我的应用程序成为 PWA。它运作良好。然后我使用 ng build 构建它并使用 http-server.

提供构建服务

Service Worker 似乎工作正常,它缓存资产,在我禁用互联网时继续工作等。

然而,此时我希望它询问用户是否要安装该应用程序,但它没有。我检查了 MDN guide and the app seems to meet all the requirements. On the other hand, this web.dev page 说要满足的安装标准之一是满足给定的用户参与启发式。

所以我不知道安装提示未显示的原因是什么,它可以是以下任何值:

如果有帮助,这里有一些相关文件:

manifest.webmanifest

{
  "name": "SiAQ",
  "short_name": "siaq",
  "description": "Aplicativo móvel do Sistema de Atendimento aos Queimados (SiAQ)",
  "theme_color": "#1976d2",
  "background_color": "#fafafa",
  "display": "standalone",
  "scope": "./",
  "start_url": "./",
  "icons": [
    {
      "src": "assets/icons/icon-72x72.png",
      "sizes": "72x72",
      "type": "image/png",
      "purpose": "maskable any"
    },
    {
      "src": "assets/icons/icon-96x96.png",
      "sizes": "96x96",
      "type": "image/png",
      "purpose": "maskable any"
    },
    {
      "src": "assets/icons/icon-128x128.png",
      "sizes": "128x128",
      "type": "image/png",
      "purpose": "maskable any"
    },
    {
      "src": "assets/icons/icon-144x144.png",
      "sizes": "144x144",
      "type": "image/png",
      "purpose": "maskable any"
    },
    {
      "src": "assets/icons/icon-152x152.png",
      "sizes": "152x152",
      "type": "image/png",
      "purpose": "maskable any"
    },
    {
      "src": "assets/icons/icon-192x192.png",
      "sizes": "192x192",
      "type": "image/png",
      "purpose": "maskable any"
    },
    {
      "src": "assets/icons/icon-384x384.png",
      "sizes": "384x384",
      "type": "image/png",
      "purpose": "maskable any"
    },
    {
      "src": "assets/icons/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png",
      "purpose": "maskable any"
    }
  ]
}

ngsw-config.json

{
  "$schema": "./node_modules/@angular/service-worker/config/schema.json",
  "index": "/index.html",
  "assetGroups": [
    {
      "name": "app",
      "installMode": "prefetch",
      "resources": {
        "files": [
          "/favicon.ico",
          "/index.html",
          "/manifest.webmanifest",
          "/*.css",
          "/*.js"
        ]
      }
    },
    {
      "name": "assets",
      "installMode": "lazy",
      "updateMode": "prefetch",
      "resources": {
        "files": [
          "/assets/**",
          "/*.(svg|cur|jpg|jpeg|png|apng|webp|avif|gif|otf|ttf|woff|woff2)"
        ]
      }
    }
  ]
}

看起来你做的一切都很好。 @angular/pwa 应该可以直接完成所有工作。我唯一的猜测是你正在构建你的应用程序,而不是在 prod 模式下。

尝试按照以下步骤操作:

  1. ng build --prod
  2. cd dist/<proj_name>
  3. http-server

显然,我的 PWA 收到警告是因为 manifest.webmanifest 我将图标的用途设置为 "maskable any"。只需从所有图标中删除此 属性(恢复为默认值)即可正常工作。

您可以通过打开开发工具并转到“应用程序”->“清单”来检查您的 PWA 是否一切正常。