Jump to content

CSS forced reload

Recommended Posts

CSS forced reload

I was having a lot of trouble getting Edge (with chrome core) and Chrome to reload css after making changes. Even to the point of deleting and replacing the css file and still no luck.
After searching, I found this site. Original source link below.


The solution!

Requesting file.css and file.css?rnd=23 will get you the contents of the same file but those two requests are not the same! Both the browser and the server see them as requests for two different resources. That’s the whole premise behind our solution. Credits for it go to Paul Irish who wrote about the method back in 2008.

With the magic of a few lines of JavaScript, we find all CSS files referenced on the page (all <link rel="stylesheet"> elements) and then change their URLs. Changing /some-path/style.css to /some-path/style.css?rnd=132 forces the browser to re-request the file from the server. Because of the rnd part, neither the browser or the server have that request in the cache and are forced to grab a fresh copy of the file! Simple, fast, universal and clean solution!

I post here in case it helps others.
Apologies if this is not the right thing to do. Admins can delete if necessary.

Original source

Share this post

Link to post
Share on other sites

If you are doing this on a live or test site on your hosting, have you looked to see whether they are caching the content. You may have to clear the cache on your hosts. If you hace cpanel its easy there.


Get the latest Phoenix version here

It's easy to say to yes every question.

Share this post

Link to post
Share on other sites
$randomnumber = rand(199999);
echo '<link rel="stylesheet" type="text/css" href="stylesheet.css?v='.$randomnumber.'">'
This will do it
Edited by sapguy

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now