ASP.Net Core 服务器中的 Angular PWA - Serviceworker-Cache 不工作

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

我创建了一个 Angular PWA 并将其移动到 ASP.Net Core Spa - 应用程序中。当通过“npm http-server”提供和安装时,我可以离线运行它。

当我在 ASP.Net 服务器中运行它时,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
        });

最佳答案

我意识到是在 Debug模式下从 Visual Studio 使用的 chrome 浏览器导致了这个问题。当我从 VS 启动浏览器时,serviceworker 没有得到详细注册。当我复制 URL 并将其粘贴到新的 chrome 实例时,错误消失了。

在我使用“清除站点数据”-“清除存储”选项卡中的按钮后,缓存也起作用了。

关于ASP.Net Core 服务器中的 Angular PWA - Serviceworker-Cache 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57244231/

相关文章:

azure - 在 azure webapp 上应用 appsettings.json

javascript - self.skipWaiting() 在 Service Worker 中不起作用

angular - 批处理文件运行 npm start

c# - IOptions 不适用于 TinyIOC/NancyFX

garbage-collection - ASP.NET Core Web 应用程序 (.NET Framework) 中 app.config 文件的用途是什么?

Angular 推送通知 - 避免存储重复的订阅对象

javascript - firebase web 通知 - 如何在 firebase-messaging-sw.js 中使用 document 或 window.document

angular - 在 Angular/Electron 应用程序上将图像转换为 base64 URI

angular - Mapbox-gl.js map 在加载事件的 angular-cli 项目中未定义

javascript - MomentJs:如何在 Typescript 中将字符串转换为日期?