所以我正在开发一个AngularJS网站,我一直负责让它对Facebook分享和SEO友好。我选择PhantomJS作为一种可能的解决方案,来评估Javascript并吐出已执行的html代码,现在只是用facebook信息填充facebook元标记。
然而,在启动phantomJS并评估页面后,我的chrome中出现此错误:
错误:[ngTransclude:orphan]在模板中非法使用ngTransclude指令!没有找到需要转换的父指令。元素:
<ul ng-transclude="">
http://errors.angularjs.org/1.2.22/ngTransclude/orphan?p0=%3Cul%20ng-transclude%3D%22%22%3E
查看代码后,我只在网站中使用了一次转换,这是用于生成我的菜单,其中包含ul和转换的li项目。
app.directive('mvMenu', ['$rootScope', '$location', function ($rootScope, $location) {
return {
restrict: 'E',
transclude: true,
controller: function ($scope, $element, $attrs) {
...
},
template: '<nav id="nav" role="navigation">' +
'<div class="block">' +
'<ul ng-transclude>' +
'</ul>' +
'</div>' +
'</nav>'
}
}]);
我的项目也是一个指令,其代码如下:
app.directive('mvMenuItem', ['$location', function ($location) {
return {
require: '^mvMenu',
restrict: 'E',
scope: {
text: '@text',
navLink: '@link',
icon: '@faicon'
},
link: function (scope, element, attr, menuCtrl) {
...
},
template: '<li ng-class="{\'is-active\': isActive(navLink)}">' +
'<a ng-click="navAndToggleMenu(navLink)"><span class="fa {{icon}}"></span>{{text | translate}}</a>' +
'</li><!--'
}
}]);
在任何设备上的任何浏览器上使用该网站时,我从未见过此错误。只有在使用phantomJS评估页面时才会出现。
这是我用来生成html的幻像代码:
var page = require('webpage').create();
var system = require('system');
var lastReceived = new Date().getTime();
var requestCount = 0;
var responseCount = 0;
var requestIds = [];
var startTime = new Date().getTime();
page.onResourceReceived = function (response) {
if (requestIds.indexOf(response.id) !== -1) {
lastReceived = new Date().getTime();
responseCount++;
requestIds[requestIds.indexOf(response.id)] = null;
}
};
page.onResourceRequested = function (request) {
if (requestIds.indexOf(request.id) === -1) {
requestIds.push(request.id);
requestCount++;
}
};
function checkLoaded() {
return page.evaluate(function () {
return document.all;
}) != null;
}
// Open the page
page.open(system.args[1], function () { });
var checkComplete = function () {
// We don't allow it to take longer than 5 seconds but
// don't return until all requests are finished
if ((new Date().getTime() - lastReceived > 300 && requestCount ===
responseCount) || new Date().getTime() - startTime > 10000 || checkLoaded()) {
clearInterval(checkCompleteInterval);
var result = page.content;
//result = result.substring(0, 10000);
console.log(result);
//console.log(results);
phantom.exit();
}
}
// Let us check to see if the page is finished rendering
var checkCompleteInterval = setInterval(checkComplete, 300);
此代码取自How to make a SPA SEO crawlable?,唯一的区别在于评估方法中的“return document.all”。
提前致谢!
答案 0 :(得分:0)
运行PhantomJS脚本后,您获得的html已经呈现。当你在已经渲染后在普通浏览器中打开它时,让angular尝试重新渲染它是没有意义的。
以下面的标记为例:
<div ng-repeat="stuff in stuffList">{{stuff}}</div>
这例如呈现为
<div class="ng-repeat" ng-repeat="stuff in stuffList">Stuff1</div>
<div class="ng-repeat" ng-repeat="stuff in stuffList">Stuff2</div>
如果在浏览器中打开已渲染的标记会发生什么?将再次执行ng-repeat
,这将导致以下标记:
<div class="ng-repeat" ng-repeat="stuff in stuffList">Stuff1</div>
<div class="ng-repeat" ng-repeat="stuff in stuffList">Stuff1</div>
<div class="ng-repeat" ng-repeat="stuff in stuffList">Stuff2</div>
<div class="ng-repeat" ng-repeat="stuff in stuffList">Stuff2</div>
那不是你想要的。
您应该在保存标记之前删除所有页面脚本。不再需要JavaScript了。
page.evaluate(function(){
[].forEach.call(document.querySelectorAll("script"), function(s){
s.parentNode.removeChild(s)
});
});
fs.write("content.html", page.content);
phantom.exit();