What is Leverage Browser Caching?
Leverage Browser Caching stores data locally so that future requests for that data can be served faster. A web browser needs to retrieve many resources such as HTML, CSS, JS, text, images, etc. in order to load a website. Web Cache is the technology used in web browsers and software applications to temporally store visited web page resources on a local disc. This temporarily stored data is called “Web Cache” or “HTTP Cache”.
By having this data stored on your visitor’s local hard drive, your webpage will be loaded much faster on their next visit. This is because the data will be pulled directly from their computer instead of having to travel through the internet from a web server.
If you have ever tested your website on Google Page Insights, GTMetrix or Pingdom you might have seen this optimization recommendation. Leverage Browser Caching is an important optimization that will speed things up for your return visitors.
The Easiest Way of Implementing Browser Caching
What we will be outlining below requires making changes to your web site’s .htaccess file. If you’re comfortable with that, you’re good to go. But, we will admit, this isn’t the easiest way to implement browser caching. The easiest way would be to use a caching plugin. Our go-to WordPress caching plugin is WP Rocket.
How to Setup Leverage Browser Caching Without a Plugin
To set up Leverage Browser Caching in WordPress, you will need to set an expiry date or a maximum age in the HTTP headers for static resources in your .htaccess file. This will instruct the browser to load previously downloaded resources from a local disk rather than over the network and how long to use these resources.
There are plugins out there that can enable Leverage Browser Caching for you, but I like to limit the number of plugins that I use to avoid problems and conflicts. Another reason you don’t really need a plugin for this is that enabling Leverage Browser Caching is extremely easy to do yourself. In the steps below, I will show you how to do this without a plugin.
Step 1: Locate your .htaccess File
Login to your cPanel and go to the File Manager. The .htaccess file is hidden by default. You will need to find the settings icon on the top right and change the option to show hidden files.
After you enable Hidden Files (dotfiles) you should be able to find the .htaccess file located in the your public_html folder.
Step 2: Edit your .htaccess File
Right click the .htaccess file and choose edit. Copy and paste the code below inside the file:
# Begin Leverage Browser Caching
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/x-icon "access 1 year"
ExpiresByType image/svg+xml "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresDefault "access 2 days"
# End Leverage Browser Caching
Make sure you save your changes and close the file editor.
Step 3: Make Sure Everything is Working
If everything is functioning correctly, you should no longer see this listed as a recommended optimization. However, you might still have an issue with other external files that you have no control over.