My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Browser Session Based Service Worker Cache Busting Possible?

Arihant's photo
Arihant
·Mar 8, 2019

TL;DR: In need of a custom caching strategy other than the default ones ( cache first, network first, network only, cache only, etc...)

Is this possible?

Use some session identifier ( like browser session cookies ) for a limited amount of time ( say half an hour ), after which particular set of api end points on different origin, should fetch all results again from network ( instead of service worker cache ) and store it in new cache, deleting the old cache.

For example, say I'm on example.com. Page loads, service worker gets installed, and on going to example.com/posts/1, a fetch request was made to myexamplesapi.com/api/posts/1 to fetch all the posts with postId = 1.

  1. In the same browser session, for the next hour an hour, I want the service worker to continue to keep sending cached posts.

  2. After half an hour ( in the same browser session ), if the user comes on the example.com/posts/1 again, the request to fetch myexamplesapi.com/api/posts/1 should be made from the network instead of the cache, and then subsequently stored in a new cache, deleting the old one.

If anyone could give code snippet examples or any other reference, it'd be helpful.

A similar question asked on sw-precache library's github issues — github.com/GoogleChromeLabs/sw-precache/iss..