Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native fetch API aggressive cache

I'm using fetch API for interacting with server in my [email protected] app, but facing with quite aggressive caching.

Call which I proceed can be expressed like:

fetch(route + '&_t=' + Date.now(), {
  headers: {
    'Cache-Control': 'no-cache',
    'Accept': 'application/json, text/plain, */*',
    'Content-Type': 'application/json',
    'Custom-Auth-Header': 'secret-token'
  },
  method: 'POST',
  body: data,
  cache: 'no-store'
})

In IOS simulator response get cached for 15-20 mins, can be cleared via Reset Content and Settings.

In result I just don't want to have any cache for any of my calls (including GET requests).

I tried all options which I know in order to avoid caching, but seems there is something else, any help would be very appreciated!

like image 592
Anton Kononenko Avatar asked Jul 30 '16 06:07

Anton Kononenko


People also ask

Does fetch API work in react native?

React Native provides the Fetch API for your networking needs. Fetch will seem familiar if you have used XMLHttpRequest or other networking APIs before. You may refer to MDN's guide on Using Fetch for additional information.

How do I load images faster in react native?

React Native FastImage is a quick way to load images in React Native. All loaded pictures are aggressively cached by FastImage. You may add your own auth headers and preload pictures to your requests. GIF caching is also supported by react-native-fast-image.

Does react native image cache?

react-native-fast-image is a performant React Native component for loading images. FastImage aggressively caches all loaded images. You can add your own request auth headers and preload images. react-native-fast-image even has GIF caching support.

What is fetch Return react?

The simplest use of fetch() takes one argument — the path to the resource you want to fetch — and does not directly return the JSON response body but instead returns a promise that resolves with a Response object.


1 Answers

It turned out caching was caused by the server setting the session cookie. iOS/Android handles cookies automatically so it was used with every fetch call.

The solution was to delete all the cookies on logout using the https://github.com/joeferraro/react-native-cookies library.

like image 134
Peter Machowski Avatar answered Nov 12 '22 10:11

Peter Machowski