【问题标题】:set header apollo client in custom fetch在自定义获取中设置标题 apollo 客户端
【发布时间】:2021-02-09 18:19:51
【问题描述】:

我尝试在 fetch 属性中根据 apollo 客户端上的response.status 更新缓存控制标头,我有一个自定义的 fetch 方法,但是当我使用 headers.set() 时出现错误

TypeError: 无法在 'Headers' 上执行 'set': Headers 是不可变的

const customFetch = (uri, options) => {
  options.headers['Cache-Control'] = 'no-cache';
  return fetch(uri, options).then(response => {
    console.log(response.headers.get('cache-control'));
    try {
      if(response.status !== 301)
      response.headers.set('Cache-Control', getNewCacheValue());
    } catch (e) {
      console.log(e);
    }

  return new BatchHttpLink({
    fetch: customFetch,
    uri: uri
  });

【问题讨论】:

  • 您的 customFetch 函数有两个返回语句,是不是拼写错误??你能更新问题吗?

标签: javascript fetch apollo apollo-client


【解决方案1】:

您无法更改回复中的标题。另一种方法是使用可以控制缓存的未使用参数发出请求。

let url = 'www.google.com?xxx=' + getNewCacheValue()

当您获得301 的状态时,您还可以使用xxx 参数再次发出请求

【讨论】:

    猜你喜欢
    • 2018-12-04
    • 2022-11-04
    • 2021-12-01
    • 2017-05-01
    • 2019-12-15
    • 2018-08-21
    • 1970-01-01
    • 2014-02-06
    • 2022-06-23
    相关资源
    最近更新 更多