Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't Google Chrome reloading my scripts?

So, I have a script called "engine", and after much headbashing and (futile) debugging, I've found out that GC simply isn't reloading it!

This is how I include it in the webpage (inside the <head> element):

<script type="text/javascript" src="engine.js"></script>

When a put 10 console.log("asdf");'s at the start of the script, it's like they aren't there. When I went to the "resources" tab in the GC console, I saw that no changes are being applied whatsoever to that script! Hlep? Would putting a + "?" + new Date() at the end help?

like image 499
corazza Avatar asked Apr 06 '12 15:04

corazza


People also ask

Why is my JavaScript code not working in Chrome?

Google ChromeIn the "Settings" section click on the "Show advanced settings..." Under the the "Privacy" click on the "Content settings...". When the dialog window opens, look for the "JavaScript" section and select "Allow all sites to run JavaScript (recommended)". Click on the "OK" button to close it.


2 Answers

The universal solution that works in Chrome, Firefox and IE is cleaning the cache via Ctrl+Shift+Del (on Mac +Shift+).

Chrome solution #1

  1. Open Developer Tools (F12 or ++i, or right-click → Inspect).
  2. Select the Network tab and tick the Disable cache checkbox.

Chrome disable cache - Network tab

  1. Reload the page.

❗️Note: The cache will be disabled only when the devtools window is open.

Chrome solution #2

This only makes sense if #1 is not used.

  1. Open Developer Tools.
  2. Click the Settings cogwheel icon in the bottom right corner.
  3. In the dialog that appears, select under the Network subsection the Disable cache checkbox: from now on the cache will be skipped when the devtools window is open. When the devtools windows is closed caching will work as usual.

Chrome disable cache in devtools settings

Chrome solution #3: empty cache + hard reload

  1. Open Developer Tools (otherwise the menu in the next step won't show).
  2. Click and hold down the Refresh button, and then select from the dropdown Empty Cache and Hard Reload.

Chrome Hard Refresh and Cache Reset

Modifying javascript code

A browser-agnostic solution which could be used for debugging is to append in your server-side code a randomly-generated version string as a query parameter, i.e. call your script as:

<script type="text/javascript" src="myscript.js?ver=12345"></script>

This trick will force the browser to reload the script when the value of the ver parameter changes. If you make ajax requests then you can append "?ver=" + new Date().getTime() to your URL.

NOTE: Don't forget to remove the parameter when you are finished debugging because in production you most likely do want the scripts to be cached. It is a common practice though to generate a new timestamp on each new build — this can be used in production, and will ensure that after a new deployment the clients will always get the updated scripts.


Unlike all the above solutions this one will work even when you have some sort of caching (e.g. redis, memcached, varnish) or CDN (e.g. akamai, cloudflare, cloudfront, etc) between the client and the server.

like image 115
ccpizza Avatar answered Oct 22 '22 19:10

ccpizza


It is possible that the script is cached so the old version is loading from cache. If you want to make sure you get a new version, you can force a browser reload, clear your browser cache or change the name of the script or put a different query parameter on the end of the filename.

like image 31
jfriend00 Avatar answered Oct 22 '22 17:10

jfriend00