MyBB Community Forums

Full Version: Preventing CSS Files From Getting Cached
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Ok this is one thing that has REALLY annoyed me. I am using nginx and its not even using caching yet if I clear my cache it does not update.

So what I have done that you can do as well is use the query string trick to force an update. We are going to append a timestamp to the file as dateline=timestamp.

Open up global.php. Find:

$stylesheets .= "<link type=\"text/css\" rel=\"stylesheet\" href=\"{$mybb->settings['bburl']}/{$page_stylesheet}\" />\n"; 
on around line 236.

replace it with:

$stylesheets .= "<link type=\"text/css\" rel=\"stylesheet\" href=\"{$mybb->settings['bburl']}/{$page_stylesheet}?dateline=".time()."\" />\n";

Save it and now when ever you change a css file it will always been showing without clearing your cache as the browser sees it as a new file.

You could also do something like x=randomint&y=randomint but i think this way is simpler and guaranteed that you will nefer get 2 of the same numbers twice Smile.

Also I would like to see this in the offical MyBB builds as it is useful, so think about it devs Smile.

Peace..
It may be useful in initial development but you could easily do ctrl + f5 which has your browser request it again anyway. And, on a live site you would want the css to be cached.
(2012-06-10, 09:20 PM)Alex Smith Wrote: [ -> ]It may be useful in initial development but you could easily do ctrl + f5 which has your browser request it again anyway. And, on a live site you would want the css to be cached.

I have been looking at this today and i have a question about this. Why would this be a bad idea to do the above? I mean the browser still caches the file and if you dont do a huge amount of updates wouldn't this improve the experience for users?

I mean if you give it a fixed value and not like above with the time() function but instead do something like:

$stylesheets .= "<link type=\"text/css\" rel=\"stylesheet\" href=\"{$mybb->settings['bburl']}/{$page_stylesheet}?vers="001"\" />\n"; 

Looking at this you could probably even make it a plugin to achieve this and simply adjust the number when you need it. After adjusting the css significantly for example.
Caching reduces load times greatly. So if you are not updating your stylesheets daily or every now and then, this is not recommended as the browser will load all the resources which can be cached, on every new page load.
(2012-11-06, 01:57 PM)crazy4cs Wrote: [ -> ]Caching reduces load times greatly. So if you are not updating your stylesheets daily or every now and then, this is not recommended as the browser will load all the resources which can be cached, on every new page load.

No no i mean if you make it a fixed value and not with the time like the above. Just when you have done significant changes one day it would be an easy solution to force a recache of the site. By changing the version number.
As such a quick refresh should clear cache. Or you can go to the css file and do a force refresh (SHIFT + F5) and it should do the job, unless you're using cloudflare for which you'll need to purge cache from CF's website.
(2012-11-06, 02:04 PM)crazy4cs Wrote: [ -> ]As such a quick refresh should clear cache. Or you can go to the css file and do a force refresh (SHIFT + F5) and it should do the job, unless you're using cloudflare for which you'll need to purge cache from CF's website.


While true for normal users its still not instantly and if i needed to guess more then 50% doesnt hit shift+f5 much. So giving it a version would make sure its forced for all the users what ever happens.
This happens to me all the time Big Grin

I am ALWAYS fiddling with the css - to such an extent that the leefish site unofficial motto is "Press Shift+F5".

It would be handy to have a quick and simple version control, so what should be cached IS cached - but after I have made a change on the css my users will get an updated css file without having to hit F5.
(2012-11-06, 03:02 PM)Leefish Wrote: [ -> ]This happens to me all the time Big Grin

I am ALWAYS fiddling with the css - to such an extent that the leefish site unofficial motto is "Press Shift+F5".

It would be handy to have a quick and simple version control, so what should be cached IS cached - but after I have made a change on the css my users will get an updated css file without having to hit F5.

Exactly i think this would be a really nice feature to add to the core of MyBB. But for now i think doing this:

Replace:
{$stylesheets}

With:
<link type="text/css" rel="stylesheet" href="{$mybb->settings['bburl']}/{$page_stylesheet}?vers=001">

Would.. well should work to not sure though.

edit COould also be that this messes up the stylesheets for other pages though :/
(2012-11-06, 03:02 PM)Leefish Wrote: [ -> ]This happens to me all the time Big Grin

I am ALWAYS fiddling with the css - to such an extent that the leefish site unofficial motto is "Press Shift+F5".

It would be handy to have a quick and simple version control, so what should be cached IS cached - but after I have made a change on the css my users will get an updated css file without having to hit F5.

This. I am constantly changing the CSS (even if they're very minor changes), and I don't like having to tell my users to hit Ctrl+F5. I go as far as to import my theme again so that users don't have to do this. I always want them seeing the latest version.
Pages: 1 2