LoginSignup
10
10

More than 3 years have passed since last update.

Ajaxページをgoogle botへPhantomjsで静的ページを用意する。

Last updated at Posted at 2014-04-15

Ajaxバリバリのページはクローラから見たらスカスカで価値がないページと判断される恐れがある


<html>
    <head>
      <title>sukasuka</title>
    </head>
  <body>
    <div>{hogehoge}</div>
  </body>
</html>

そこで、phantom jsを使ってページをレンダリングして静的ページに変換する。
phantomjsがなんだか分からないけど、webkitをシミュレーションしてくれる。便利なツール

var page = require('webpage').create();
page.open('http://target-ajax.page.com/index.html', function (status) {
    window.setTimeout(function () {
      if (status === 'success') {
        console.log(status);
        var html = page.evaluate(function () {
          return document.documentElement.outerHTML;
        });
        console.log(html);
      }
      phantom.exit();
    }, 200);
 });

クーロンは適当にこのように。

0 * * * * /usr/bin/phantomjs  /home/hoge/make_static.js > /var/www/hogehoge/production/current/public/static.html

そして、nginxここでハマる。。
if分で、プロキシは設定できないらしい。。

[root@host]# service nginx start
Restarting nginx: nginx: [emerg] "include" directive is not allowed here in /etc/nginx/sites-enabled/bar:21
nginx: configuration file /etc/nginx/nginx.conf test failed
`

nginx.conf
if ($http_user_agent ~* "googlebot|yahoo|bingbotbaiduspider|yandex|yeti|yodaobot|gigabot|ia_archiver|facebookexternalhit|twitterbot|developers\.google\.com") {
  rewrite ^/$ http://target-ajax.page.com/static.html last;
}

Chromeでエージェントをgoogle-botに変えてテストしてみて、うまくstaticが見れればok
urlが変わってしまうので、効果のほどはいま様子を見てます。

参考サイト
http://havelog.ayumusato.com/develop/javascript/e526-make_static_html_using_phantomjs.html

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10