Store CSS as files


Well-known member
As it is right now, XenForo does deliver CSS through css.php.
This does work just fine, gives a lot of flexibility and the generated CSS can be cached efficiently by browsers.

However, doing it this way does require some PHP processing for each CSS call (there are typically 2 calls per page).
Also webservers will perform compression on every request.

While it is possible to setup a reverse proxy in front of css.php to avoid PHP processing and recompressing for every request, this is rather complicated and not smth. a lot of customers could use (due to shared hosting, etc.)

If CSS was stored as files, PHP processing (and compression if storing pre-compressed files and the webserver is configured to support that) could be avoided.
This was already suggested several years ago but rejected due to the dynamic nature of CSS URLs.

It would be nice if this idea could be given a second thought as I think it should be possible without much hassle:
  1. Just before delivering the final CSS to the browser though css.php, write the CSS to a file (ideally uncompressed, compressed with gzip and compressed with brotli if available) within data.
  2. Change the HTML to always point to those files
  3. Have webserver rewrite rules in place to rewrite those requests to css.php in case the file does not exist
  4. Add code to cleanup the generated files when CSS changes
This way, if the files do exist the webserver can just serve them (precompressed), if they do not exist they will be created on the fly.
Also this approach should be easy enough so it could benefit a lot of customers.

We are using this approach in production and it does seem to work just fine :)
Upvote 13