I have registered my service worker by adding serviceWorker.register(); in my code.In create-react-app Api results are not cached.How do we cache Api result so we counld improve Offline support.
Asked
Active
Viewed 1,576 times
3
-
Do you use redux or mobx or apollo or something else to manage your app state? – Arseniy-II Nov 04 '19 at 04:58
-
I use Redux for state management. – Gokul Sajan Nov 04 '19 at 05:09
-
Check [redux-persist](https://github.com/rt2zz/redux-persist) – Arseniy-II Nov 04 '19 at 05:22
1 Answers
1
you can cache the network responses and can be served from it as below
window.addEventListener('fetch', function(event) {
event.respondWith(
caches.open('your-app').then(function(cache) {
return cache.match(event.request).then(function (response) {
return response || fetch(event.request).then(function(response) {
cache.put(event.request, response.clone());
return response;
});
});
})
);
});

Noushad
- 6,063
- 3
- 25
- 28

Eslam Abu Hugair
- 1,162
- 1
- 11
- 23
-
1Just a note, that is a very simple, minimal example. For a real production application you will need to have a much more sophisticated solution to manage what strategy is used for different resources, data, which includes how cached assets are invalidated etc. – Chris Love Nov 05 '19 at 03:20