Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - Why Fetch caches result?

I use Fetch to fetch data from server like this:

fetch(url, {
    method: 'GET',
    cache: 'no-cache'
});

It works fine without any caching sometimes, and sometimes it will get the same result(I'm sure data has changed and I've checked that fact using browser).

It seems like React Native is doing caching under the hood, but how can I disable it?

like image 874
user3087000 Avatar asked Sep 01 '26 16:09

user3087000


1 Answers

It should be fixed if you set the cache to reload.

Here are some uses of cache.

  // Download a resource with cache busting, to bypass the cache
  // completely.
  fetch("some.json", {cache: "no-store"})
    .then(function(response) { /* consume the response */ });

  // Download a resource with cache busting, but update the HTTP
  // cache with the downloaded resource.
  fetch("some.json", {cache: "reload"})
    .then(function(response) { /* consume the response */ });

  // Download a resource with cache busting when dealing with a
  // properly configured server that will send the correct ETag
  // and Date headers and properly handle If-Modified-Since and
  // If-None-Match request headers, therefore we can rely on the
  // validation to guarantee a fresh response.
  fetch("some.json", {cache: "no-cache"})
    .then(function(response) { /* consume the response */ });

  // Download a resource with economics in mind!  Prefer a cached
  // albeit stale response to conserve as much bandwidth as possible.
  fetch("some.json", {cache: "force-cache"})
    .then(function(response) { /* consume the response */ });

Note: Let me know if it doesn't work please and I will update my answer.

There is more about fetch on: hacks.mozilla and developer.mozilla

like image 82
Fatih Aktaş Avatar answered Sep 03 '26 23:09

Fatih Aktaş