Bigpipe介紹
Facebook首創的一種減少HTTP請求的,首屏快速加載的的異步加載頁面方案。是前端性能優化的一個方向。
BigPipe與AJAX的比較
AJAX主要是XMLHttpRequest,前端異步的向服務器請求,獲取動態數據添加到網頁上。這樣的往返請求需要耗費時間,而BigPipe技術并不需要發送XMLHttpRequest請求,這樣就節省時間損耗。減少請求帶來的另一個好處就是直接減少服務器負載。還有一個不同點就是AJAX請求前服務器在等待。請求后頁面在等待。BIGPIPE可以前后端并行工作也帶來了效率上的提升。
Bigpipe缺點
SEO問題。Facebook的動態展現內容主要是面向客戶的個性頁面。對于SEO的要求并不高。而如果把BIGPIPE技術用到淘寶上的話SEO的問題就會明顯了,現在不確定百度對于這種動態頁面的搜索支持度如何,其實在使用ANGULARJS動態綁定數據的時候也會有這方面的問題所以對于SEO有需求的頁面需要慎重考慮是否使用BIGPIPE技術。(已知GOOGLE搜索對于ANGULAR的SEO有優化。)至于百度么-。-看下圖就知道了
NODEJS實現
bigpipe.js頁面引入的js
var Bigpipe=function(){  this.callbacks={};}Bigpipe.prototype.ready=function(key,callback){  if(!this.callbacks[key]){    this.callbacks[key]=[];  }  this.callbacks[key].push(callback);}Bigpipe.prototype.set=function(key,data){  var callbacks=this.callbacks[key]||[];  for(var i=0;i<callbacks.length;i++){    callbacks[i].call(this,data);  }}app.js服務器代碼
var express = require('express');var path = require('path');var http = require('http');var ejs = require('ejs');var app = express();app.set('port', process.env.PORT || 3000);app.use(express.static(path.join(__dirname, 'public')));app.engine('.html', ejs.__express);app.set('view engine', 'html');app.get('/index.html', function (req, res) {  res.render('index', { title: "測試" }, function (err, str) {    res.write(str)  })  var Pagelets_list ={    pagelet1:false,    pagelet2:false  }  var data = {is: "true"};  function is_end(Pagelets) {    Pagelets_list[Pagelets]=true;    for (x in Pagelets_list) {      if(!Pagelets_list[x]){        return;      }    }    res.end();    return;  }  function Pagelets(Pagelets) {    res.write('<script>bigpipe.set("' + Pagelets + '",' + JSON.stringify(data) + ');</script>');    is_end(Pagelets)  }  setTimeout(function(){Pagelets("pagelet1");},1000);  setTimeout(function(){Pagelets("pagelet2");},3000);});http.createServer(app).listen(3000);index.html前端代碼
<!doctype html><html class="no-js"><head>  <meta charset="utf-8">  <meta http-equiv="X-UA-Compatible" content="IE=edge">  <meta name="description" content="">  <meta name="keywords" content="">  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">  <title>zchq88-bigpipe</title>  <!-- Set render engine for 360 browser -->  <meta name="renderer" content="webkit">  <!-- No Baidu Siteapp-->  <meta http-equiv="Cache-Control" content="no-siteapp"/>  <link  rel="stylesheet"></head><body><div id="test1">loading......</div><div id="test2">loading......</div><script src="http://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script><script src="http://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script><script src="http://cdn.bootcss.com/angular.js/1.5.0-rc.0/angular.min.js"></script><script src="/js/bigpipe.js"></script><script>  var bigpipe=new Bigpipe();  bigpipe.ready('pagelet1',function(data){    $("#test1").html("test1 ready");  })  bigpipe.ready('pagelet2',function(data){    $("#test2").html("test2 ready");  })</script></body></html>總結
Bigpipe技術其實具體實現需要服務器的代碼配合,在開發中我感覺功能占20%,優化占80%的工作量,優化的難度很多時候比開發還高。還需可能對全棧的了解。所以現在nodejs作為前后端分離的中間層是一個我個人認為比較合理的一個解決方案。如果前后端完成nodejs的中間層分離,Bigpipe技術的實現將會是前端可以獨立完成的一個優化。提高首屏加載時間。并且提高整個網頁的加載時間,對于瀏覽量的提升會帶來一定效果的。
新聞熱點
疑難解答