In this post I will demonstrate how to compress text content in your web applications or websites, so that your trafficked size is smaller and therefore your application load time as well.
To do this, we will use the Apache web server configuration file, the .htaccess. Remember that files such as videos, music, and other binary files are probably already compressed, and because they are usually much larger than text files, would require a lot of server processing to compress them again, and the gain from compression would be Minimum.
For this reason, it is recommended to activate compression only in text files, and especially in “Big 3” (HTML, CSS and Javascript).
Enabling compression with DEFLATE
This is the easiest compression to configure on the server and is already enabled by default. It uses less server processing than GZip, but does not compress as much as it does.
1 2 3 4 5 6 7 8 9 10 11 12 |
<IfModule mod_deflate.c> # Compactar por tipo - html, text, css, xml AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml # Compactar por tipo - javascript AddOutputFilterByType DEFLATE application/x-javascript application/javascript text/javascript text/x-js text/x-javascript # Compactar por extensão AddOutputFilter DEFLATE js css htm html xml ttf eot </IfModule> |
Enabling Compression with GZip
GZip has been gaining a lot of space on the web today because of its great data compression power, reducing page load times dramatically, especially when we talk about mobile internet (3G) in Brazil, which is still very slow and precarious Many places. Because it has a high level of compression, it requires more processing than the DEFLATE compression method.
1 2 3 4 5 6 7 8 9 10 |
<IfModule mod_gzip.c> mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file \.(html?|txt|css|js|php|pl|ttf|eot)$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/.* mod_gzip_item_include mime ^application/x-javascript.* mod_gzip_item_exclude mime ^image/.* mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* </IfModule> |
I created the .htaccess file with gzip code in the root of the site, but google pagespeed continues to lack compression. What may be happening?