React Native - 为什么获取缓存结果?

use*_*000 3 fetch react-native

我使用 Fetch 从服务器获取数据,如下所示:

fetch(url, {
    method: 'GET',
    cache: 'no-cache'
});
Run Code Online (Sandbox Code Playgroud)

有时它在没有任何缓存的情况下工作正常,有时会得到相同的结果(我确信数据已经改变,并且我已经使用浏览器检查了这一事实)。

看起来 React Native 正在幕后进行缓存,但我如何禁用它呢?

Fat*_*taş 5

如果您将缓存设置为 ,则应该修复它reload

以下是缓存的一些用途。

  // 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 */ });
Run Code Online (Sandbox Code Playgroud)

注意:如果它不起作用,请告诉我,我会更新我的答案。

还有更多关于fetchhacks.mozilladeveloper.mozilla