New configuration respectMaxAgeZero#503
Conversation
11a8f43 to
b6ebc7f
Compare
If a server sends "Cache-Control: max-age=0" this might be a misconfiguration on the server; in this case we should ignore it (the behaviour up to now). It might also mean, that the content potentially already expired; for this case this commit provides a new configuration parameter so you reinterpret the duration how long this downloaded file should stay valid.
b6ebc7f to
bcb342d
Compare
rickdijk
left a comment
There was a problem hiding this comment.
Please add tests in http_file_fetcher_test.dart for max-age=0 (default vs custom) and no-header + custom duration, plus a CHANGELOG [Unreleased] entry.
| : _httpClient = httpClient ?? http.Client(); | ||
| HttpFileService({http.Client? httpClient, Duration? durationOnMaxAgeZero}) | ||
| : _httpClient = httpClient ?? http.Client(), | ||
| _durationOnMaxAgeZero = durationOnMaxAgeZero ?? Duration(days: 7); |
There was a problem hiding this comment.
Using _durationOnMaxAgeZero as the initial ageDuration also changes the TTL when there’s no Cache-Control / no positive max-age. Please only apply it when max-age=0 (or ≤ 0), and keep const Duration(days: 7) as the default.
Also prefer const Duration(days: 7) in the HttpFileService fallback.
| fileSystem = fileSystem ?? MemoryCacheSystem(), | ||
| fileService = fileService ?? HttpFileService(); | ||
| fileService = fileService ?? | ||
| HttpFileService( |
There was a problem hiding this comment.
durationOnMaxAgeZero is only passed into the default HttpFileService. If a custom fileService is provided (as in the README example), this Config value is ignored.
Either document that clearly, or drop it from Config and configure it only on HttpFileService. The redundant ?? Duration(days: 7) can use this.durationOnMaxAgeZero if you keep the field.
| /// [stalePeriod] is the time duration in which a cache object is | ||
| /// considered 'stale'. When a file is cached but not being used for a | ||
| /// certain time the file will be deleted. | ||
| /// [durationOnMaxAgeZero] is the time duration a fetched API response is |
There was a problem hiding this comment.
Doc comment is fine, but please clarify that this only affects the default HttpFileService (or remove it from Config if you go that route).
| It could either mean, that the content expires really fast (then `Cache-Control: no-cache` might | ||
| be a better response) or it could be that the server has some misconfiguration in place. | ||
|
|
||
| There where some confusions among users of this library because of the second case (see also |
There was a problem hiding this comment.
| There where some confusions among users of this library because of the second case (see also | |
| There were some confusions among users of this library because of the second case (see also |
| to `Duration(days: 7)`. | ||
|
|
||
| If you want to treat `max-age=0` the same as `no-cache` (or something less rigid like 30 seconds), then | ||
| set the `Config` parameter `durationOnMaxAgeZero` to `Duration(seconds: 0)` or `30` or whatever you |
There was a problem hiding this comment.
| set the `Config` parameter `durationOnMaxAgeZero` to `Duration(seconds: 0)` or `30` or whatever you | |
| set the `Config` parameter `durationOnMaxAgeZero` to `Duration(seconds: 0)` or `Duration(seconds: 30)` or whatever you |
| behaviour this library ignores `max-age=0` and instead sets the validity of the downloaded file | ||
| to `Duration(days: 7)`. | ||
|
|
||
| If you want to treat `max-age=0` the same as `no-cache` (or something less rigid like 30 seconds), then |
There was a problem hiding this comment.
FAQ says set Config durationOnMaxAgeZero, but Customize always passes fileService: HttpFileService(). That combo silently ignores the setting. Show the working setup.
If a server sends "Cache-Control: max-age=0" this might be a misconfiguration on the server; in this case we should ignore it (the behaviour up to now).
It might also mean, that the content potentially already expired; for this case this commit provides a new configuration parameter to react on this response and configure the validity duration of such a response.
✨ What kind of change does this PR introduce? (Bug fix, feature, docs update...)
New configuration parameter so that on
Cache-Control: max-age=0we can configure the validity duration (it even can be treated the same asCache-Control: no-cacheby setting the parameter toDuration(seconds: 0)).Currently, if the server responds with
Cache-Control: max-age=0, the library ignores this and sets the max-age to one week.🆕 What is the new behavior (if this is a feature change)?
See above. The default behavior is left as-is.
💥 Does this PR introduce a breaking change?
No
🐛 Recommendations for testing
📝 Links to relevant issues/docs
I've seen the discussion in #283
In my app I have this issue: I'm contacting an API (which I cannot control or change) and this API delivers "some kind of" real time data (results for sport games). The server delivers json files; the response contains an etag header and the header
Cache-Control: max-age=0. So I periodically poll this endpoint and if the etag is still valid I go with the cached file.But the current behavior of the lib gives me the cached file regardless if the json changed or not. By setting the Config parameter which is introduced here, I can properly work with the API.
I added a new FAQ to the README.md.
🤔 Checklist before submitting
flutter format .anddart format .reformatted the whole project. So ... I hope the code formatting is fine.