gzip is on, I heard that if you minify css and js files and create them into binary files (.gz), the server will speed up. Here php has a gzwrite function that can write such data, but how will the server read it or how to use it correctly to speed up the site?
2 answers
- The same amount of styles / scripts in one request will be faster than in several. Well, with a reasonable amount (and if it is unreasonable, optimization will not help you).
- Minification will reduce the amount of code sent by, at a minimum, eliminating unnecessary spaces. For javascript it is also possible to change the names of local variables and some other things in order to further compress it.
- Compression (for example, gzip) reduces the available volume by archiving. This speeds up its transmission over the network, but the browser will need time to decompress. Ideally, you should evaluate this time and decide whether to use compression.
- Compression on the fly uses server resources. It is better to execute it once and while the file does not change just send the finished compressed version. In theory, the server you use should be able to do it yourself if you enable compression in the settings.
- Point 4 concerns also minification. However, minification is usually performed during the build process, and is not specified in the server settings.
|
There is a library Minify , you simply connect it to your page and specify which files to compress. For example:
<link rel="stylesheet" type="text/css" href="http://site.ru/min/index.php?charset=UTF-8&b=template/css&f=style1.css,style2.css" media="all" /> <script type="text/javascript" src="http://site.ru/min/index.php?charset=UTF-8&b=template/js&f=project.js,project2.js" charset='UTF-8'></script>
And for faster page loading, it is better to connect js before the closing body tag, so the browser will load the page for the user faster and then start downloading js.
|