I was looking at service worker practices and workbox.
There are many articles talking about precaching, workbox even provides special method precachingAndRoute()
for just that. I guess I understand the conceptual difference between precache and runtime cache, but what confuses me is why precache is treated so specially?
All articles I've read about precaching emphasize how it makes web app available when client is offline. Isn't that what cache (even it's not precache) is for? I mean it seems that runtime cache can also achieve just that if configured properly. Does it have to be precache to have web app work offline?
The only obvious difference is when the caches are to be created. Well, if client is offline, no cache can be created, no matter it is a precache or runtime cache, and if caches were created during last visit when client was online, how does it matter whether the cache to respond with for current visit was a precache or runtime cache?
Consider 2 abstract cases for compare. Say we have two different service workers, one (/precache/sw.js
) only does precache and the other (/runtime/sw.js
) only does runtime cache, where /precache
and /runtime
host same web app (meaning same assets to be cached).
Under what scenario, web app /precache
and /runtime
could run differently due to different sw setup?
In my understanding,
- If cache can not be created (e.g. offline on first visit), then precache and runtime cache shouldn't be any different.
- If precache can be created successfully (i.e. client is online on first visit), runtime cache should too. (Let's not go too wild with cases like the client may be online only for some certain moment, they still should be the same in my examples.)
- If cache are available, then precache and runtime cache have nothing to do, hence are still the same.
The only scenario I could think of when precache shows advantages, is when cache need to be updated on current visit, where precache makes sure current visit get up to date info. If this is the case, wouldn't a NetworkFirst runtime cache do just about the same? And still, there are nothing to do with "offline", what almost every article I've read about sw precaching would mention.
How online/offline makes precache a hero?
What did I miss here, what's so special about precaching?