简体   繁体   English

在线时影响非 GET 请求的服务工作者 - @angular/pwa

[英]service worker affecting non-GET requests when online - @angular/pwa

I added a service worker to my project, and the GET requests work perfectly online and offline.我在我的项目中添加了一个 Service Worker,并且 GET 请求在线和离线都可以完美运行。
but i have a problem with non-GET requests when online.但是我在在线时遇到非 GET 请求的问题。
when i load the website using http-server (to activate the service worker),当我使用 http-server(激活 service worker)加载网站时,
it seems like the service worker intercepts them.似乎服务工作者拦截了它们。 and it never forwards the POST/DELETE request to the server in the same method.并且它永远不会以相同的方法将 POST/DELETE 请求转发到服务器。
instead of sending DELETE/POST requests to the server, it sends a GET request to the same path, (which is not helpful obviously) and of course the item is not removed:它不是向服务器发送 DELETE/POST 请求,而是向同一路径发送 GET 请求(这显然没有帮助),当然该项目不会被删除: 在此处输入图片说明

ngsw-config.json: ngsw-config.json: 在此处输入图片说明

localhost:3000 is the server. localhost:3000 是服务器。

when i'm loading the website using ng serve (without a service-worker) everything works perfectly.当我使用 ng serve (没有服务工作者)加载网站时,一切正常。

can anyone tell me how i can fix it?谁能告诉我如何解决它?

In order to get the fresh response from the cache you need to specify cache strategy (freshness or performance) in the ngsw-config.json file.为了从缓存中获得新鲜的响应,您需要在 ngsw-config.json 文件中指定缓存策略(新鲜度或性能)。

After assetGroups key add below dataGroups :assetGroups键后添加以下dataGroups

"dataGroups": [{
    "name": "GiveAnySuitableName",
    "urls": [
      "/**",
    ],
    "cacheConfig": {
      "strategy": "freshness",
      "maxSize": 100,
      "maxAge": "3d",
      "timeout":"3s"
    }
  }
]

Official angular PWA documentation: https://angular.io/guide/service-worker-config官方 angular PWA 文档: https : //angular.io/guide/service-worker-config

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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