[Feature Request] Invalidation callback
See original GitHub issueSimilar to axios-cache-adapter
callback called invalidate
.
With this callback, we could easily fine-tune the interceptor’s behaviour, by inspecting the current request id, method and what not.
As an example, this is what I have right now, along with axios-cache-adapter
:
async function invalidate (config, req) {
if (req.forceRefresh) {
await invalidateByForcedRefresh(config, req)
} else if (req.method.toLowerCase() !== 'get') {
await invalidateByMethod(config, req)
}
}
It allows me to pass in extra arguments (like forceRefresh
in this case) straight from each request, allows me to test for id patterns for clearing cache, etc.
The reason is that the current update[id]
method can be a little too simplistic. I may have an user requesting GET /things?filter=alpha
and GET /things?filter=beta
, and I want both cached. When the user does POST /thing
, I want to invalidate both entries with different filters. Currently, I would have to implement that into each request point, making sure I generate suitable IDs and clearing all caches after a success.
The callback would allow me to better defined default invalidation behaviour into a centralised place.
Edit:
Another common case is multiple page loading. GET /things
and GET /things?nextPageToken=ABCDE
are both parts of the same query, and both need to be invalidated if a new item is added.
Issue Analytics
- State:
- Created a year ago
- Comments:10 (7 by maintainers)
Top GitHub Comments
@nolde, just released v0.10.7, can you test it out?
@nolde, i got time and created a PR with the above changes. Can you take a look at it before I merge into prod?