0

我已经在 Xampp 上安装了 prerender.io,我没有做任何花哨的事情。只需安装 prerender 所需的一切并遵循prerender.iohttps://github.com/zf-fr/zfr-prerender上的文档。所以可以说我在这里放了预渲染:

/public/assets/services/prerender

在控制台中我跑了:

npm install
node server.js

Zfr-prerender 是中间件,我只更改了一件事,即引用 prerender 的 url:

'prerender_url' => 'http://localhost:3000/public/assets/services/prerender',

在我的浏览器中,我转到:

本地主机/商店/api/list?_escaped_fragment_=

当找不到路由时,来自 angularJS 的路由器重定向到 /list/1:

ListerApp.config(['$routeProvider', '$httpProvider', '$locationProvider', function($routeProvider, $httpProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('!');
$routeProvider
    .when('/list/:page?',
        {
            templateUrl : '/assets/services/partials/list.html',
            controller : 'ListerCtrl',
            reloadOnSearch : false,
            js : ['/assets/min/shoplist_js.min.js', '/assets/min/bootstrap-typeahead.min.js'],
            //css : [],
            resolve : {
                sharedServiceAppData: function($sharedData){
                    // sharedFactoryAppData will also be injectable in your controller, if you don't want this you could create a new promise with the $q service
                    return $sharedData.promise();
                    //return;
                }
            }
        }
    )
    .otherwise({ redirectTo: '/list/1' });

}]);

控制台告诉我这个:

getting public/assets/services/prerender/http://localhost/shop/api/list
got 200 in 419ms for public/assets/services/prerender/http://localhost/shop/api/list

但是所有 phantomJS 返回的都是奇怪的字符。

public function prerenderPost(PrerenderEvent $event)
{
    // This is the response we get from the Prerender service
    $response = $event->getResponse();
    var_dump($response->getHeaders());
    var_dump($response->getContent());
    // You could get the body and put it in cache
    // ...

}

这些是标题,var_dump($response->getHeaders()):

 array (size=5)
  0 => 
    array (size=2)
      'name' => string 'Content-Type' (length=12)
      'line' => string 'Content-Type: text/html;charset=UTF-8' (length=37)
  1 => 
    array (size=2)
      'name' => string 'Content-Encoding' (length=16)
      'line' => string 'Content-Encoding: gzip' (length=22)
  2 => 
    array (size=2)
      'name' => string 'Content-Length' (length=14)
      'line' => string 'Content-Length: 45' (length=18)
  3 => 
    array (size=2)
      'name' => string 'Date' (length=4)
      'line' => string 'Date: Thu, 30 Oct 2014 18:23:39 GMT' (length=35)
  4 => 
    array (size=2)
      'name' => string 'Connection' (length=10)
      'line' => string 'Connection: close'

这是一个 PHP var_dump($response->getContent()):

字符串 '‹������³É(Éͱ³ÉHML±³Ñ‡PIù)•@”+�� 'H'����' (长度=45)

‹³É(Éͱ³ÉHML±³Ñ‡PIù)•@”+ 'H'

4

1 回答 1

0

首先,解压缩并获取您需要的内容

$response->getBody()

其次,如果您的计算机非常旧且速度慢,增加 de Zfr-prenderer 模块中的套接字超时可能会很有用。

于 2014-11-01T19:24:07.267 回答