简体   繁体   English

Service Worker 比 PWA 中的网络调用慢?

[英]Service worker slower than network calls in PWA?

Can PWA based cache calls be slower than Network calls?基于 PWA 的缓存调用能否比网络调用慢?

This is what I saw in one of the web app, for a JSON resource:这是我在其中一个 Web 应用程序中看到的 JSON 资源:

Cache cleared and called from service : 426 ms Called from Service worker(in dev tools / network size it is mentioned from Service worker) : 576 ms缓存清除并从服务调用:426 毫秒从服务工作者调用(在开发工具/网络大小中,它从服务工作者中提到):576 毫秒

The file has no entry in Manifest.json, but in ngsw-manifest.json the file is mentioned in static.该文件在 Manifest.json 中没有条目,但在 ngsw-manifest.json 中以静态方式提及该文件。

I am not sure if this thinking is wrong, but looks like reading from cache is slower than a network call for some resources here.我不确定这种想法是否错误,但看起来从缓存中读取比对某些资源的网络调用慢。

Yes, if you have fetch event handler in a service worker, then it adds some latency for the network requests.是的,如果您在 Service Worker 中有fetch事件处理程序,那么它会为网络请求增加一些延迟。 Since it intercepts all requests from your app.因为它拦截了来自您的应用程序的所有请求。 But the beauty and benefits of using it shines when you serve responses immediately from Cache Storage API, instead of going to the network.但是,当您立即从缓存存储 API 提供响应而不是通过网络提供响应时,使用它的好处和好处就会大放异彩。

You might want to read more about this topic in the article about service worker usage in Google Search https://web.dev/google-search-sw/#problem:-service-worker-overhead您可能想在有关 Google 搜索中的 Service Worker 使用的文章中阅读有关此主题的更多信息https://web.dev/google-search-sw/#problem:-service-worker-overhead

Without a service worker, this network request happens immediately upon user navigation.如果没有 service worker,这个网络请求会在用户导航时立即发生。 When a service worker is registered, it always needs to be started up and given a chance to execute its fetch event handlers, even when there's no chance those fetch handlers will do anything other than go to the network.当 service worker 被注册时,它总是需要被启动并有机会执行它的 fetch 事件处理程序,即使这些 fetch 处理程序除了进入网络之外没有机会做任何事情。 The amount of time that it takes to start up and run the service worker code is pure overhead added on top of every navigation.启动和运行 Service Worker 代码所需的时间是在每次导航之上添加的纯粹开销。

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

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