简体   繁体   中英

Angular PWA in ASP.Net Core Server - Serviceworker-Cache not working

I created a Angular PWA and moved it into a ASP.Net Core Spa - Application. I can run it offline when served and installed via "npm http-server".

When I run it in the ASP.Net Server the Serviceworker loads with an error "Cannot read property 'put' of undefined" in ngsw-worker.js:

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

在此输入图像描述

I am trying to cache requests from the webapi and configured them in the ngsw-config.json - file

    {
      "$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)"
            ]
          }
        }
      ]
    }

In the first place the manifest.webmanifest file was served with wrong content-type, so I added a provider in my startup.cs. But this didn't help.

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

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

I realized that the chrome browser which is used from Visual Studio in the debugging mode causes the issue. The serviceworker doesn't get proberly registered when I launch the browser from VS. The error disappears when I copy the URL and paste it to a new instance of chrome.

Also the caching worked after I used the the "clear site data" - button in the "Clear storage" tab.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM