Introduction
Use many small JS and CSS files to replace a large JS or CSS fileCodeIt is a good practice to achieve better maintainability. However, in turn, the performance of the website is lost. Although you should write your JavaScript code in a small file and split the large CSS file into small files, when a browser requests those JS and CSS files, it generates a request for each file. Each HTTP request will result in a "round-trip" from your browser to the server. The waiting time between the response server and the client browser is called "delay ". Therefore, if you have four JS files and three CSS files that need to be loaded by pages, you will have to wait for seven "round trips" on the network ". In China, the average latency is 70 ms. Therefore, the total latency is 490 ms, about half a second. The average latency of access from outside China is about Ms. Therefore, it means a waste of MS of time. The page will not be fully displayed until the CSS and JS files are fully loaded. Therefore, the longer the latency, the slower the page loading.
How bad the latency is
Here is an image showing how each request produces a "latency". These "latencies" have a significant impact on page loading:
You can use CDN (content delivery network) to reduce the waiting time. However, a better solution is to use httphandler to provide one request for multiple files. The httphandler integrates several files and provides one output. Therefore, instead of many <scropt> or <link> labels, you only need to write a <scropt> and <link> label and mark them in httphandler. It is up to you to tell handler which files need to be integrated and which files provide one output. This saves the latency of sending many requests from the browser.
Here you can see how performance can be improved if you integrate multiple JS files and CSS files into one output.
On a common webpage, you will see a lot of JS references:
However, we can use only one <SCRIPT> tag to request the collection of the entire JS file, instead of each <SCRIPT> tag here:
Httphandler reads the file names defined in a configuration file, integrates all those files, and responds to the client once. It compresses the response content through gzip to save bandwidth. In addition, it provides a cache Response Request Header to cache the response to the browser's cache, so that the browser does not need to request it again for subsequent access.
In the request parameters, you can use the "S" parameter to identify the name of the file set, use the "T" parameter to identify the content type, and then use the "v" parameter to identify a version. Because the response is cached, If you modify any of the file sets, you will have to add the value of the parameter "V" for the browser to download the response again.
With the httphandler, you can request the CSS file as follows:
How to define the request set is listed here, in Web. config:
Example of using httphandler Integrator
I have built a simple test website to show you how to use it. The test website has two CSS and JS files. Default. aspx uses only one <link> and <SCRIPT> tag to request them through httpcombiner. ashx.
The content of the default. aspx file is as follows:
As you can see, there is a <link> label to httpcombiner. ashx sends a request and provides the name of the request set-set_css. Of course, there is also a <SCRIPT> tag that requests a set of set_javascript.
The preceding two sets are defined in the web. config file:
Here we list how handler works:
(1) First, it reads the name of the file set from the "S" parameter.
(2) then it obtains the set definition from the Web. config file.
(3) It reads every file and then caches them in the buffer.
(4) The buffer is then compressed using gzip.
(5) The contents in the compressed buffer will be sent to the browser.
(6) The content in the compressed buffer is stored in ASP.. Net buffer, so that subsequent requests to the same set can directly obtain data from the cache, rather than reading each file from the file system or external URL.
Handler benefits:
(1) It reduces the number of "round-trips" on the network. The more files you put into a collection, the more you can reduce network latency and improve performance.
(2) It caches all the integrated compression responses, saving you from reading the file system and compressing it again and again. It provides scalability.
How httphandler works
First, Handler reads the set name, type, and version from the request string:
If the file set to be loaded has been cached, it will directly write the response stream from the cache. Otherwise, the file will be loaded one by one and then stored in a memorystream. Memorystream is compressed by gzipstream (if the browser supports compressed output ).
After all the files are integrated and compressed, the integrated byte stream is cached so that subsequent requests can directly obtain data from the cache.
The getfilebytes method reads a file or URL and returns bytes. Therefore, you can use a virtual path on your website, or you can use a URL to point to the JS/CSS file of a host in another domain.
There are many tips for the writebytes method. It provides a response header based on whether bytes are compressed. Then it provides a cache ID header for the browser to cache the response content.
How to use this handler?
- Include httpcombiner. ashx in your project
- The definition file set is in the <etettings> node of your web. config configuration file.
- The changed <link> and <SCRIPT> labels use the format required by httpcombiner. ashx: httpcombiner. ashx? S = <setname> & t = <contenttype> & V = <versionno>
Link: http://www.codeproject.com/KB/aspnet/HttpCombine.aspx Source code:Http://download.csdn.net/detail/yanghua_kobe/3654006
Translator's note: I encountered a problem when trying to use this technology today. That is, when packing and compressing CSS files. If the file involves the image path (such as the URL attribute of background-IMG ). An error occurred while requesting images. The reason is that these images usually use relative paths. After the browser downloads and obtains the CSS file, it uses the CSS file as a reference and searches for the image based on the relative path of the image. During batch packaging, the path used for reference is changed to the handler path, which leads to an error in the image search path and cannot be downloaded. In fact, even if you manually set the image path to a relative path relative to the current handler, the image cannot be downloaded!
Solution: Use an absolute path, but it also depends on the website publishing method. If you create a new website at the time of release, you can directly use "/" to express the absolute path of the background image in CSS from the root directory. When creating a website, you can use "/" to identify the root directory. When creating a virtual directory, you must add the folder name of the new virtual directory. For example, if myweb is used, the absolute path is "/myweb/images ......" to get the image correctly.