【问题标题】:Angular : service worker configurationAngular:服务工作者配置
【发布时间】:2019-11-22 22:48:09
【问题描述】:

我正在尝试将 pwa 功能添加到在 Angular 8 上运行的网站。 我遵循了很多教程,官方和非官方的,我不明白我做错了什么。

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",
          "/*.min.js"
        ],
        "urls": [
          "https://fonts.googleapis.com/**"
        ]
      }
    }, {
      "name": "assets",
      "installMode": "lazy",
      "updateMode": "prefetch",
      "resources": {
        "files": [
          "/assets/**",
          "/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)"
        ]
      }
    }
  ]
}

我可以在 Chrome 开发控制台的应用程序选项卡中看到服务工作者已注册并正在运行。

但我有两个主要问题。

  1. 当我在离线模式下通过浏览器并重新加载页面时,我收到了 chrome 错误消息:

好像索引页无法服务,service worker 还在注册并运行。

我可以在在线模式下看到我从 Service Worker 获取索引页面,那么为什么它在离线模式下不起作用?

  1. 在在线模式下,我可以从网络选项卡中看到每个请求都被缓存,即使是来自 api 的请求。但是我没有在 ngsw-config.json 中配置 dataGroups 那么为什么会这样呢? 未在assetGroups的url数组中指定的外部js文件也是如此。

该网站可以在这里测试:https://dev.ecni.fr/

谢谢!

编辑: 在运行 Windows 的两台计算机上尝试使用不同的最新浏览器时出现同样的问题。 但是,在 mac 上使用 chrome 可以正常工作。 windows 电脑怎么了?

【问题讨论】:

  • Linwe,我也遇到了同样的问题,去clubup.it 时看到同样的问题,就像你一样。你找到解决方案了吗?
  • 我可以找到url_of_site/ngsw/state 的问题。我可以看到所提供的 index.html 的哈希值与缓存的哈希值不同。然后我对比了2个文件,发现是chrome扩展(卡巴斯基杀毒)在每个页面都加了一个小js代码导致的,然后文件不完全一样,hash不匹配。它导致服务人员无法做它的事情。

标签: angular service progressive-web-apps offline worker


【解决方案1】:

我可以在 Chrome 离线模式下看到您的网站,看起来不错。我还对比了我的ngsw-config.json 文件:

{
  "index": "/index.html",
  "assetGroups": [
  {
    "name": "app",
    "installMode": "prefetch",
    "updateMode": "prefetch",
    "resources": {
      "files": [
        "/index.html",
        "/manifest.json",
        "/browserconfig.xml",
        "/assets/images/favicon/**",
        "/*.css",
        "/*.js"
      ]
    }
  }, {
    "name": "assets",
    "installMode": "lazy",
    "updateMode": "prefetch",
    "resources": {
      "files": [
        "/assets/**",
        "/*.woff",
        "/*.woff2",
        "/*.svg",
        "/*.ttf",
        "/*.eot"
      ]
    }
  }]
}

除了manifest.json 之外,一切看起来都非常相似。你有什么新格式吗?我还将分享我的manifest.json 文件以完成,它可能有用:

{
    "name": "ClubUp! Volley Network",
    "short_name": "ClubUp!",
    "theme_color": "#00aeef",
    "background_color": "#ffffff",
    "display": "standalone",
    "scope": "/",
    "start_url": "/search?utm_source=homescreen",
    "dir": "ltr",
    "lang": "it",
    "orientation": "portrait",
    "description": "Cerchi un giocatore o una squadra? Fai un salto in ClubUp! per trovare il tuo team ideale. Provalo, è semplice da usare.",
    "related_applications": [],
    "prefer_related_applications": false,
    "icons": [
        {
            "src": "assets/images/pwa/icon-72x72.png",
            "sizes": "72x72",
            "type": "image/png"
        },
        {
            "src": "assets/images/pwa/icon-96x96.png",
            "sizes": "96x96",
            "type": "image/png"
        },
        {
            "src": "assets/images/pwa/icon-128x128.png",
            "sizes": "128x128",
            "type": "image/png"
        },
        {
            "src": "assets/images/pwa/icon-144x144.png",
            "sizes": "144x144",
            "type": "image/png"
        },
        {
            "src": "assets/images/pwa/icon-152x152.png",
            "sizes": "152x152",
            "type": "image/png"
        },
        {
            "src": "assets/images/pwa/icon-192x192.png",
            "sizes": "192x192",
            "type": "image/png"
        },
        {
            "src": "assets/images/pwa/icon-384x384.png",
            "sizes": "384x384",
            "type": "image/png"
        },
        {
            "src": "assets/images/pwa/icon-512x512.png",
            "sizes": "512x512",
            "type": "image/png"
        }
    ]
  }

如果您想比较网络选项卡中发生的情况,相关网站是:https://clubup.it/

【讨论】:

  • 感谢您的回答!我发现您的网站存在同样的问题(第一次成功加载后离线时出错,缓存了 api 请求),所以可能是计算机或 chrome 相关问题...我会尝试另一个。
  • webmanisfest 文件是由 Angular cli 生成的,我认为它是最近引入的一种新格式:developer.mozilla.org/en-US/docs/Web/Manifest
猜你喜欢
  • 1970-01-01
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
  • 2016-06-08
  • 1970-01-01
  • 2019-12-21
  • 2018-06-22
  • 2020-01-08
相关资源
最近更新 更多