开发者

Apache server-side files caching via .htaccess?

开发者 https://www.devze.com 2022-12-28 23:35 出处:网络
I\'m starting new website and gonna includ开发者_如何学编程e several JS libs and would like to know how .htaccess file template should look like with caching of media and JS files on?
  1. I'm starting new website and gonna includ开发者_如何学编程e several JS libs and would like to know how .htaccess file template should look like with caching of media and JS files on?

  2. Whats better for compression, GZip or Deflate?

  3. Is it better/faster solution to serve those JS libs off the Google CDN perhaps then locally?

I'm asking CDN question since some of scripts served off GoogleCDN are potentially going to update and eventually break the website layout so i thought it would be better for me to host them locally and cache via webserver if its going to work with same/near-same speed.


(Sorry rep is too low to post comment)

In terms of including several js files there are some things that you should keep in mind re building the site. First, each of those files is a request that will have some overhead on your server if you serve them. Even on a cdn it will still be a request that the client needs to make. If you have your site setup with say a cookie then every request will have a bit more overhead because of it. I mention this because of your caching because you asked re CDN. What I mean is you should consider first where you are going to serve the content from and then caching. Personally I have setup a second sub url to serve this type of content, so static.website.com. This way the content has less overhead in the request and you can take advantage of parellel downloads. So re the Google CDN, yeah sure if you have most stuff there. If you are afraid of it changing or want to just host it locally then I would recommend you setup a cookie free domain for that purpose.

That brings me to caching and compression. If you have many files then you might want to consider combining them all into one and serve that up. What I do is get the 15 or so js files that we use, put them all into one file and hash it and make the hash the file name. (Note, this is a small thing I wrote for the build step before deploy). This way if any of the files change then the hash changes and you will serve the new content straight away. Your danager of caching the js files for say a year is that you have no control re the client machine to get the new file, unless you change the name of it. Those are sort of things to keep in mind re setting up caching and cdn and multiple js files

Personally, like I said, I combine all js files into one and then compress them to a min file (ajax minifier), and use that. I do the same for css files. This sits on a sub domain and gets served 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 crap load more... but you get the idea

Final note, if you are building a mobile site, you really must remember the key thing being keep bandwidth cost low. How does this affect the answer? Well for my mobile site, I only have 2-3 custom js files which I serve in the same way, but for the jquery-mobile ones I serve this from the offical repo. Why? because the client's mobile will already have it cached and hence it saves bandwidth for them. For a desktop I don't care as much but this makes a big difference in the mobile world.

I hope this help, let me know if you need more info on something,

Cheers

Robin

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号