user2363939
user2363939

Reputation:

save css files at browser for future use

Is there any way to save the css files of my site that are loaded with the tag at users' browsers for future use. I mean that I do not want the users to reload the css files each time they visit my site cause it makes the site slower. Can I achive this with html, javascript or php?
Thank you for your time!

Upvotes: 0

Views: 112

Answers (1)

jdepypere
jdepypere

Reputation: 3553

It's good practice to leverage browser caching, as can be read here.

How you can do this is by editing your .htaccess file like so (example):

<IfModule mod_expires.c>

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/x-javascript "access 1 month"
ExpiresByType text/javascript "access 1 month"
ExpiresByType image/x-icon "access 1 year"

</IfModule>

As you can see, with this code css files will be cached for 1 month.

Be careful setting your times, because the browser will not reload the file unless the time has expired or the name of the file has changed. An easy way to circumvent this would be to use versions in your file like so: main.css?v=1.0. The upside of this is you don't have to rename your files everytime, but downside is most proxies don't cache resources with a "?" in the url.

Upvotes: 1

Related Questions