如何防止 Apollo 客户端查询在 IE11 中缓存?

Posted

技术标签:

【中文标题】如何防止 Apollo 客户端查询在 IE11 中缓存?【英文标题】:How to prevent an Apollo Client Query from caching in IE11? 【发布时间】:2019-04-11 03:53:53 【问题描述】:

We can disable caching globally in jQuery by doing:

$.ajaxSetup( cache: false );

我正在尝试在 Apollo Client 中找到类似的选项。我已经尝试过中间件和缓存控制标头,但没有运气。

是否可以像之前的 jQuery 选项那样以类似的方式(即,通过在查询字符串中附加时间戳)禁用缓存?

【问题讨论】:

【参考方案1】:

尝试将 fetchPolicy 设置为“no-cache”。像这样:

const defaultOptions = 
      watchQuery: 
        fetchPolicy: 'no-cache',
        errorPolicy: 'ignore',
      ,
      query: 
        fetchPolicy: 'no-cache',
        errorPolicy: 'all',
      ,
    

const client = new ApolloClient(
    link: concat(authMiddleware, httpLink),
    cache: new InMemoryCache(),
    defaultOptions: defaultOptions,

);

来自this link的代码。

【讨论】:

我试过了,IE11 仍然缓存请求(Edge 和任何其他浏览器都没有按预期缓存它)。我认为我们只有两个选择:1. 使用查询字符串时间戳 2. 使用 Cache-Control 标头(请参阅 ***.com/q/4303829/2009886)。我正在寻找一个选项来拦截我的 Apollo 查询并将时间戳注入查询字符串(或类似选项)。

以上是关于如何防止 Apollo 客户端查询在 IE11 中缓存?的主要内容,如果未能解决你的问题,请参考以下文章