Skip to content
Discussion options

You must be logged in to vote

For ETag-based cache validation with RTK Query, the trick is to handle the 304 response in your baseQuery and 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:

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

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by mharj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants