17

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.
AndrewL64
  • 15,794
  • 8
  • 47
  • 79
klm123
  • 12,105
  • 14
  • 57
  • 95
  • I sometimes find the disabling the cache in chrome dev tools + refreshing + enabling the cache again + refreshing sometimes fixes the issue? Might be worth a shot... – Nick Parsons Dec 30 '19 at 09:52
  • 2
    what type of server are you using? – Jhecht Jan 19 '20 at 10:25
  • @Jhecht how do I check it? – klm123 Jan 19 '20 at 10:34
  • are you using node.js web server or is it python web server? providing this information would help. – ROOT Jan 19 '20 at 10:40
  • @mamounothman how do I check it? – klm123 Jan 19 '20 at 10:49
  • are you using a programming language for serve the HTML, or maybe you are using apache, nginx ..etc? or are you opening the HTML file directly in the browser? – ROOT Jan 19 '20 at 10:54
  • If you are debugging from visual studio, it'd most probably be an iisexpress – phifi Jan 19 '20 at 12:43
  • @mamounothman I just open html in the browser. I did a very simple setup few months ago, it was so quick that I"m failing completely to recall what exactly I did. Now I just have inetpub/wwwroot directory where I put html & js files and then open index.html in a browser. – klm123 Jan 19 '20 at 17:45
  • @klm123 can you make a simple .html file write "hello world" in it open it in browser and then again change the content "hello new world" then again open the file in browser and let us know if your .html changes not reflecting in browser ? and also show us the URL of your local file ? – Aabir Hussain Jan 22 '20 at 11:20
  • @klm123 for your local changes or development, you can use (CTRL+F5 to reload ) in chrome and better if you use Firefox ( use CTRL+R to reload ) – Aabir Hussain Jan 22 '20 at 11:24
  • `` is the right way to do it. Unfortunately, this is for each document and you are writing it in your HTML-page. You have to write it in your javascript file. But you can't write it inside the script itself... So you have to set it in the request-header. One way would be to write a php script and add the headers. Another one is setting your (Apache) server to add the headers for this script. – Adriano Jan 23 '20 at 12:15
  • @AabirHussain see the edit at the bottom of the post. I do use CTRL+R and right click on the reload symbol to reload. – klm123 Jan 23 '20 at 13:25
  • After you change the index file. Have you checked for updates in other browsers? Are you using APS.NET MVC? Are you using an IIS system server or local IIS from VS? – Aleksandr Smityukh Jan 23 '20 at 15:18
  • If you use IIS. Have you checked server caching settings? There is a separate item in the settings "Caching output data". – Aleksandr Smityukh Jan 23 '20 at 15:23
  • 1
    From what I see in answers you probably will work around it with a parameter. And that probably will work. However, normally this shouldn't be necessary when you disable the cache in chrome dev tools. So my hunch would be there is another cache involved. Between you and the server could be any number of additional caching technology. A few examples: Server side caching (eg Varnish), CDN caching (eg Cloudflare), local proxies from router/firewalls (eg squid-cache) and similar things. I'd check for those as well. – Jey DWork Jan 23 '20 at 17:09
  • @AleksandrSmityukh, no, i use only Chrome. And I have no idea what are all these names are. – klm123 Jan 23 '20 at 18:01
  • @klm123 to get some useful help here you should _really_ specify what kind of web server you are using to serve the files to chrome! Since opening devTools should disable client-side caching it is probably an issue with **server-side** caching. What do you mean by _Now I just [...] open index.html in a browser_? Do you open it from the file explorer and the URL in the address bar begins with `file:/// ...`? Then you're opening it as local file and there is no web server involved. Otherwise opening it via `http://localhost` you need to check the caching config of your local web server. – acran Jan 24 '20 at 19:33
  • @acran I check it on http://localhost by using dev tools/Sources. Many people ask for what server I'm using, but, unfortunately, no one specifies exactly what should I do to provide you that information. Neither anyone says how to disable cache in the cache config of the server. – klm123 Jan 24 '20 at 21:10
  • 1
    @klm123 without knowing which web server this is about it is impossible to tell you, how to change or check the cache config for it. If you have no idea about your web server one possible way to check for it is in the network tab of the devTools: check the response headers of the loaded `index.html` for something like a `X-Powered-By` or `Server` header which might give a hint on the web server. Googling for your file path `inetpub/wwwroot` strongly points to a IIS server. – acran Jan 24 '20 at 21:40
  • @acran I don't see anything like that https://imgur.com/b1AFupn But I looked for ISS in the windows - and I remember that application window - I tried to change something there soon after server setup, but haven't found any kind of helpful option. – klm123 Jan 24 '20 at 22:11
  • @klm123 I am unable to generate your case in my system, I have trying MAC, Ubuntu but still unable to generate your case. Can you provide some more information of your system configurations ? and are you using any web server on local machine ? issue is occur on local machine right ? browser version etc – Aabir Hussain Jan 25 '20 at 06:25
  • Did you just double-click the html file(s) and it opened on your browser or did you do anything first in your terminal maybe (to start a server that loads your html)? Because if it was the former, you wouldn't have `http://localhost/name-of-file` as the url of your local file but instead have `file://pathtofile/name-of-file` as the url. – AndrewL64 Jan 25 '20 at 10:32
  • @AabirHussain Chrome Version 79.0.3945.130 (Official Build) (64-bit). And you need to specify your question about local machine. As I told, I open http://localhost, can it still be a remote machine somehow? – klm123 Jan 25 '20 at 11:53
  • @acran There is iisstart.htm at `inetpub/wwwroot` when I open localhost/iisstart.htm it says IIS7. Is this the server? – klm123 Jan 25 '20 at 12:16
  • @klm123 I did'nt see your last edit thats why I have asked you about local machine and web server. Its clear that you are using windows with IIS web server on your local machine. Please have a look on this => https://blogs.iis.net/ksingla/caching-in-iis7. – Aabir Hussain Jan 25 '20 at 13:07

9 Answers9

5

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!

lehm.ro
  • 750
  • 4
  • 16
  • I guess I can do it. But isn't it effectively the same as my fourth option? to add "?blabla" after the file name. It doesn't work quite often - it's either the same file name in the Sources tab, or blabla is added, but the content of the file is still the same. – klm123 Jan 19 '20 at 14:21
  • It's not the same. Once a parameter is used twice (which will never happen with Date.time() it may render a cached version. ?blabla is the same after using it twice as not using it at all. – lehm.ro Jan 19 '20 at 15:59
  • parameter is not used twice. I edit code, then change the parameter, then check source code in the browser and it is the old one. – klm123 Jan 19 '20 at 17:47
  • Your approach helps, thank you. But it doesn't solve my problem. What you wrote is not true in my situation. "forces all cached versions to be overwritten as the parameter is always a new one", changing the parameter to a new one doesn't force my cache to reload reliably. Also it can't help with changes in index.html. – klm123 Jan 20 '20 at 12:23
4

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?

AndrewL64
  • 15,794
  • 8
  • 47
  • 79
  • There is no web.config at the mentioned path. I've created it and put your code there. But I had to replace index.html by full path like `projectName/index.html`, which I copy from url in my browser `http:/localhost/projectName/index.html`. Result - no effect on the caching problem. – klm123 Jan 26 '20 at 11:14
  • @kim123 Can you try adding the `web.config` file in the root folder of your site (folder where your site is in) instead and changing the path back to just `index.html` and see if that works. – AndrewL64 Jan 26 '20 at 12:02
  • 1
    i did. it doesn't help. – klm123 Jan 26 '20 at 13:46
1

I had same problem and this is my checklist:

  1. Check web.config (caching and clientcache, kernel cache policy etc.) or .htaccess (Header set Cache-Control).
  2. If you used dynamic languages like ASP, PHP there might be internal cache setting. In one case I found <%Response.Expires=1440%> within many lines of ASP code and it was overcoming all other settings! In php session_cache_expire may does the same.
  3. If you are using IIS then check the server > HTTP Response headers to make sure there is nothing about caching also the same for individual websites listed under the server name.
  4. check http-equiv="Cache-control" in html headers ( you passed this test! ).

Finally I have to say that in all situations above, Checking Disable cache in newtwork tab in Chrome console always worked for me to view a fresh version of pages however this is good only for debugging purposes and visitors wont do that!

Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
  • 1. Where do I find web.config and .htaccess? 2. I use html and js only. 3. HTTP Response headers looks like this: https://imgur.com/leYpdej – klm123 Jan 25 '20 at 12:21
  • If you are using IIS the web.congfig is in the root ot wwwroot. Aslo there is Machine.config file in server level which you can modify it from IIS console of directly from here: https://stackoverflow.com/questions/2325473/where-is-machine-config. If you are using apache server on windows please serach windows to find .htaccess @klm123 – Ali Sheikhpour Jan 25 '20 at 12:28
0

What you write in HTML meta is a recommendation to browsers, not a strict instruction. If strict caching is enabled in the browser, this will not help.

The solutions are described well: https://curtistimson.co.uk/post/front-end-dev/what-is-cache-busting/

