Link to home
Start Free TrialLog in
Avatar of peter-cooper
peter-cooper

asked on

Why are my javascript files being read from cache

Hello
I have a problem where my javascript and css files are being read from the cache instead of when I do refresh for new content so it never updates. I am using firefox and wamp server and in firefox disabled privacy.trackingprotection, have selected always look for new pages in web developer tools and put wamp in offline mode.

Is it apache that is caching these files? Here is the exact message in the net tab of firebug. How can I solve this so my content is always updated. Thanks

The request was resolved directly from the cache, so we have no response from the server. See below for the cached response.

Open in new window

Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

They are being read from the browser cache because the file names match the request and the browser sees no indication that there are new versions on the server.  Refreshing a page or even Ctrl-F5 refresh only refreshes the main page.  It does Not automatically refresh JS and CSS files if the name remains the same.  This is normal operation in all browsers that I know of.
Avatar of peter-cooper
peter-cooper

ASKER

@Dave
It does Not automatically refresh JS and CSS files if the name remains the same
actually it does. I just cleared cache and it loaded new files even though it is still showing as 200 OK (BFCache). How can I prevent this action as I do not want to clear cache each time I make updates to css or js file. Thanks
You just confused two different things.  Clearing the cache forces everything to be refetched because that's where it is all stored.  Doing a 'page' refresh does not.  Methods to force JS and CSS files to be updated when they are changed requires adding a query string with the version number or timestamp so they won't match the old version in the browser cache.  

This question gets asked frequently because it is irritating to have to force the updates when you are developing.  The idea of the cache is to speed up page loads when the files don't change.  Makes it better for the user but irritating for the developer.
@Dave thanks for clearing that up. Do you have example how to code version number to file. Thanks
It would look like "somejavascript.js?version=9".  The version number does not have to actually be used by the code.  The link in the browser must use it when you change the code.  Even then, it doesn't get picked up until the new version of the page is downloaded.

Here is a discussion about it from last week.

https://www.experts-exchange.com/questions/29023735/Global-version-website-variable-for-clearing-cache.html
@Dave Thanks for link. However, I have 2 questions arising from such.

1) Do I have to change the version # each timeI update the CSS or JS file?

2) If so, is there a script that is available in php or jquery that could could randomise the version # and and replace with that version in the file name?

Thanks
#1.  Yes.  That is the point, to make the version and thus the file name different from the previous version in the cache every time you change it.

#2.  You could write one in PHP.  jQuery runs in the browser so that is too late to affect the file names that are fetched.  But why would you want to randomize them?  I can't think of any use for that.
@Dave perhaps I should have said timestamp rather than randomize
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks very much Dave. Exactly what I need. Cheers
You're welcome, glad to help.