开发者

How to handle javascript & css files across a site?

开发者 https://www.devze.com 2023-01-03 02:37 出处:网络
I have had som开发者_运维问答e thoughts recently on how to handle shared javascript and css files across a web application.

I have had som开发者_运维问答e thoughts recently on how to handle shared javascript and css files across a web application.

In a current web application that I am working on, I got quite a large number of different javascripts and css files that are placed in an folder on the server. Some of the files are reused, while others are not.

In a production site, it's quite stupid to have a high number of HTTP requests and many kilobytes of unnecessary javascript and redundant css being loaded. The solution to that is of course to create one big bundled file per page that only contains the necessary information, which then is minimized and sent compressed (GZIP) to the client.

There's no worries to create a bundle of javascript files and minimize them manually if you were going to do it once, but since the app is continuously maintained and things do change and develop, it quite soon becomes a headache to do this manually while pushing out new updates that features changes to javascripts and/or css files to production.

What's a good approach to handle this? How do you handle this in your application?


I built a library, Combres, that does exactly that, i.e. minify, combine etc. It also automatically detects changes to both local and remote JS/CSS files and push the latest to the browser. It's free & open-source. Check this article out for an introduction to Combres.


I am dealing with the exact same issue on a site I am launching.

I recently found out about a project named SquishIt (see on GitHub). It is built for the Asp.net framework. If you aren't using asp.net, you can still learn about the principles behind what he's doing here.

SquishIt allows you to create named "bundles" of files and then to render those combined and minified file bundles throughout the site.


CSS files can be categorized and partitioned to logical parts (like common, print, vs.) and then you can use CSS's import feature to successfully load the CSS files. Reusing of these small files also makes it possible to use client side caching. When it comes to Javascript, i think you can solve this problem at server side, multiple script files added to the page, you can also dynamically generate the script file server side but for client side caching to work, these parts should have different and static addresses.


I wrote an ASP.NET handler some time ago that combines, compresses/minifies, gzips, and caches the raw CSS and Javascript source code files on demand. To bring in three CSS files, for example, it would look like this in the markup...

<link rel="stylesheet" type="text/css"
      href="/getcss.axd?files=main;theme2;contact" />

The getcss.axd handler reads in the query string and determines which files it needs to read in and minify (in this case, it would look for files called main.css, theme2.css, and contact.css). When it's done reading in the file and compressing it, it stores the big minified string in server-side cache (RAM) for a few hours. It always looks in cache first so that on subsequent requests it does not have to re-compress.

I love this solution because...

  • It reduces the number of requests as much as possible
  • No additional steps are required for deployment
  • It is very easy to maintain

Only down-side is that all the style/script code will eventually be stored within server memory. But RAM is so cheap nowadays that it is not as big of a deal as it used to be.

Also, one thing worth mentioning, make sure that the query string is not succeptible to any harmful path manipulation (only allow A-Z and 0-9).


What you are talking about is called minification.

There are many libraries and helpers for different platforms and languages to help with this. As you did not post what you are using, I can't really point you towards something more relevant to yourself.

Here is one project on google code - minify.

Here is an example of a .NET Http handler that does all of this on the fly.

0

精彩评论

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

关注公众号