MyBB Community Forums

Full Version: Add option to cache CSS
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I think it's good idea to add option to simply cache styles on users browsers.
In css.php find:

header("Content-type: text/css"); 

change to for examample:
header("Content-type: text/css"); 
header("Pragma: public");
header("Cache-Control: maxage=" . $mybb->settings['css_expire_time']);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $mybb->settings['css_expire_time']) . ' GMT'); 

and of course add setting option to "Server and Optimization Options" (0, one hour [3600], etc).

Why this?
Becuase it's very simple, and in most cases, admins don't change styles often. If there is 3-4 css styles, on any request MyBB must initialize, include all plugins files, connect databse etc. (a lot of I/O for only CSS). I'ld like to do this in plugin, but there is no hook on css.php and init.php.

It doesn't conflict with many themes, because each request has other style id. Also, it of course work in MyBB 1.6.
css.php is a fallback. It's only used when ./cache/ isn't writable. There is a "css_start" hook which might be suitable here. It's not recommended to use css.php for serving content though.
Yes, but if styles are inherited, MyBB uses css.php file.
You can see this on polish MyBB support - we have 4 themes, for each season.
It's working fine here:
<link type="text/css" rel="stylesheet" href="http://community.mybb.com/cache/themes/theme2/global.css" />
<link type="text/css" rel="stylesheet" href="http://community.mybb.com/cache/themes/theme1/star_ratings.css" />
<link type="text/css" rel="stylesheet" href="http://community.mybb.com/cache/themes/theme1/showthread.css" />
From MyBBoard.pl:

<link type="text/css" rel="stylesheet" href="https://forum.mybboard.pl/cache/themes/theme17/global.css" />
<link type="text/css" rel="stylesheet" href="https://forum.mybboard.pl/cache/themes/theme17/mybbpl.css" />
<link type="text/css" rel="stylesheet" href="https://forum.mybboard.pl/css.php?stylesheet=146" />
<link type="text/css" rel="stylesheet" href="https://forum.mybboard.pl/css.php?stylesheet=94" />
<link type="text/css" rel="stylesheet" href="https://forum.mybboard.pl/css.php?stylesheet=130" />
<link type="text/css" rel="stylesheet" href="https://forum.mybboard.pl/css.php?stylesheet=162" />

It works without css.php only if all themes has their styles.
Now, they are inherited and MyBB uses css.php