【问题标题】:Angular PWA in ASP.Net Core Server - Serviceworker-Cache not workingASP.Net Core 服务器中的 Angular PWA - Serviceworker-Cache 不起作用
【发布时间】:2019-12-06 05:26:26
【问题描述】:

我创建了一个 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)));
        }

我正在尝试缓存来自 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
        });

【问题讨论】:

  • 为什么需要这条线? provider.Mappings[".webmanifest"] = "application/manifest+json";
  • 因为 ASP.Net 核心服务的清单文件内容类型错误(文本/html)
  • 清单文件与 Service Worker 缓存无关。您可以在没有清单文件的情况下注册服务工作者和缓存。

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


【解决方案1】:

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

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

【讨论】:

  • 我一直在努力让 Visual Studio 中的 Angular 8 .NET Core 2.2 项目作为 PWA 运行。我可以使用 CLI 和 http-server 运行一个基本的 angular PWA 应用程序,但我不知道如何让我的 .NET Core 项目工作。任何有帮助的文章或链接将不胜感激。一个示例项目会更好!
猜你喜欢
  • 2019-07-05
  • 2020-02-12
  • 1970-01-01
  • 2020-03-16
  • 2020-07-08
  • 2019-11-01
  • 2018-07-26
  • 1970-01-01
  • 2018-12-23
相关资源
最近更新 更多