【问题标题】:PhantomJS not serving JS and CSS files correctlyPhantomJS 无法正确提供 JS 和 CSS 文件
【发布时间】:2014-11-28 11:47:50
【问题描述】:

我有一个节点服务器正在运行并监听端口 8080。有一个活跃的 mod_rewrite 规则将机器人重定向到这个端口。

RewriteCond %{HTTP_USER_AGENT} (googlebot|adsbot-google|bingbot|msnbot|psbot|gigabot|twitterbot|linkedinbot|yahoo-mmcrawler|pingdom\.com_bot) [NC]
RewriteRule ^ http://127.0.0.1:8080%{REQUEST_URI} [P]

节点脚本依赖 phantomjs 脚本打开 Bot 请求的任何 URL 并返回内容。

代码取自这里http://backbonetutorials.com/seo-for-single-page-apps/

我通过在本地机器上运行 phantomjs 并使用相同的 phantomjs 代码直接请求我的网页来测试这一点。 结果和“crawl as google”表示的一样(google站长工具),就是phantomjs没有正确服务CSS和JS文件。

CSS 文件只包含

<html><head></head><body></body></html>

并且没有实际的 CSS 内容。 JS文件有

<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">

在任何 JS 代码之前插入。

因此,当 google 尝试抓取和呈现页面时,布局被破坏,并引发 JS 错误。

有什么提示吗?谢谢。

【问题讨论】:

  • 嘿,你解决了吗?我有同样的问题。谢谢。

标签: node.js web-crawler phantomjs single-page-application


【解决方案1】:

好的。我找到了解决办法。

您可能对每个请求使用page.content。您只需要将它用于 html 文件。这是我的代码:

    if (url.indexOf('.html') > -1) cb(page.content);
    else cb(page.plainText);

第二种解决方案需要更改您的 htaccess。

RewriteCond %{HTTP_USER_AGENT} (googlebot|adsbot-google|bingbot|msnbot|psbot|gigabot|twitterbot|linkedinbot|yahoo-mmcrawler|pingdom\.com_bot) [NC]
RewriteCond %{REQUEST_URI} (.*).html(.*) <---- ADDED THIS ONE
RewriteRule ^ http://%{HTTP_HOST}:3004%{REQUEST_URI} [P]

【讨论】:

    猜你喜欢
    • 2018-02-07
    • 2017-02-04
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    相关资源
    最近更新 更多