ASP.Net Core Server中的角度PWA-Serviceworker-Cache不起作用

时间:2019-07-28 19:19:38

标签: angular asp.net-core service-worker progressive-web-apps

我创建了一个Angular PWA,并将其移动到ASP.Net Core Spa-Application中。通过“ npm http-server”提供服务并安装后,我可以脱机运行它。

当我在ASP.Net Server中运行它时,Serviceworker在ngsw-worker.js中加载错误“无法读取未定义的属性'put'”:

        write(key, value) {
          return this.cache.put(this.request(key), this.adapter.newResponse(JSON.stringify(value)));
        }

enter image description here

我正在尝试缓存来自webapi的请求,并将其配置在ngsw-config.json-文件中

    {
      "$schema": "./node_modules/@angular/service-worker/config/schema.json",
      "index": "/index.html",
      "dataGroups": [
        {
          "name": "api-freshness",
          "urls": [
            "/odata/**"
          ],
          "cacheConfig": {
            "strategy": "freshness",
            "maxSize": 500,
            "maxAge": "3d",
            "timeout": "2s"
          }
        }
      ],
      "assetGroups": [
        {
          "name": "app",
          "installMode": "prefetch",
          "resources": {
            "files": [
              "/favicon.ico",
              "/index.html",
              "/*.css",
              "/*.js"
            ]
          }
        },
        {
          "name": "assets",
          "installMode": "lazy",
          "updateMode": "prefetch",
          "resources": {
            "files": [
              "/assets/**",
              "/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)"
            ]
          }
        }
      ]
    }

首先,manifest.webmanifest文件的内容类型错误,因此我在startup.cs中添加了一个提供程序。但这没有帮助。

        provider.Mappings[".webmanifest"] = "application/manifest+json";

        app.UseSpaStaticFiles(new StaticFileOptions() {
            ContentTypeProvider = provider
        });

1 个答案:

答案 0 :(得分:0)

我意识到在调试模式下从Visual Studio使用的Chrome浏览器会导致此问题。当我从VS启动浏览器时,serviceworker不会得到详细注册。复制网址并将其粘贴到chrome的新实例时,错误消失。

在使用“清除存储”标签中的“清除站点数据”-按钮之后,缓存也起作用。