The browser needs styling and scripting information to complete the rendering of a given site. When a visitor calls your site for the first time, he has to load all static files one after another with a spare parallelization option. Anyway, every further click should be faster, even on an unoptimized site. But as long as not all data has been delivered to the client, the user sees the previous site or even worse, a blank page. The problem comes from the fact, that browsers are allowed to open only a small number of parallel connections to one domain. Newer browsers have increased this value, but the main problem remains the same: Too many connections.
I started with a simple PHP script which did exactly the same as described on sitepoint, but when I started hacking some lua scripts to handle special requests (for example, my already published approach of dynamic thumbnail generation) directly inside of lighttpd using mod_magnet, I also added the combining script with a bit more intelligence. Thus, the script optimizes and caches the data on several levels beside the actual combining process. If you do not want to use lighttpd as webserver, you can also use mod_concat for Apache or a PHP script like the one of Niels Leenheer and adapt it to your needs.
Okay, I think we had enough preface. Let's dive a bit deeper. We assume, we have the following <link>/<script> tags to bind the corresponding files into the current document:
Our goal is to reduce the number of requests and more over to achieve a high cache hit rate. Thus, we should not stack too many individual files in one global file (okay you could also stack everything in one file, but remember: Internet Explorer truncates the content after a certain size), but files we use on some groups of undersides more often are good candidates to get into one group. My proposal for the example above would be:
To get the script working, you have to add the following line to your lighttpd.conf, assumed that your configuration directory of lighttpd is /etc/lighttpd:
magnet.attract-physical-path-to = ( "/etc/lighttpd/combine.lua" )
You might also be interested in the following
- Dynamic thumbnail generation on the static server
- Debian lighttpd startup script
- Faster PHP behind FastCGI
- Add HTTP Response Headers in lighttpd
Sorry, comments are closed for this article. Contact me if you want to leave a note.