-
|
I have been playing with custom BaseQueryFn and hook other state to keep track query on queryCacheKey API ETags. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
For ETag-based cache validation with RTK Query, the trick is to handle the 304 response in your In your custom const baseQuery: BaseQueryFn = async (args, api, extraOptions) => {
const etag = getStoredETag(args.url);
const headers = etag ? { 'If-None-Match': etag } : {};
const result = await fetchBaseQuery({ baseUrl: '/api', headers })(args, api, extraOptions);
if (result.meta?.response?.status === 304) {
// Return previous cached data — RTK Query sees no change, no re-render
return { data: api.getState().api.queries[api.queryCacheKey]?.data };
}
// Store new ETag
const newEtag = result.meta?.response?.headers.get('etag');
if (newEtag) storeETag(args.url, newEtag);
return result;
};The key insight is that returning the same |
Beta Was this translation helpful? Give feedback.
For ETag-based cache validation with RTK Query, the trick is to handle the 304 response in your
baseQueryand return the cached data without triggering a store update.In your custom
baseQuery, you can check if the response is 304 and return the previous result from the cache. Something like: