Lately I've seen a lot of problems in Google Chrome (in other browsers it works fine, except also Safari on Mac) when loading a new version of a style sheet ( .css
) or a javascript script ( .js
), I thought I could solve it by A simple:
<link rel="stylesheet" href="/private/css/visual.css?v=1" />
<script src="/private/js/visual.js?v=1"></script>
But it doesn't always work for me and I have to be constantly changing the value of v=1
so that it loads the new versions of the respective files
Is there any way to prevent the cache properly?
Get a library, native javascript code, something to add to my files .css
or .js
, change on my server, change on my machine, browser settings, etc... and avoid this problem.
NOTE: The cache remains both locally and already hosted on a server / domain
UPDATE: Maybe at first I wasn't very clear, although JuankGlezz's answers and derivatives help me in the development environment, I'm looking for an answer that can serve my end users without them having to touch anything...
This can be used in production and debugging environments, if you are one of those who constantly modify the SCRIPT and CSS of web pages, but you do not usually keep the browser console open and you want to avoid the cache, you can add the following lines in the HTML.
In WordPress, to add tags in the
</head>
we must edit the template or theme , specifically we must edit the fileheader.php
where the<head>
and tags are located</head>
.In an application developed in PHP, you can force it not to save cache in the browser, you can use the following lines:
If you are a developer and you don't want to add the previous lines, you can avoid the cache from the browser console (F12).
It is recommended to keep the browser console open (F12) and enable the disable cache box in the network tab
For Firefox open the debugger console (F12)
One way is when you reference JavaScript files you put at the end
?1
. This means that the?1
is some kind of versioning of the file so that the browser reloads it again and doesn't load the cached files. For example:The recommended thing is to have some kind of function so that after the question mark I generate a random number.
The advantage of using this functionality is that it requires no user intervention.
Update
Since you are using a PHP solution, it would be best to generate a dynamic number to the version of the JavaScript or CSS file to ensure that the file is always loaded from the server and not from the browser's cache using the function
rand()
to generate a random number.For example:
You can test this way if your file has a .php extension :
And if you want to do it from JS, maybe something like this simple would help:
Just remember that if you use JS code, your JS or CSS files should be loaded first, then immediately update the src or href attribute .
If you always want to avoid the cache, you could do:
Or else dynamically make the reference, remember that the variable
estilos
could be a collection of strings.What I do is the following:
I verify that there are no errors in the code.
I use the combination Control + shift + R
I open the console and go to the network section, followed by pressing F5 to see if the files are imported correctly.
Another way to avoid the cache of, for example, your css or js, would be modifying the .htaccess of your apache, this to prevent it from being cached or limit the time of life, this would help you to download your js every less time and css status code = 200 and not 304, which is what it receives when the resource is already in cache.
either
If you use an apache server on linux you can do the following:
activate mod_expire :
modify the /etc/apache2/apache2.conf file and add the following:
Then restart the apache server:
with this you should resolve the cache on the client. however you should be aware that on a production server you should increase the value for the ExpiresDefault variable .
Another option you could apply is to automatically version your
CSS y JS
.OPTION 1:
Add the
versión
(eg: date of last modification of the file ) as a variable to the filepath
.So:
We create a function that gets the last modified date (eg:
filemtime
) of the file and concatenates it as a variable to thepath
.Then we call this function on the files to version.
The result would be something like this:
OPTION 2
Assuming that option 1 "doesn't always work for you" , and following the same line, we can add the
versión
as part of the file name and by.htaccess
rewriting thepath
of these.So:
We create a function that gets the last modified date (eg:
filemtime
) of the file and includes it as part of the file name.Then we call this function on the files to version.
The result would be something like this:
We create a file
.htaccess
a rule to translate these "routes"