, 6 tweets, 3 min read
Finally, when you are building a #PWA & setting up a service worker, this is how i feel your fetch event handler should be to handle a myriad of edge cases:

Part 1:
This part shows a CACHE FIRST LOOKUP. Also notice when the app is offline. Setup fetch to work with the HTTP cache
Part 2:
This part show a NETWORK NEXT LOOKUP. We detect errors with the fetch request first and return early. Then detect server-sent events and return too. Then split the by content type: html,css,js,svg responses gets put in the cache and json responses gets put in IndexedDB.
Notice this area: it helps us handle cache eviction gotchas that might hit us when developing a PWA

Don't worry the navigator object is very well accessible inside both Web Workers & Service Workers.

Notice how persist the cache after updating with `cache.put()`
Part 3:
This final part deals with error handling for when a fetch request fails (especially if we have a network error e.g. 502, 503, 504 status).

`getFromLocal` is a function that gets data from IndexedDB. `objectStoreNamesMap` is a dictionary that maps req url to store names.
The End!!!
Missing some Tweet in this thread? You can try to force a refresh.

Enjoying this thread?

Keep Current with Okechukwu Ifeora

Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!