Prerender.io 未投入生产

Prerender.io Not Working in Production

我正在尝试使用 prerender.io 中间件为正确的 SEO 设置我的 MEAN 堆栈应用程序。在本地,一切都很好。在生产环境中,nada.The 应用托管在 OpenShift 上。我正在为 preRenderToken 和 preRenderServiceUrl 使用环境变量(服务 URL 仅用于开发,并指向另一个本地节点服务器)。

/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}}">

这些绑定表达式在 Angular 的 $rootScope 上设置如下:

(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=,但页面未预呈现(如果您想验证,那是实际站点 URL)。

我试过将 app.use(prerender . . .) 语句放在不同的位置,每次我最终得到的东西在本地工作,部分或根本不在生产中。

所以,看来你需要在片段后加下划线 ?_escaped_fragment_=