-
Notifications
You must be signed in to change notification settings - Fork 732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Apollo URLRequest.CachePolicy not respecting Cache-Control header #3483
Comments
I believe Practically these are the same behaviour because the Apollo cache has no notion of expiration, so whatever is in the Apollo cache will always be returned before attempting a network request when combined with the default Apollo client cache policy. It should only be that if you disable the Apollo-side cache or manually clear it out, with the iOS HTTP cache still having that record, that you encounter this problem. We don't have any equivalent of You're using |
In this code change, Apollo began setting a cachePolicy for the request:
Previously, it did not set a cachePolicy. So the NSURLRequest always used Apple's default value of After this change, Apollo is now over-caching content indefinitely, causing stale data to show in the app. This was a new bug introduced by that code change. We had a custom in-memory Apollo cache that did check for expiration, but we were still having reports of stale content in the app showing for months until we finally tracked down that this change caused it. We've fixed this by overriding the Apollo cache policy in our app. I just don't think the default for NSURLRequest should be to ignore the caching headers indicated by the server response. It's not going to be obvious to other consumers of Apollo who need to respect cache time-to-live that they need to override this cache policy to get what Apple thinks should be the default behavior of NSURL caching. |
That's correct, but it also meant that there was no way to opt-out of the iOS caching behaviour - which is also a bug for requests that do not want any cached data served.
I don't think Given that if the Apollo iOS cache store had expiration features it would obey them I can support the argument that |
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo iOS usage and allow us to serve you better. |
@marksvend - apollographql/apollo-ios-dev#550 has been merged and will be included with the next release. |
Summary
The fix for Issue 3432 introduced a new caching bug. Before this change, NSURLSession was using the default URLRequest.CachePolicy of
.useProtocolCachePolicy
. After the change, the default policy for using cached data changed to.returnCacheDataElseFetch
.The
.returnCacheDataElseFetch
value ignores the Cache-Control header of the document and does not ever expire the data. This leads to NSURLCache returning stale data long after data has expired. From Apple's documentation:Instead, it should be using
.useProtocolCachePolicy
:Version
1.15.3
Steps to reproduce the behavior
I reproduced the issue by loading a query with Charles to intercept network requests. The first time the page loads with a clean cache, the network request loads returning a response containing a Cache-Control header with a max age of something like 60 seconds. After waiting several minutes and loading the same URL through the Apollo SDK, Charles does not log a new request on the network. Instead, the NSURLCache is returning stale data every time.
Logs
No response
Anything else?
I believe the code should be updated in JSONRequest to look like this instead:
The text was updated successfully, but these errors were encountered: