How to Optimize a Website for Google PageSpeed
Let's look at an example of how, and with what tools, you can optimize your website for its Google PageSpeed score.
Enabling GZIP compression
Enabling gzip compression is quite simple: add the following code to the .htaccess file:
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/css text/javascript application/javascript application/x-javascript
</ifModule>
After enabling GZIP compression, the site's score went up from 58 to 79
Enabling browser caching
To enable browser caching, add the following code to the very beginning of the .htaccess file:
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
This code tells the browser exactly what to cache and for how long.
After setting up browser caching, the site's score rose to 81
Image optimization
Image optimization is also an important factor in improving your site's score. For optimization, you can use various software or online services that are always at hand.
Let's look at the "Optimizilla" service
Go to http://optimizilla.com/ru/
Download the folder with your site's images to your computer.
You can see the file paths by expanding the "Optimize images / Show how to fix" tab
To optimize the images, upload the files to the service. You can upload them using the "Upload files" button or by dragging the images onto the form on the site.
P.S. The number of files is limited to 20.
After the images are uploaded, the service shows how much each image file can be compressed and how this will affect image quality.
You can adjust the compression level yourself, separately for each file.
In 90% of cases, the default settings are enough.
When you are done, save the images using the "Download" button under each file, or the "DOWNLOAD ALL" button (clicking "DOWNLOAD ALL" downloads all files as a single archive).
After saving, remove the added "-min" suffix from the file names and upload the files back to the site.
If PageSpeed still lists a file that needs compressing, repeat the compression with a higher compression level.
After optimizing the images, the site's score rose to 87
Minifying CSS and JS (JavaScript) files.
To minify CSS and JS files, we will use the "Online YUI Compressor" online service:
https://ru.piliapp.com/minify/yui-compressor/
Download the *.css and *js files that PageSpeed points to.
1. Upload the files to the service one by one and save the results to your computer
2. Remove the "-min" suffix from the file names
3. Upload the files back
After minifying the *.css and *js files, the site's score rose to 92
The remaining optimization items suggested by PageSpeed are best left alone, since optimizing them requires a specialist.