手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >nodejs实现bigpipe异步加载页面方案
nodejs实现bigpipe异步加载页面方案
摘要:Bigpipe介绍Facebook首创的一种减少HTTP请求的,首屏快速加载的的异步加载页面方案。是前端性能优化的一个方向。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

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> <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> <> <meta name="renderer" content="webkit"> <> <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="http://www.jb51.net/cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script> <script src="http://www.jb51.net/cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <script src="http://www.jb51.net/cdn.bootcss.com/angular.js/1.5.0-rc.0/angular.min.js"></script> <script src="http://www.jb51.netjs/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技术的实现将会是前端可以独立完成的一个优化。提高首屏加载时间。并且提高整个网页的加载时间,对于浏览量的提升会带来一定效果的。

【nodejs实现bigpipe异步加载页面方案】相关文章:

记录几个node.js错误及解决方案

javascript实现动态改变层大小的方法

剖析Node.js异步编程中的回调与代码设计模式

实现无刷新联动例子汇总

javascript实现淡蓝色的鼠标拖动选择框实例

JS实现简单路由器功能的方法

jQuery实现给页面换肤的方法

基于jQuery插件实现环形图标菜单旋转切换特效

js实现异步循环实现代码

jQuery插件jRumble实现网页元素抖动

精品推荐
分类导航