sij
sij

Reputation: 1347

Service worker slower than network calls in PWA?

Can PWA based cache calls be slower than Network calls?

This is what I saw in one of the web app, for a JSON resource:

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

The file has no entry in Manifest.json, but in ngsw-manifest.json the file is mentioned in static.

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.

Upvotes: 2

Views: 1388

Answers (1)

Dmytro Olefyrenko
Dmytro Olefyrenko

Reputation: 359

Yes, if you have fetch event handler in a service worker, then it adds some latency for the network requests. 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.

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

Without a service worker, this network request happens immediately upon user navigation. 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. 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.

Upvotes: 1

Related Questions