Angular caching - should we use service worker?

Viewed 84

I am new to angular caching, and while navigating the docs I found that we may use service worker which uses localStorage, and we could use directly the localStorage without the service worker

What is a service worker, and in what scenarios we should use it or avoid using it?

What are the advantages of using service workers besides offline capabilities?

Thanks!

1 Answers

There are tons of document on the subject but to keep it simple : a service worker is a JS script that run in another thread than your main script and can act as a proxy between all your website's calls (ressources, APIs etc...) and your server.

By using a service worker, you can indead cache ressources but also REST responses. To do this, the service worker can cache response into the localStorage as you mentionned : the implementation is up to you.

Depending on what you want to cache, you could cache it directly into the localStorage without the need of a service worker as it is simplier (in a service for example).

Related