我試圖讓我的MEAN堆棧應用程序設置爲正確的SEO,使用prerender.io中間件。在本地,一切都很好。在製作中,nada.The應用程序託管在OpenShift上。我爲preRenderToken和preRenderServiceUrl使用環境變量(服務URL僅用於dev,並指向另一個本地節點服務器)。Prerender.io不能在生產中工作
在/server/config/local.env.js
:
(function()
{
'use strict';
module.exports = Env();
function Env()
{
var IEnvironmentVariables = {
// Prerender.io
PRERENDER_SERVICE_URL: 'http://localhost:3000/',
PRERENDER_TOKEN: 'my prerender.io token',
};
return IEnvironmentVariables;
}
})();
在/server/config/environment/index.js
:
// Prerender.io
// I've definitely got both of these set locally and the token set on OpenShift
prerenderServiceUrl: process.env.PRERENDER_SERVICE_URL || process.env.OPENSHIFT_PRERENDER_SERVICE_URL || 'http://localhost:3000/',
prerenderToken: process.env.PRERENDER_TOKEN || process.env.OPENSHIFT_PRERENDER_TOKEN || 'prerender-token'
該文件頭包含以下meta
標籤:
<meta name="fragment" content="!">
<meta name="description" content="{{description}}">
<meta name="keywords" content="{{keywords}}">
這些約束表達式對角的$rootScope
像THI設置S:
(function()
{
'use strict';
angular.module('myApp')
.run(['$rootScope', '$state', Run]);
function Run($rootScope, $state)
{
$rootScope.$on('$stateChangeSuccess', function (event, current, previous)
{
// Meta tags
$rootScope.description = $state.current.description || 'default-description';
$rootScope.keywords = $state.current.keywords ?
$state.current.keywords
.toString()
.split(',')
.join(' ') : 'default-keywords
});
}
})();
最後,在每個$state
配置,這樣的事情:
(function()
{
'use strict';
angular.module('myApp')
.config(['$stateProvider', Config]);
function Config($stateProvider)
{
$stateProvider
.state('main', {
url: '/',
templateUrl: 'app/main/main.html',
controller: 'MainController',
controllerAs: 'vm',
description: 'my site description',
keywords: ['array', 'of', 'keywords']
});
}
})();
參觀http://localhost:9000/?_escaped_fragment=
(或任何其他網站的頁面與查詢串)產生的預渲染頁面,用適當的元標記中的值。在製作過程中,我可以訪問http://www.dancakes.com/?_escaped_fragment=
,但該頁面未預先呈現(如果要驗證,則爲實際網站網址)。
我已經玩弄了將app.use(prerender . . .)
聲明放在不同的位置,每次我最終得到的東西在本地工作,部分或根本不在生產中。
你需要在結束後強調強調 - http://www.dancakes.com/?_escaped_fragment_= – YOU
哇。好眼睛。這是答案,謝謝! – MyCompassSpins