1

I have an application running node.js in the backend and react.js at the front.

I have encountered a bug that happens when I log in as a admin, log out and immediately try to log back in as a customer without refreshing the page. When I do that the user logs into the admin account that was just logged out.

In my attempt to debug this issue i checked to see if my auth cookie was being removed successfully upon logout and it does. So what I checked was if the cache was interfering in the bug by not refreshing the page, just clearing the cache (Ctrl+Shift+R) and trying to log as user using the same steps. This works 100% of the time, which probably means that the problem is the browser cache.

I did some research and there is a way to "remove" cache by using meta tags like this:

<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>

I wanted to make sure that not storing cache does not interfere in the performance of my app, and if it does I would like to know if someone knows a way to turn this around.

3
  • 1
    I think the important question is "why does clearing the browser cache solve the problem?" Commented Jul 23, 2021 at 18:11
  • 2
    does your auth process uses localStorage ? Commented Jul 23, 2021 at 18:25
  • Yes it uses local storage Commented Jul 23, 2021 at 18:54

2 Answers 2

1

If you just want to clear-out complete cache data you can do this way .

const clearCacheData = () => {
    caches.keys().then((names) => {
      names.forEach((name) => {
        caches.delete(name);
      });
    });
    alert('Complete Cache Cleared')
  };

then return

    <div style={{ height: 500, width: '80%' }}>
      <h4>How to clear complete cache data in ReactJS?</h4>
      <button onClick={() => clearCacheData()} >
        Clear Cache Data</button>
    </div>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. Will try implementing this.
This didn't work for me!
0

Just clear your localStorage on logout: localStorage.clear() (place this code inside your "logout" function)

1 Comment

I already do that, if you read it carefully I said the auth token was removed upon logout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.