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 href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"></head><body><div id="test1">loading......</div><div id="test2">loading......</div><script src="//cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script><script src="//cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script><script src="//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技術的實現將會是前端可以獨立完成的一個最佳化。提高首屏載入時間。並且提高整個網頁的載入時間,對於瀏覽量的提升會帶來一定效果的。