redux-toolkit
redux-toolkit copied to clipboard
SSR RTK Query Caching
Hi all! We use RTK Query to query our API (using createAPI)
I'd love to persist record of what the API state has cached in cookies, so that in SSR I could skip an API request if already in the cache.
I've tried implementing the following way: I created an "apiCookiesCache" with middleware that runs on API actions (executeQuery/fulfilled) and replicates the queries state (not including the response data) into the cookies (using https://www.npmjs.com/package/next-redux-cookie-wrapper).
There's a few problems with this:
- No simple way to intercept a
store.dispatch(getPosts.initiate(query))action and check the apiCookiesCache to see if we already have this cached in the client-side. I wish I could intercept the action, check if the queryKey is in cache, and drop if so. But this dispatch triggers an anonymous function (which I assume makes the request). Seems like the way to prevent the API from happening is hacky:dispatchIfNotCached(dispatch, 'getPosts', query)