似乎只有当应用程序在 Angular 8 中编译用于生产(PWA)时,才会导致浏览器从服务器接收 SSE 事件时出现问题。
如果我在开发模式下运行应用程序(只需使用ngserve
),事情就会按预期工作。所以我最初的想法是 PWA 在某种程度上是问题的根源。
我尝试在 ngsw-config.json
中排除 api /v1/events
上的缓存,但没有区别:
{
"name":"api",
"urls": ["/api/v1/**"],
"cacheConfig": {
"maxSize": 0,
"maxAge": "0u",
"strategy": "freshness"
}
}
有人遇到过类似的问题并成功解决了吗?
最佳答案
我想我已经明白了。我只是添加了对我的 API /v1/events
的调用绕过 Service Worker 的查询参数:/v1/events?ngsw-bypass=true
.
我在 Angular web page 上找到了答案
In some cases, you may want to bypass the service worker entirely and let the browser handle the request instead. An example is when you rely on a feature that is currently not supported in service workers (e.g. reporting progress on uploaded files). To bypass the service worker you can set ngsw-bypass as a request header, or as a query parameter. (The value of the header or query parameter is ignored and can be empty or omitted.)
关于Angular 8 PWA 应用程序将服务器发送的事件永远置于挂起状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65462397/