I prefer on the server side to add to the scripts the version with the date of the last update of the file itself.

<script src="/myScript.js?v=1579780745150"></script>

Where 1579780745150 is Unix Timestamp updates the myScript.js file itself. It works only with its own files, but external ones are not needed. Most often in the file name is the version.

0

Based on your answer's updates, I would say that your issue is caused by IIS v7 that it uses your PC Memory for caching not you local Hard drive that's why Hard reload and all other method seems to not work properly.

I could think of 3 solution:

1. Fixing your IIS cache issue: (Not tested due the fact that i don't have a PC with IIS)

  1. From Start menu, click Administrative Tools, and then click Internet Information Services (IIS) Manager. (see Images)
  2. In the tree view on the left side, find your application.
  3. Select the Output Caching menu item.
  4. Click on Add Cache Rule then type the file extensions that you want to disable cache on them i.e. .aspx, .css, .js etc... (one at the time I guess?)
  5. Now, you can either you Untick user-mode caching or Check prevent all caching. It should look similar to this:

enter image description here

Solution Source - Read more about IIS 7 Cache


2. Using some alternatives to IIS There are quite a few but i'd recommend (based on my experiences) WAMP or XAMPP Both will give you a better development "environment" and both support Windows 7. Also, if you're familiar with VS Code you can just use Live Server extension.


3. Use Chrome Incognito mode while developing. (Not sure if it works for IIS7)

awran5
  • 4,333
  • 2
  • 15
  • 32
-1

This is a long shot - but what sort of editor/IDE are you using? Also, what sort of edits are you making to your index.html?

Could you provide some examples of such edits that aren't being reflected when you save and try to load the file?

I'd like to try to reproduce your issue. From what people are suggesting, it doesn't appear that the problem is within the code itself. I suspect it is an environmental issue but I'd need more info from you to confirm my suspicions.

foreza
  • 46
  • 8
  • I use microsoft visual studio code. Example: replace by – klm123 Jan 23 '20 at 07:13
-1

This is probably really frustrating, and it doesn't sound like you're doing anything wrong.

The problem is likely that something between your browser and the script file you are calling doing some caching.

To determine what it is, let me ask how are you browsing to the localhost url?

Examples:

file://path/to/file.html
http://localhost/file.html
http://127.0.0.1/file.html
http://[machine's-hostname]/file.html
http://[custom-domain-defined-in-hosts-file]/file.html

If you are running file://path/to/file.html this means you are not browsing through a web server/proxy. If it is http://[something]/file.html then you are running some kind of web server and that is likely the culprit. Look for some kind of setting where you can turn off caching.

If you are not using a web server and are literally browsing directly to a local html file without a web server, then I would suggest the browser is the culprit. If this is the case, I'd suggest turning off browser caching like Martin Shishkov suggested.

Dean Householder
  • 549
  • 1
  • 7
  • 13
  • it's ```http://localhost/ProjectName/index.html``` Where this setting where you can turn off caching can be? – klm123 Jan 24 '20 at 22:43
  • It looks like from the screen shot you posted elsewhere, you are using IIS, (also called Microsoft's Internet Information Services). A quick solution is to just double-click on your .html file to load it in the browser, which will use a `file://path/to/projectName/index.html`. That will remove the web server from the equation. To continue using IIS and just fix the cache, refer to this article: https://support.microsoft.com/en-us/help/247404/how-to-modify-the-cache-control-http-header-when-you-use-iis – Dean Householder Jan 25 '20 at 17:14
-1

Chrome does this thing where, if file hasn’t changed too much, not sure what it means, it will use the “compiled” cache version. For html it means parsed don tree. For JS precompiled code etc.

There is usually ways to address this, many frameworks use hash inside filenames, like: main.md5hash.js, because changing file name invalidates any cache.

Then you will be left with possibly html not updating. And to be clear, I am not sure, you can always add comment with random blob... like a date.

Akxe
  • 9,694
  • 3
  • 36
  • 71
-2

In the specific case with using Google Chrome, try this: Open Dev Tools -> Go to 'Network' tab. Check 'Disable cache' checkbox, after that refresh the page.

NOTE: In order for this to work every time the Dev Tools window MUST stay open at all times.

enter image description here

However, in order to propagate changes to your users, without having them to hard refresh the page, etc, you have to append file versions at the end of each referenced file for files that change frequently (just as you pointed out). For example:

<link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/Sites/stackoverflow/primary.css?v=20b379f72a37" >

The v=20b379f72a37 appended to the end is an auto-generated file hash of the file contents.

Martin Shishkov
  • 2,709
  • 5
  • 27
  • 44