5

I'm writing a react-native app, and I'm polling from an API that supports the If-Modified-Since header.

My request is:

const response = await fetch(
  uri,
  {
    method: 'GET',
    cache: 'no-store',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      Authorization: `token ${token}`,
      'If-Modified-Since': lastUpdatedAt.toUTCString(),
    },
  }
);

Now, the problem is that this request will always return a response.status equal to 200, while if I run the same request with CURL:

curl -s -o dev/null -w "%{http_code}" $uri -H "If-Modified-Since: Mon, 27 Nov 2017 11:36:41 GMT"

It will, correctly, return 304.

What am I doing wrong? Do I have to set any additional option?

Fez Vrasta
  • 14,110
  • 21
  • 98
  • 160

2 Answers2

1

Set the cache option to "default" and fetch will send the If-Modified-Since header - it's not necessary to define the header separately. Remember that the If-Modified-Since header is for the browser level cache. If you don't cache anything in the browser, sending the header to check if you can use the cached data is pointless (which is what 304 means - just use the data you already have cached).

You can find all of the possible configurations for fetch() here: https://javascript.info/fetch-api

0

I had a similar question. It seems checking for a 304 status is unreliable. Look here: Recognize HTTP 304 in service worker / fetch()