nodejs实现bigpipe异步加载页面方案
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
varBigpipe=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){
varcallbacks=this.callbacks[key]||[];
for(vari=0;i<callbacks.length;i++){
callbacks[i].call(this,data);
}
}
app.js服务器代码
varexpress=require('express');
varpath=require('path');
varhttp=require('http');
varejs=require('ejs');
varapp=express();
app.set('port',process.env.PORT||3000);
app.use(express.static(path.join(__dirname,'public')));
app.engine('.html',ejs.__express);
app.set('viewengine','html');
app.get('/index.html',function(req,res){
res.render('index',{title:"测试"},function(err,str){
res.write(str)
})
varPagelets_list={
pagelet1:false,
pagelet2:false
}
vardata={is:"true"};
functionis_end(Pagelets){
Pagelets_list[Pagelets]=true;
for(xinPagelets_list){
if(!Pagelets_list[x]){
return;
}
}
res.end();
return;
}
functionPagelets(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前端代码
<!doctypehtml>
<htmlclass="no-js">
<head>
<metacharset="utf-8">
<metahttp-equiv="X-UA-Compatible"content="IE=edge">
<metaname="description"content="">
<metaname="keywords"content="">
<metaname="viewport"content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>zchq88-bigpipe</title>
<!--Setrenderenginefor360browser-->
<metaname="renderer"content="webkit">
<!--NoBaiduSiteapp-->
<metahttp-equiv="Cache-Control"content="no-siteapp"/>
<linkhref="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css"rel="stylesheet">
</head>
<body>
<divid="test1">loading......</div>
<divid="test2">loading......</div>
<scriptsrc="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>
<scriptsrc="https://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<scriptsrc="https://cdn.bootcss.com/angular.js/1.5.0-rc.0/angular.min.js"></script>
<scriptsrc="/js/bigpipe.js"></script>
<script>
varbigpipe=newBigpipe();
bigpipe.ready('pagelet1',function(data){
$("#test1").html("test1ready");
})
bigpipe.ready('pagelet2',function(data){
$("#test2").html("test2ready");
})
</script>
</body>
</html>
总结
Bigpipe技术其实具体实现需要服务器的代码配合,在开发中我感觉功能占20%,优化占80%的工作量,优化的难度很多时候比开发还高。还需可能对全栈的了解。所以现在nodejs作为前后端分离的中间层是一个我个人认为比较合理的一个解决方案。如果前后端完成nodejs的中间层分离,Bigpipe技术的实现将会是前端可以独立完成的一个优化。提高首屏加载时间。并且提高整个网页的加载时间,对于浏览量的提升会带来一定效果的。