3
votes

Within a sample demo app I'm using React Native in order to make network calls. Under the hood React Native uses NSURLSession with NSURLRequestUseProtocolCachePolicy as the default cache policy. When a network call is made, the response returned by the service contains these cache controls headers:

public, max-age=10, must-revalidate

What I'm able to notice is that NSURLSession does not honour the 10 seconds if max-revalidate is present. On the contrary if the latter is not present whenever a new network call is made within the 10 seconds, the cache response is returned, otherwise a new call is made.

Based on the 14.9.4 Cache Revalidation and Reload Controls section this should not be the expected behaviour but maybe I'm wrong.

Because a cache MAY be configured to ignore a server's specified expiration time, and because a client request MAY include a max- stale directive (which has a similar effect), the protocol also includes a mechanism for the origin server to require revalidation of a cache entry on any subsequent use. When the must-revalidate directive is present in a response received by a cache, that cache MUST NOT use the entry after it becomes stale to respond to a subsequent request without first revalidating it with the origin server. (I.e., the cache MUST do an end-to-end revalidation every time, if, based solely on the origin server's Expires or max-age value, the cached response is stale.)

I found a useful discussion on this subject: HTTP Cache Control max-age, must-revalidate. Based on the discussion, it seems that the protocol is a bit

ambiguous here, but in practice I have found that must-revalidate means it must revalidate regardless of max-age.

A very strange thing is that if I do the same with Android, using both max-age and must-revalidate the behaviour is correct. After 10 seconds a new request will be made. Within the 10 seconds no request is made.

Any clue?

1

1 Answers

1
votes

I did some tests and it seems that in iOS (using NSURLSession) whenever must-revalidate is set, the max-age is not taken into consideration. It's a different story for Android where these two values can be used together.

P.S. if someone expert in HTTP protocol has a clue on this subject, please let me know.