Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome doesn't recognize my changes on my javascript file and loads old code?

I have been sitting here for almost an hour here to test the website I'm building. Since I wanted to see the new changes from my code I reloaded, but it was reloading old one. I opened the devetools to hard reload and empy cache hard reload, they both load my old code. I went to incognito mode and it did the same thing. I went to devtools again to disable the cache from the settings and checked the disable cache in the network tab; it still cache my old code. Add-ons to clear the cache didn't work as well. Man, I haven't had this problem before and it only happened last night and it's worst today.

I'm so lost now since chrome doesn't load my new changes from my javascript file. Is there a solution for this?

like image 457
박두준 Avatar asked Mar 08 '17 15:03

박두준


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.

Why js file changes are not reflected in browser?

css files and move them to a production environment, those changes may not get reflected in the browser of the customer who is using our software. This is because those . css and . js files have been cached in the browser.

Why is JavaScript not loading files?

“js file not loading in html” Code Answer Most likely, the problem is that you are including your js file in a head tag or somewhere above your main content. ... You should be able to add the js file in a script tag. The page loads first then JavaScript.


2 Answers

One solution for this problem is to force reloading the resource in order to avoid the cache. You can get this modifying the url with http get parameters:

Change:

<script src="myscripts.js"></script>

to:

<script src="myscripts.js?newversion"></script>

Where newversion can be any string as it will be ignored. A useful option is to use the date, or version, of your code.

I found this workaround particularly useful when I came across this same problem and wanted to ensure that all clients (not just my own browser!) would run the new version of the code.

like image 182
airos Avatar answered Sep 24 '22 00:09

airos


I think there's an even better way:

You can use PHP to add the last modification date of your JavaScript file to the URI of that file.

<script src="js/my-script.js?<?php echo filemtime('js/my-script.js'); ?>"> 
</script>

The browser will receive:

<script src="js/my-script.js?1524155368"></script>

The URI of the file will automatically change if the file is updated.

This way the browser can still cache unchanged files while recognizing changes instantly.

like image 26
Florian Metzger-Noel Avatar answered Sep 27 '22 00:09

Florian Metzger-Noel