转帖:
网站性能优化- JS、CSS的合并、压缩、缓存管理
本篇文章主要讨论下目前JS,CSS 合并、压缩、缓存管理存在的一些问题,然后分享下自己项目中用到的1个处理方案,并提供1个实例下载。
<c:if test="${env=='prod'}"><script type="text/javascript" src="/js/all.js"></script></c:if><c:if test="${env=='dev'}"><script type="text/javascript" src="/js/1.js"></script><script type="text/javascript" src="/js/2.js"></script><script type="text/javascript" src="/js/3.js"></script></c:if>
# 文件合并配置文件,多个文件以|隔开,以/开头的表示从根目录开始,# 空格之后的文件名表示合并之后的文件名
# 把1,2,3合并到all文件中1.js|2.js|3.js all.js
#合并CSS/css/mian.css|/css/common.css all.css
<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><% boolean isDev = false; // 是否开发环境%><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title><% if(isDev){ %><script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.4.2.js"></script><script type="text/javascript" src="<%=request.getContextPath() %>/js/1.js"></script><script type="text/javascript" src="<%=request.getContextPath() %>/js/2.js"></script><link type="text/css" rel="stylesheet" href="<%=request.getContextPath() %>/css/1.css" /><link type="text/css" rel="stylesheet" href="<%=request.getContextPath() %>/css/2.css" /><% }else{ %><script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.4.2.js"></script><script type="text/javascript" src="<%=request.getContextPath() %>/js/all.js"></script><link type="text/css" rel="stylesheet" href="<%=request.getContextPath() %>/css/all.css" /><% } %></head><body><h1 class="c1">Hello World!</h1></body></html>
<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%boolean isDev = false; // 是否开发环境%><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title><% if(isDev){ %><script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.4.2-3gmin.js?99375"></script><script type="text/javascript" src="<%=request.getContextPath() %>/js/1-3gmin.js?90"></script><script type="text/javascript" src="<%=request.getContextPath() %>/js/2-3gmin.js?91"></script><link type="text/css" rel="stylesheet" href="<%=request.getContextPath() %>/css/1-3gmin.css?35" /><link type="text/css" rel="stylesheet" href="<%=request.getContextPath() %>/css/2-3gmin.css?18" /><% }else{ %><script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.4.2-3gmin.js?99375"></script><script type="text/javascript" src="<%=request.getContextPath() %>/js/all-3gmin.js?180"></script><link type="text/css" rel="stylesheet" href="<%=request.getContextPath() %>/css/all-3gmin.css?53" /><% } %></head><body><h1 class="c1">Hello World!</h1></body></html>