Reputation: 607
Based on Google page speed tool, I should leverage browser caching. How do I do that for my site? I just need to cache all assets excluding the static HTML page. What codes do I add? And where do I add these code in the html page?
Upvotes: 3
Views: 3286
Reputation: 401182
In order to have the browser cache the static elements (typically: CSS, JS, images), you must send the required HTTP headers for those elements.
Note that it has nothing to do with your HTML page : CSS/JS/images are fetched using distinct HTTP request -- one per asset.
What you need to do is configure your Webserver, so it send HTTP headers to indicate the browser it should cache those items.
Typically, if using Apache, you'll work with mod_expires
, and you'll use some configuration block like this one :
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 6 hours"
ExpiresByType application/javascript "access plus 6 hours"
ExpiresByType image/gif "access plus 1 weeks"
ExpiresByType image/png "access plus 1 weeks"
ExpiresByType image/jpeg "access plus 1 weeks"
</IfModule>
Note: you'll probably want to change the caching durations, to fit your needs.
Upvotes: 5