Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is server ignoring changes in the code files even though cache is disabled?

I test an html/js code on my localhost (Windows 7, Chrome v79.0.3945.130 (64-bit)) and about 50% of the time code changes are not reflected in the browser (I see it with Dev Tools/Sources).
There are a ton of advice on the internet, but neither seems to work:

  • Rightclick on the reload and chose "Empty cache and Hard reload" - doesn't help in 30% of the cases.
  • Disable cache in the Network tab of the Chrome Dev Tools - doesn't help.
  • Add <meta http-equiv="Cache-control" content="no-cache"> in the header - doesn't help.
  • Replace <script src="common.js"></script> by <script src="common.js?blabla"></script> - helps in 60% of the cases, but you need to do it after every change is a huge chore. Also, it doesn't work with html changes.
  • Copy a file to a new file (like index.html to index2.html) and replace the file name in the code - always works, but it is an even bigger chore.

The exact same problem present when I commit the code to github.io

Please help me to make it so the site reflects the code changes immediately.


Edit: I've created a file index3.html and put only "hello world" there. Opened the file in the browser. Changed to "hello world2" - the browser updated the content. Changed to "hello world3" - the browser still was showing "hello world2" even after multiple reloads and "Empty cache and hard reload". I changed to "hello world4" - the browser still showed "hello world2". In for 4 hours I changed to "hello world5" - the browser still shows "hello world2". This file I edited with basic notepad.


Edit2: People keep asking what server I'm using. This looks like a part of the problem. Unfortunately, I don't know and neither do i know what exactly I need to do to check it. Here is all I've found out so far:

  • I have inetpub/wwwroot directory where I put html & js files and then open index.html in a browser at http://localhost/.
  • My Network panel in Devs Tools looks like this: image link.
  • The server setup was very fast and didn't require any additional software to install. I.e. I'm not using node.js.
  • There is iisstart.htm at inetpub/wwwroot and when I open http://localhost/iisstart.htm it says IIS7.
like image 550
klm123 Avatar asked Dec 30 '19 09:12

klm123


Video Answer


2 Answers

Using a parameter is the right way! Here you can add a version number you only change on big changes manually and for debugging you can set it to Date.time() so in your < head> tag, this forces all cached versions to be overwritten as the parameter is always a new one:

< script type="text/javascript"> 
    var script = document.createElement('script'); 
    var version = Date.time();
    script.src = "common.js?v="+version; 
    document.head.appendChild(script) 
< /script>

Hope this helps!

like image 153
lehm.ro Avatar answered Sep 17 '22 06:09

lehm.ro


In IIS7 Windows server running on Windows 7, to prevent caching of your content, you need to make some changes to your one of your configuration files called web.config. The configuration files for IIS 7 and later are located in %WinDir%\System32\Inetsrv\Config folder where %WinDir% is the folder where you installed Windows (usually C:/Windows).

Create a backup copy of your web.config file before editing it so you can rollback to it if the changes you'll make to the file breaks it.

In your web.config file, just add the following to prevent caching of your index.html:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <location path="index.html">
    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <add name="Cache-Control" value="no-cache" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>
  </location>

</configuration>

Got the file path for the web.config file from here: Windows Doc: Configuration Reference

Got the configuration for the cache above from here: How to disable caching of single page application HTML file served through IIS?

like image 42
AndrewL64 Avatar answered Sep 17 '22 06:09

AndrewL64