用HttpCombiner来减少js和css的请问次数
HttpCombiner也不记得是谁写的了,功能是把多个js文件或css文件合并到一块,压缩一下一起发给客户端来优化网站。
用法是这样的: <script type="text/javascript" src="/Part/Handle/HttpCombiner.ashx?t=js&s=a.js,b.js,dialog/c.js,dialog/d.js"></script> 但这样又不利于找错,所以在中间又加了一个方法,可随时控制是如上引用还是,如下一般引用: <script type="text/javascript" src="/RES/JS/a.js"></script>
<script type="text/javascript" src="/RES/JS/b.js"></script>
<script type="text/javascript" src="/RES/JS/dialog/c.js"></script>
<script type="text/javascript" src="/RES/JS/dialog/d.js"></script> 修改后引用文件时: <%= HttpCombiner.Requires(true,"js","a.js", "b.js", "dialog/c.js", "dialog/d.js")%> 第一个参数来控制合并 。 一般处理程序cs源码: 复制代码
using System;
using System.Web;
using System.Net;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Web.Caching; public class HttpCombiner : IHttpHandler
{
#region Config
private const bool DO_GZIP = true;
private readonly static TimeSpan CACHE_DURATION = TimeSpan.FromDays();
private const string JSPathPre = "~/RES/JS/";
private const string CSSPathPre = "~/RES/CSS/";
private const string CombinerUrl = "/Part/Handle/HttpCombiner.ashx";//些handler路径 private const string JSAbsPathPre = "/RES/JS/";
private const string CSSAbsPathPre = "/RES/CSS/";
#endregion #region Requires 默认combin
public static string Requires(bool combin, string type, params string[] files)
{
if (combin)
{
if (type == "js")
{
return string.Format("<script type=\"text/javascript\" src=\"{0}?t=js&s={1}\"></script>", CombinerUrl, string.Join(",", files));
}
else if (type == "css")
{
return string.Format("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}?t=css&s={1}\" />", CombinerUrl, string.Join(",", files));
}
else
{
return string.Empty;
}
}
else
{
if (type == "js")
{
StringBuilder sb = new StringBuilder();
foreach (var file in files)
{
sb.AppendFormat("<script type=\"text/javascript\" src=\"{0}{1}\"></script>", JSAbsPathPre, file);
sb.AppendLine();
}
return sb.ToString();
}
else if (type == "css")
{
StringBuilder sb = new StringBuilder();
foreach (var file in files)
{
sb.AppendFormat("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}{1}\" />", CSSAbsPathPre, file);
sb.AppendLine();
}
return sb.ToString();
}
else
{
return string.Empty;
}
}
}
public static string Requires(string type, params string[] files)
{
return Requires(true, type, files);
}
#endregion #region Process
public void ProcessRequest(HttpContext context)
{ HttpRequest request = context.Request; string setName = request["s"] ?? string.Empty;
string contentType = request["t"] ?? string.Empty;
if (string.IsNullOrEmpty(contentType))
{
contentType = "type/javascript";
}
else
{
if (contentType == "js")
{
contentType = "type/javascript";
}
else if (contentType == "css")
{
contentType = "text/css";
}
else
{
contentType = "text/plain";
}
}
bool isCompressed = DO_GZIP && CanGZip(context.Request); UTF8Encoding encoding = new UTF8Encoding(false); if (!WriteFromCache(context, setName, isCompressed, contentType))
{
System.Collections.Generic.List<string> dependencyFiles = new System.Collections.Generic.List<string>();
using (MemoryStream memoryStream = new MemoryStream())
{
using (Stream writer = isCompressed ?
(Stream)(new GZipStream(memoryStream, CompressionMode.Compress)) :
memoryStream)
{
string[] fileNames = setName.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string fileName in fileNames)
{
byte[] fileBytes = GetFileBytes(contentType, context, fileName.Trim(), encoding, dependencyFiles);
writer.Write(fileBytes, , fileBytes.Length);
}
writer.Close();
}
byte[] responseBytes = memoryStream.ToArray();
context.Cache.Insert(GetCacheKey(setName, isCompressed), responseBytes, new CacheDependency(dependencyFiles.ToArray()), System.Web.Caching.Cache.NoAbsoluteExpiration, CACHE_DURATION);
WriteBytes(responseBytes, context, isCompressed, contentType);
}
}
} private static byte[] GetFileBytes(string contentType, HttpContext context, string virtualPath, Encoding encoding, System.Collections.Generic.List<string> depencesFile)
{
if (virtualPath.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase))
{
using (WebClient client = new WebClient())
{
return client.DownloadData(virtualPath);
}
}
else
{
if (!virtualPath.StartsWith("~/", StringComparison.InvariantCultureIgnoreCase))
{
if (contentType == "text/css")
{
virtualPath = CSSPathPre + virtualPath;
}
else if (contentType == "type/javascript")
{
virtualPath = JSPathPre + virtualPath;
}
}
string physicalPath = context.Server.MapPath(virtualPath);
depencesFile.Add(physicalPath);
byte[] bytes = File.ReadAllBytes(physicalPath);
return bytes;
}
} private static bool WriteFromCache(HttpContext context, string setName,bool isCompressed, string contentType)
{
byte[] responseBytes = context.Cache[GetCacheKey(setName, isCompressed)] as byte[];
if (null == responseBytes || == responseBytes.Length) return false;
WriteBytes(responseBytes, context, isCompressed, contentType);
return true;
} private static void WriteBytes(byte[] bytes, HttpContext context,bool isCompressed, string contentType)
{
HttpResponse response = context.Response;
response.AppendHeader("Content-Length", bytes.Length.ToString());
response.ContentType = contentType;
if (isCompressed) response.AppendHeader("Content-Encoding", "gzip");
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.Add(CACHE_DURATION));
context.Response.Cache.SetMaxAge(CACHE_DURATION);
context.Response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
response.OutputStream.Write(bytes, , bytes.Length);
response.Flush();
} private static bool CanGZip(HttpRequest request)
{
string acceptEncoding = request.Headers["Accept-Encoding"];
if (!string.IsNullOrEmpty(acceptEncoding) && (acceptEncoding.Contains("gzip") || acceptEncoding.Contains("deflate")))
return true;
return false;
} private static string GetCacheKey(string setName, bool isCompressed)
{
return "HttpCombiner." + setName + "." + isCompressed;
} public bool IsReusable
{
get
{
return true;
}
}
#endregion
复制代码
}
用HttpCombiner来减少js和css的请问次数的更多相关文章
- Web性能优化-合并js与css,减少请求
Web性能优化已经是老生常谈的话题了, 不过笔者也一直没放在心上,主要的原因还是项目的用户量以及页面中的js,css文件就那几个,感觉没什么优化的.人总要进步的嘛,最近在被angularjs吸引着,也 ...
- IE和firefox火狐在JS、css兼容区别
1.firefox不能对innerText支持. firefox支持innerHTML但却不支持innerText,它支持textContent来实现innerText,不过默认把多余的空格也保留了. ...
- [转][前端优化]使用Combres合并对js、css文件的请求
本文转自:http://www.cnblogs.com/parry/archive/2011/01/28/Reduce_Http_Request_Using_Combres_For_Js_Css.ht ...
- 将Microsoft Ajax Minifier集成到VS2013对JS、CSS进行编译时压缩
在网站发布中,一般要将js,css文件压缩减少体积,以减少在HTTP请求中的流量.将Microsoft Ajax Minifier集成到VS2013中就可以对JS.CSS进行编译时压缩. VS2013 ...
- MVC中的JS和CSS压缩
小说一下Js和CSS压缩的好处: 1.减小了文件的体积 2.减小了网络传输量和带宽占用 3.减小了服务器的处理的压力 4.提高了页面的渲染显示的速度 很多建议将站点的静态文件(如图片.js.css ...
- 为js和css文件自动添加版本号
web应用必然要面对缓存问题,无论前台后台都会涉足缓存.特别是对于前端而言,缓存利用的是否得当直接关系到应用的性能. 通常情况下,我们会倾向于使用缓存,因为缓存一方面可以减少网络开销,一方面可以减轻服 ...
- nginx js、css多个请求合并为一个请求(concat模块)
模块介绍 mod_concat模块由淘宝开发,目前已经包含在tengine中,并且淘宝已经在使用这个nginx模块.不过塔暂时没有包含在nginx中.这个模块类似于apache中的modconcat. ...
- PHP中,JS和CSS优化工具Minify的使用方法
为减少HTTP请求,我们往往需要合并和压缩多个JS和CSS文件,下面记录下网上关于实现这个功能的PHP源码以及开源项目Minify的使用方法 一.实现合并和压缩多个JS和CSS文件的代码请参考 1.一 ...
- 经验总结:按需加载JS和css
项目中做过这样的事情:所有页面都通过SSI指令 include这样一份public-js.shtml, 用来引入涉及到的js(包括公共的脚本 验证插件 自定义组件等),但是一些没有交互效果的页面根本不 ...
随机推荐
- debug时红点消失
问题描述:debug时红色断点和黄色小箭头不见,而用行代码高亮的形式时. 解决办法:可以用设置 工具 => 选项 => 文本编辑器 => 指示器边距 勾上选项
- js常用操作~~~~将持续更新
1.替换多个模板变量 var s="my javascript is very poor,who can help me?" var reg=/(\w*)my(.*)is(.*)c ...
- java中使用String的replace方法替换html模板保存文件
在我们的D盘下有这样一个html模板,现在我们要做的就是解析news.template文件,从数据库中提取数据将数据添加到指定的模板位置上 <head> <title>{tit ...
- Scala-基础-变量与常量
import junit.framework.TestCase import org.junit.Test //变量 //var 代表变量 //val 代表常量 //关键字 class,extends ...
- 通过yum命令搭建lamp环境(centos6.5)
centos 6.5 1.yum安装和源代码编译在使用的时候没啥区别,但是安装的过程就大相径庭了,yum只需要3个命令就可以完成,源代码需要13个包,还得加压编译,步骤很麻烦,而且当做有时候会出错,源 ...
- html——表单控件
基本的表单控件还有html5的一些新的表单控件: <!DOCTYPE html> <html> <head> <meta charset="utf- ...
- 控制台——EventLog实现事件日志操作
我们应该如何通过写代码的方式向其中添加“日志”呢? 在操作之前,先明确几个概念: 1:事件日志名(logName):“事件查看器”中的每一项,如“应用程序”.“Internet Explorer”.“ ...
- 【转载】HTTP 请求头与请求体
原文地址: https://segmentfault.com/a/1190000006689767 HTTP Request HTTP 的请求报文分为三个部分 请求行.请求头和请求体,格式如图:一个典 ...
- rrdtool 实践
rrdtool 实践 rrdtool 参数比较多,如果直接看文档,不知从何入手,直接从例子入手这样容易理解,模拟网卡流量 1. 创建数据库 rrdtool create Flow.rrd --star ...
- POJ_3013_最短路
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23630 Accepted: 5 ...