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:
<meta http-equiv="Cache-control" content="no-cache">
in the header - doesn't help.<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.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:
inetpub/wwwroot
directory where I put html & js files and
then open index.html in a browser at http://localhost/
.inetpub/wwwroot
and when I open http://localhost/iisstart.htm
it says IIS7.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!
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With