Search code examples
javascriptfetchapolloapollo-client

set header apollo client in custom fetch


I try to update a cache control header depending on response.status on apollo client in the fetch property, I have a custom fetch method, but I have an error when I use headers.set()

TypeError: Failed to execute 'set' on 'Headers': Headers are immutable

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
  });

Solution

  • You can't change the headers in your response. An alternative would be to make the request with an unused param where you can control the cache.

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

    You could also make the request a second time with the xxx param when you get a status of 301