Reset cache on logout
See original GitHub issueHi,
I’m facing an issue in order to clear state and cache after a user logout.
Here, what I’m doing right now to handle that :
import { combineReducers, configureStore } from '@reduxjs/toolkit';
import { api } from './api';
import authReducer, { logout } from '../features/auth/authSlice';
import requesterReducer from '../features/requester/requesterSlice';
import { unauthorizedMiddleware } from './store/middlewares/unauthorized';
const combinedReducer = combineReducers({
[api.reducerPath]: api.reducer,
auth: authReducer,
requester: requesterReducer,
});
export const store = configureStore({
reducer: (rootState, action) => {
let state = rootState;
if (logout.match(action)) {
state = undefined;
}
return combinedReducer(state, action);
},
middleware: (getDefaultMiddleware) => [
...getDefaultMiddleware().concat(api.middleware),
unauthorizedMiddleware,
],
});
The issue comes in App.js :
function App() {
const {isLoading} = useGetSessionQuery();
if (isLoading) return <Loader />;
return (
<Router>
<div className="App">
<Switch>
<Route exact path="/login" component={Login} />
<PrivateRoute path="/">
<Requester />
</PrivateRoute>
</Switch>
</div>
</Router>
);
}
Reseting state like I do provokes isLoading from my getSession query to be ‘true’ so my app becomes stuck until I refresh.
Thanks!
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top Results From Across the Web
To clear cache after logout - Stack Overflow
clearstatcache — Clears file status cache. This function caches information about specific filenames, so you only need to call ...
Read more >How to Clear cache after logout - Laracasts
How to Clear cache after logout. I tried using this code. However, it does not work. Even after a logout I can use...
Read more >[Ioniv 4] How to properly clear the cache when logout - ionic-v3
Hi, when I logout with a user-A and then login with another user (user-B) I still ... How can I properly clear the...
Read more >How to clear the browser cache after logoff - MSDN - Microsoft
How can I force to clear the cache during the logout to be able to click the Log In and be able to...
Read more >rtk query clear cache on logout - You.com | The AI Search ...
I created new api for each logical part. So to invalidate cache I would have to reset each api's state individually. Fortunately rtk...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Nothing around the hook :
What do you mean by ‘dispatch the “reset cache”’? Using resetApiState from cache management utils? If so, i tried it and change my configureStore like that :
But I faced the same issue.
I also faced this exact same issue where I have a useGet…Query in the App.js that gets triggered on resetting api when user logs out. I also used the invalidate tags approach to work around it but feel it will not be very friendly in the long run since most of my endpoints will be active depending on what information am currently using on the current page.
my code looks exactly the same as the OP. Is there anything that one could try to do in this case? My main problems are two: