Apache server side file caching via .htaccess?
-
I am starting a new website and am about to include several JS libraries and would like to know what the .htaccess file template looks like with media caching and JS files on?
-
Which is better for compression, GZip or Deflate ?
-
Is this the best / quickest solution to serve these JS libraries from the Google CDN, perhaps locally then?
I am asking a CDN question as some of the scripts served by GoogleCDN are potentially going to update and end up breaking the site layout, so I thought I'd be better off hosting them locally and caching them through a webserver if it works with the same / close to the same speed.
a source to share
(Sorry, it's too late to leave comments)
In terms of including multiple js files, there are some things you should keep in mind when building a website. First, each of these files is a request that will have some overhead on your server if you serve them. Even on cdn, there will still be a request that the client has to fulfill. If you have customization of the site using cookies, then because of this, each request will have a little more overhead. I mention this because of your caching because you asked for re CDN. I mean, you first have to consider where you will collect the content and then cache. Personally, I have a second additional url to serve this type of content, hence static.website.com. This way the content has less overhead per request and you can take advantage of the parellel download.So republish the Google CDN, yes of course, if you have the most there. If you are afraid that it changes or just wants to host it locally, I would recommend that you set a free cookie domain for this purpose.
This brings me to caching and compression. If you have a lot of files, you might consider merging them all into one and filing. What I do is get 15 or so js files that we are using, put them in one file and a hash file, and make a hash of the file name. (Note, this is a small thing I wrote for the build stage before deployment). This way, if any of the files change, the hash will change and you will serve the new content immediately. Your js file caching danager for a year is that you have no control over the client machine to get a new file unless you change its name. These are the things to remember to restore caching and cdn and multiple js files.
Personally, as I said, I combine all the js files into one and then compress them into a min file (ajax minifier) and use that. I am doing the same for css files. It sits on a subdomain and gets there.
In apache, I have the following:
# Enable memory caching
CacheEnable mem /
# Limit the size of the cache to 50 Megabyte
MCacheSize 51200
AddOutputFilterByType DEFLATE application/x-javascript
ExpiresActive On
ExpiresDefault "access plus 1 second"
# hashed file so safe for long out cache
ExpiresByType text/javascript "access plus 1 years"
Along with a shit load more ... but you get the idea
One final note: if you are building a mobile site, you really need to remember that the key thing is low bandwidth. How does this affect the answer? Ok for my mobile site, I only have 2-3 custom js files that I serve the same way, but for jQuery mobiles I serve this from the official repo. What for? because the client mobile will already be caching and therefore it saves bandwidth for them. For the desktop, I don't care, but it makes a big difference in the mobile world.
I hope this help, let me know if you need more information on something,
Greetings
Robin
a source to share