2015-10-19 98 views
3

我試圖讓我的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 . . .)聲明放在不同的位置,每次我最終得到的東西在本地工作,部分或根本不在生產中。

+1

你需要在結束後強調強調 - http://www.dancakes.com/?_escaped_fragment_= – YOU

+0

哇。好眼睛。這是答案,謝謝! – MyCompassSpins

回答

1

因此,看來你需要片段?_escaped_fragment_=

+0

這是絕對的,只要使用該查詢字符串訪問該頁面時獲取頁面進行預先呈現即可。儘管當我在Google的搜索控制檯中作爲Google獲取時,我仍然只能看到部分內容。我不認爲你知道爲什麼會這樣? – MyCompassSpins

+0

@MyCompassSpins,有window.prerenderReady標誌,你可以在角度加載前設置爲false,並在加載完成後將其設置爲true - https://prerender.io/documentation/best-practices – YOU

+0

Google呈現一個完整的而不是部分的,但是我的Angular表達式在渲染之前沒有被評估。不過,這可能完全是另一個問題。感謝這個幫助:) – MyCompassSpins