39

how can I force a browser to always load the newest version of index.htm when the page is loaded by entering the URL www.mydomain.com/index.htm or just www.mydomain.com in the browser's address field and pressing enter.

I'm trying this in Chrome and the newest version of index.htm is apparently only loaded, when I refresh manually (F5), or when the URL is already in the browser's address field and I press enter.

I guess I am doing something extremely stupid, because when I searched for the issue, all I could find were solutions about how to make a browser reload your .js and .css files by appending ?v=xxxx to the file names. But how should this work, if not even the newest version of index.htm page, in which I am doing these modifiactions, is loaded??

I also tried putting

<meta http-equiv="cache-control" content="no-cache">

in the <head> of index.htm. But this does not seem to have any effect.

Any help would be greatly appreciated!

Thanks, Linus

Linus
  • 1,320
  • 2
  • 12
  • 20
  • 1
    If you just got here, you might actually be looking for https://stackoverflow.com/questions/49547/how-to-control-web-page-caching-across-all-browsers – Anthony Drogon Mar 20 '18 at 10:40

3 Answers3

60

OK, apparently no-cache was not enough. The following does the trick:

  <meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0" />
  <meta http-equiv="cache-control" content="max-age=0" />
  <meta http-equiv="expires" content="0" />
  <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
  <meta http-equiv="pragma" content="no-cache" />
Linus
  • 1,320
  • 2
  • 12
  • 20
  • @stefanosn you can also try to add some variables to JS and CSS files. see more here https://stackoverflow.com/a/15562751/4481831 – crisc2000 Nov 04 '20 at 08:05
3

You can use the code below to refresh or reload the currently loaded index page from a URL address entered directly into the browser's address bar, after a specific number of seconds, thereby forcing the browser to always reload the current document. In this case, the number of seconds has been set to 5:

<meta http-equiv="refresh" content="5" />

Please note that setting the number of seconds to 0 will cause the page to be automatically reloaded instantly, every time it is successfully downloaded.

1

To do this, you will need to perform some server-side coding along the way. You can use technologies like PHP or ASP.NET for this. I prefer coding with PHP, so here is a PHP-based example. First of all, make sure your INDEX is called "index.php", rather than "index.html" or "index.htm". Now take note of the following codes and make integrate this to your index.php file, while inserting your own page contents as well:

<html>

    <?php
        header("Content-Type: text/event-stream");
        header("Cache-Control: no-cache");
    ?>

    <head>
        <script>
            var sse = new EventSource("index.php");
            sse.onmessage = function(event) {
                document.write(event.data);
            }
        </script>
    </head>

    <body>

        <!-- Insert HTML Codes -->

        <?php
            flush();
        ?>

    </body>

</html>