【问题标题】:React Native - Why Fetch caches result?React Native - 为什么获取缓存结果?
【发布时间】:2019-03-12 16:51:27
【问题描述】:

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

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

有时无需任何缓存即可正常工作,有时会得到相同的结果(我确定数据已更改,并且我已使用浏览器检查过这一事实)。

React Native 似乎在后台进行缓存,但我该如何禁用它呢?

【问题讨论】:

    标签: react-native fetch


    【解决方案1】:

    如果您将缓存设置为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 */ });
    

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

    关于fetch 的更多信息请参见:hacks.mozilladeveloper.mozilla

    【讨论】:

    • 不,它不能解决问题。
    猜你喜欢
    • 1970-01-01
    • 2014-05-16
    • 2021-08-17
    • 1970-01-01
    • 2014-03-09
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    相关资源
    最近更新 更多