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技术的实现将会是前端可以独立完成的一个优化。提高首屏加载时间。并且提高整个网页的加载时间,对于浏览量的提升会带来一定效果的。