openxpc.source code is poetry!

26Jul/080

optimize web applications [part 1]

A tricky way to optimize web applications is to generate .js and .css files with PHP. The particular rule here is to set the expires header in the future, so the browser isn't downloading these files again till the expires day of the header is arrived. If the browser cache is empty, the browser would cache the files again, otherwise not. A other benefit to use one PHP generated script is the agreement of the browser to load only 2 files parallel from one host. So you have only one .js file to download and one .css. In contrast to download for example 4 .js and 2 .css files in 3 sequences ( 6 files / 2 parallel downloads = 3 sequences) you only use one sequence. But watch at the code to understand: (pay attention to the headers)

Code

<?php
header("Content-type: text/javascript");
header("Cache-Control: cache");
header("Expires: Mon, 1 Jan 2020 00:00:00 GMT");
require("scripts/main.js");
require("scripts2/ajax.js");
?>

A addional trick to get a bit more performance is to generate gzipped files. To compress the file before response use: ob_start("ob_gzhandler"); at the end of the code sequence.

Comments

No comments yet.

Leave a comment