3

How do we do a hard reload/refresh in Chrome using Javascript?

window.location.reload(true);
location.reload(true);

didn't do it for me.

===========================

What I meant by 'didn't do it for me...'

The session cookies (ex. JSESSIONID) were not renewed specially the HttpOnly ones.

What I want to achieve...'

  1. I wanted to reload the page like it's the first time I accessed the URL.
  2. I wanted to simulate the steps below (which is like accessing the URL for the first time)
   - Open browser
   - Type URL
   - Hit Enter

I wonder if there is a more powerful Javascript command that reloads as if it is the first time.

lemont80
  • 89
  • 2
  • 10
  • Can't reproduce. What version of Chrome are you using? – Spectric Sep 22 '21 at 23:44
  • We need more details. How did you test this? https://stackoverflow.com/questions/2099201/javascript-hard-refresh-of-current-page – trinalbadger587 Sep 22 '21 at 23:47
  • 1
    the param is deprecated, if your trying to clear cache you should look into using cache busting vars on asset urls so if a file changes so does the param which will cause the file to freshly loaded, i.e version your files or use a service worker like workbox – Lawrence Cherone Sep 22 '21 at 23:54
  • I added more details. Hope you are getting my point. – lemont80 Sep 23 '21 at 00:43

2 Answers2

2

The best (and practically only) way to ensure a page is hard reloaded is by using your server. One of the ways you can do this is by serving headers, when a page is requested, that invalidate resources such as Cache-Control which will tell the browser to not cache resources and always revalidate resources which means everything must be redownloaded each time.

I would not recommend serving this header on every request in your production application, though.

Cache-Control: no-store, max-age=0

On your page you can add meta tags that will tell the browser what to do, provided there weren't headers already passed to the browser. This depends on how you serve your content, but you can add the following to your head element which will tell the browser to store absolutely nothing and is equivalent to hitting a page for the first time:

<head>
...
<meta http-equiv="Cache-Control" content="no-store, max-age=0">
...
</head>
Marcus Parsons
  • 1,714
  • 13
  • 19
  • 1
    As much as possible, I wouldn't like to go to server side – lemont80 Sep 23 '21 at 01:26
  • I'm going to edit my answer with some additional things you can do on the front end. – Marcus Parsons Sep 23 '21 at 02:14
  • 1
    manually, I could just close the browser, open it back, and access the same URL again, then sessions cookies are refreshed. I literally would want to achieve this in Javascript – lemont80 Sep 23 '21 at 02:46
  • Session cookies being cleared are absolutely not the same as hard reloading your cache. You'll need to do a whole lot more research on the subject because I won't argue with someone on a Stack Overflow post. – Marcus Parsons Sep 23 '21 at 02:47
  • 1
    love to upvote your answer @Marcus Parsons, but as you can see, I can't vote yet. maybe my question is misleading but my comment above is actually what I wanted to achieve – lemont80 Sep 23 '21 at 02:50
  • 1
    It's okay if you can't upvote. I re-read your question after your edits, and I believe that my edited answer should help you out. Try adding that element to the `head` of your document. But your mileage may very because meta tag results depend upon how your content is served to the end user. – Marcus Parsons Sep 23 '21 at 11:33
  • 1
    I'm in SI team so I don't really have the luxury to pass things back to the server side. I am just doing what I can with what I receive in the front-end. – lemont80 Sep 23 '21 at 23:35
  • 1
    Another option you can try is using testing software like Puppeteer or Playwright to launch a browser instance that goes to the website, regardless of whether it's hosted locally or on a server. It should, in theory, launch each time with no cache, session, or application data in it. – Marcus Parsons Sep 24 '21 at 00:23
  • 1
    Thanks bro. but can i get an upvote? I'm kind of short on reps here. :D – lemont80 Sep 24 '21 at 03:55
0

location.reload() should work. Works for me!

Paradox
  • 133
  • 1
  • 11
  • `location.reload()` just reloads the current page. It doesn't perform a hard reload. In now obsolete methods of `location.reload` you could pass in `true` which would perform a hard reload. But, they are now obsolete, of course. – Marcus Parsons Sep 23 '21 at 00:04
  • @The_Paradox, I tried this but it didn't reload the page like it's the first time it is accessing the URL – lemont80 Sep 23 '21 at 00:47
  • Not pulling data from server. – Khalid Almannai Mar 31 '23 at 12:15