Skip to content
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

Cache invalidate based on headers #1904

Closed
sriteja777 opened this issue Jan 10, 2022 · 3 comments
Closed

Cache invalidate based on headers #1904

sriteja777 opened this issue Jan 10, 2022 · 3 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@sriteja777
Copy link

sriteja777 commented Jan 10, 2022

For RTK Query

Currently the cache key takes only the query args into consideration for deciding validation of cache.
We have an endpoint getPosts and in the headers of this GET request we are sending the JWT token of the user. The response is different based on whether the token is present or not. We use prepareHeaders to set the token in requests instead of sending it as arg in every request.

baseQuery: fetchBaseQuery({
  baseUrl: apiConfig.baseUrl,
  prepareHeaders: (headers, { getState }) => {
    // By default, if we have a token in the store, let's use that for authenticated requests
    const token = (getState() as RootState).user.token
    if (token) {
      headers.set(Constants.AUTH_HEADER, token)
    }  
    return headers
  },

But with current setup the getPosts will not refetch data from server if the token in Header changes. Is there any way / workaround to achieve this?

  • One solution is to send the token as query arg, but token needs to be sent every where the posts are required. Is there any way to minimize this?
@phryneas
Copy link
Member

phryneas commented Jan 10, 2022

Hmm, if you can derive that from the response, you can add some kind of "UNAUTHENTICATED" tag to queries that were not authenticated.
At the moment we do not have meta available in providesTags/invalidatesTag at the moment, but it would probably be consistent with transformResponse to add it to these two functions too. Then you could look in there and look at the actual request & response including headers.

Pull Requests in that direction would be very welcome - at the moment I don't have the time to do a lot on RTK.

@bever1337
Copy link
Contributor

I just submitted a PR to pass meta arg to result description functions. This is my first redux commit, so the changes lean more towards "perfectly serviceable" than "wow, such beautiful code". Please let me know what adjustments I can make.

@phryneas
Copy link
Member

phryneas commented Feb 3, 2022

The meta argument has been released in https://github.com/reduxjs/redux-toolkit/releases/tag/v1.7.2

This should be possible now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants