<script type="text/blade_config"> { url: 'restful/index', template: 'indexTmpt' } </script>
<script id="indexTmpt" type="text/blade_template"> <ul> <% for(var i = 0, len = data.length; i < len; i++) { %> <li><%=data[i].name %></li> <% } %> </ul> </script>
//模擬數據返回 data = [ {id: 0, name: 'item_0'}, {id: 1, name: 'item_1'}, {id: 2, name: 'item_2'} ]
① 解析頁面的config信息,取出url以及templatejavascript
② 根據url發出請求返回數據,這裏因爲是局域網應該很快php
③ 解析template,根據data生成靜態htmlhtml
④ 其它處理,返回客戶端前端
咱們這裏略去url請求一步,假設數據已經返回,不然這裏又要寫.net程序html5
var data = [ {id: 0, name: 'item_0'}, {id: 1, name: 'item_1'}, {id: 2, name: 'item_2'} ];
這裏的模板字符串爲:java
var template = [ '<ul>', '<% for(var i = 0, len = data.length; i < len; i++) { %>', '<li><%=data[i].name %></li>', '<% } %>', '</ul>' ].join('');
而後咱們要作的就是解析這個模板,生成對應的模板解析函數,這裏是調試代碼:node
var data = [ { id: 0, name: 'item_0' }, { id: 1, name: 'item_1' }, { id: 2, name: 'item_2' } ]; var template = [ '<ul>', '<% for(var i = 0, len = data.length; i < len; i++) { %>', '<li><%=data[i].name %></li>', '<% } %>', '</ul>' ].join(''); var templateHandler = function (text, data) { var noMatch = /(.)^/; var escapes = { "'": "'", '\\': '\\', '\r': 'r', '\n': 'n', '\t': 't', '\u2028': 'u2028', '\u2029': 'u2029' }; var escaper = /\\|'|\r|\n|\t|\u2028|\u2029/g; var templateSettings = { evaluate: /<%([\s\S]+?)%>/g, interpolate: /<%=([\s\S]+?)%>/g, escape: /<%-([\s\S]+?)%>/g }; var render; settings = templateSettings; var matcher = new RegExp([ (settings.escape || noMatch).source, (settings.interpolate || noMatch).source, (settings.evaluate || noMatch).source ].join('|') + '|$', 'g'); var index = 0; var source = "__p+='"; text.replace(matcher, function (match, escape, interpolate, evaluate, offset) { source += text.slice(index, offset) .replace(escaper, function (match) { return '\\' + escapes[match]; }); if (escape) { source += "'+\n((__t=(" + escape + "))==null?'':escape(__t))+\n'"; } if (interpolate) { source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'"; } if (evaluate) { source += "';\n" + evaluate + "\n__p+='"; } index = offset + match.length; return match; }); source += "';\n"; if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n'; source = "var __t,__p='',__j=Array.prototype.join," + "print=function(){__p+=__j.call(arguments,'');};\n" + source + "return __p;\n"; return source; try { render = new Function(settings.variable || 'obj', source); } catch (e) { e.source = source; throw e; } if (data) return render(data); var template = function (data) { return render.call(this, data); }; template.source = 'function(' + (settings.variable || 'obj') + '){\n' + source + '}'; return template; } templateHandler(template, data)
首次調試爆了不少錯誤,並且服務器端的調試比較費力,錯了只能靠經驗去猜想c++
這裏返回的是須要構形成函數的字符串,可是咱們看到咱們的「ul」等標籤被吃掉了!!!git
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');}; with(obj||{}){ __p+=''; for(var i = 0, len = data.length; i < len; i++) { __p+=''+ ((__t=(data[i].name ))==null?'':__t)+ ''; } __p+=''; } return __p;
咱們這裏一旦調用就拋了一個錯誤,這個時候通常是模板或者傳入數據出錯了,惋惜的是他是對其中一段語法不可解析!這裏從側面反映出一個問題:github
該方法如果模板出錯會致使程序沒法運行,若是是node的話極可能就crash了!
PS:這裏因爲CLR4解析javascript的時候字符串的replace遇到正則時有問題,在此逗留3小時,這裏把我搞慘了,定位就好久最後還得重寫模板解析!!!
1 var data = [ 2 { id: 0, name: 'item_0' }, 3 { id: 1, name: 'item_1' }, 4 { id: 2, name: 'item_2' } 5 ]; 6 7 var template = [ 8 '<ul>', 9 '<% for(var i = 0, len = data.length; i < len; i++) { %>', 10 '<li><%=data[i].name %></li>', 11 '<% } %>', 12 '</ul>' 13 ].join(''); 14 15 var templateHandler = function (text, data) { 16 17 var noMatch = /(.)^/; 18 19 var escapes = { 20 "'": "'", 21 '\\': '\\', 22 '\r': 'r', 23 '\n': 'n', 24 '\t': 't', 25 '\u2028': 'u2028', 26 '\u2029': 'u2029' 27 }; 28 29 var escaper = /\\|'|\r|\n|\t|\u2028|\u2029/g; 30 31 var templateSettings = { 32 evaluate: /<%([\s\S]+?)%>/g, 33 interpolate: /<%=([\s\S]+?)%>/g 34 }; 35 36 var render; 37 var settings = templateSettings; 38 39 var matcher = new RegExp([ 40 (settings.interpolate || noMatch).source, 41 (settings.evaluate || noMatch).source 42 ].join('|') + '|$', 'g'); 43 44 var index = 0; 45 var source = "__p+='"; 46 var _text = text; 47 48 var _treg; 49 50 while (1) { 51 52 var matcher = new RegExp([ 53 (settings.interpolate || noMatch).source, 54 (settings.evaluate || noMatch).source 55 ].join('|') + '|$', 'g'); 56 57 (function () { 58 _treg = matcher.exec(_text) 59 })(); 60 61 var t_str = _treg[0]; 62 var t_len = t_str.length; 63 var t_index = _treg.index; 64 65 source += _text.slice(index, t_index).replace(escaper, function (match) { return '\\' + escapes[match]; }); 66 _text = _text.slice(t_index + t_len); 67 68 if (_treg[2]) { 69 source += "';\n" + _treg[2] + "\n__p+='"; 70 } 71 if (_treg[1]) { 72 source += "'+\n" + _treg[1] + "\n'"; 73 } 74 if (_text.length == 0) break; 75 76 } 77 source += "';\n"; 78 79 if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n'; 80 81 source = "var __t,__p='',__j=Array.prototype.join," + 82 "print=function(){__p+=__j.call(arguments,'');};\n" + 83 source + "return __p;\n"; 84 85 try { 86 render = new Function(settings.variable || 'obj', source); 87 } catch (e) { 88 e.source = source; 89 throw e; 90 } 91 92 //return source; 93 94 if (data) return render(data); 95 var template = function (data) { 96 return render.call(this, data); 97 }; 98 99 template.source = 'function(' + (settings.variable || 'obj') + '){\n' + source + '}'; 100 101 return template; 102 } 103 104 templateHandler(template, { data: data })
我這裏使用生命在調試啊!!!由於服務器解析javascript時候,不少東西都不支持,感受有點回到了c++!!!
這裏字符串解析成功,咱們這部分也就告一段落了,自己.net方案也不是此次的重點,這裏提供基本思路各位本身去看看吧,總之調試很坑
對javascript來講,nodeJS天然是親爹,咱們此次的主要方案實際上是基於nodeJS的,這裏的指望:
① 用戶請求過來時候首先判斷是否爲網絡爬蟲
② 網絡爬蟲訪問seo/index.html,用戶訪問webapp/index.html
固然,咱們作demo時候不會這麼麻煩,咱們直接爲其添加一個seo=true的標誌位在url便可
nodeJS實現SEO的方案重點依舊在首屏渲染,咱們這裏首先基於blade作兩個頁面,而後以此擴展seo的方案
固然此塊內容有點小複雜,加之,小釵對nodeJS停留在學習階段,這塊須要學習,並且最近有些其它事情擾心,暫時便擱置了
這塊的內容可能與RapidJS(clouda前身)有關,有興趣的同窗能夠先去看看
文中有誤請您指出,若您對webapp的seo有什麼好的想法請留言