模仿yui將css和js打包,加速網頁速度

來源:互聯網
上載者:User

標籤:des   http   java   os   io   檔案   for   ar   

如果你有機會用firebug看看自己網站的網路請求,你會發現請求數量之多超乎你的想象。為減少這個數量,有許多技術方案。比如yui的combo,會將所有需要的js混合成一個檔案下載,現代web伺服器好像也有這種技術,通過分析網頁的連結,將一些檔案合并。但這是別人實現的,也許不合你的需求。以下代碼是本人在詩篇建站平台上的一個servlet,希望能給你寫類似的代碼起到一些參考作用。 
1、約定格式。這是: http://www.m3958.com/in 的網頁原始碼的片段,你必須在用戶端和伺服器端約定格式。 
<script type="text/javascript" src="/yuicombo?version=2.2&prefix=/codemirror/2.2/&lib/codemirror.js&mode/xml/xml.js&mode/javascript/javascript.js&mode/css/css.js&mode/htmlmixed/htmlmixed.js&lib/util/dialog.js&lib/util/searchcursor.js&lib/util/search.js"></script> 

參數: 
version,如果一個combo請求帶有version參數,作者就應該知道這個url的內容什麼時候改變。這個參數很有用,待會兒繼續說。 
prefix,postfix這個故名思意了,可以大大的縮短url長度。 
separator,如果大部分檔案是具有相同前尾碼,而有的沒有遵循這個約定,也可以混合在一起,就是這個searator之後,不再添加前尾碼。 

那麼version有什麼用呢?既然你知道version和內容變化的對應關係,對於具有version的url請求,你可以設定永久緩衝,比如10年。除此之外,從url產生一個md5值,作為etag發送給瀏覽器。當瀏覽器請求的時候,伺服器如果發現:有version,有etag,那麼最大的可能是使用者強制重新整理瀏覽器,通過比較etag和url的md5值,如果一致,返回304即可。
?1. [代碼][Java]代碼    
if(finalqs.isEmpty()){//在混合請求的情況下,如果缺少某個組件,yui會自動拉去,這個情況下前綴也是yuicombo
    finalqs = req.getRequestURI().replace("/yuicombo", "/yui");
    fns = new String[]{finalqs};
}
 
//client cache 針對url而定,那麼只要url不同,cache就不會hit
//如果某個url帶有version欄位,那麼作者必須知道這個內容的變化,所以可以永久緩衝。
if("css".equals(type)){
    type = "text/css";
}else if("js".equals(type)){
    type = "text/javascript";
}else{
    ;
}
if(type == null || type.isEmpty())type = "text/javascript";
res.setContentType(type + ";charset=utf-8");   
res.setCharacterEncoding("UTF-8");
if(version == null || version.isEmpty()){
    ;
}else{
    String etag = req.getHeader("If-None-Match");
    if(etag != null && etag.equals(shaService.encrypt(finalqs))){
        res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return;http://www.enterdesk.com/special/shouhui/?
    }手繪圖片
    res.setHeader("Cache-Control","max-age=" + PeriodContants.YEAR_IN_SECONDS * 10);
    res.setHeader("Etag", shaService.encrypt(finalqs));
}
Writer out = res.getWriter();
 
 
File wr = new File(getServletContext().getRealPath("/"));
boolean separatorReached = false;
for(String fn : fns){
     if(fn.startsWith("type=") || fn.startsWith("prefix=") || fn.startsWith("postfix=") || fn.startsWith("separator=") || fn.startsWith("version=")){
         continue;
     }
    if(fn.equals(separator)){
        separatorReached = true;
        continue;
    }
    File f = null;
    if(!separatorReached){
        if(prefix != null && !prefix.isEmpty()){
            fn = prefix + fn;
        }
        if(postfix != null && !postfix.isEmpty() && !fn.endsWith(postfix)){
            fn = fn + postfix;
        }
    }
    f = new File(wr,fn);
    if(f != null && f.isFile() && f.exists()){
        try {
            FileInputStream is = new FileInputStream(f);
            out.write(autils.read2String(is, "utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
out.flush();
out.close();
?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.