所以我试图在prerender.io之后使用ExpressJS后端为我的AngularJS应用程序设置tutorial。我完全按照指示完成,唯一的区别是我启用了HTML5mode
。我已将meta(name="fragment" content="!")
添加到我的index.jade
中,并将prerender令牌添加到我的server.js
文件中(使用prerender-node包)但不知何故我的网页似乎没有被缓存或生成任何抓取统计数据。
config.coffee
angular.config ['$stateProvider','$urlRouterProvider','$locationProvider','toastrConfig',($stateProvider, $urlRouterProvider,$locationProvider,toastrConfig)->
$stateProvider
.state 'home',
url:'/'
templateUrl: 'html/main.html'
controller:'mainController'
$urlRouterProvider.otherwise '/'
$locationProvider.html5Mode
enabled: true
requireBase: false
$locationProvider.hashPrefix '!'
]
Server.JS
// Here we require the prerender middleware that will handle requests from Search Engine crawlers
// We set the token only if we're using the Prerender.io service
app.use(require('prerender-node')
.set('prerenderServiceUrl', 'http://www.mydomain.co.com/')
.set('prerenderToken', 'my-token'));
// HTML5MODE settings
// ------------------------------------------------------
app.use('/js', express.static(__dirname + '/public/js'));
app.use('/css', express.static(__dirname + '/public/css'));
app.use('/html', express.static(__dirname + '/public/html'));
// Routes
// ------------------------------------------------------
require('./app/js/routes/routes.js')(app);
app.all('/*', function(req, res, next) {
// Just send the index.html for other files to support HTML5Mode
res.sendFile('/public/index.html', { root: __dirname });
});
答案 0 :(得分:1)
您应该从配置中删除此行:
.set('prerenderServiceUrl', 'http://www.mydomain.co.com/')
服务网址应指向Prerender服务器,因此您不应将其设置为您的网站网址。
答案 1 :(得分:0)
我认为您应该确保已在Angular App中实现了正确的prerender.io配置。
我测试了以下代码并将其正确缓存在prerender.io仪表板中。
File: ../static/routes.json content
["/","/content/terms-conditions","/content/about-us",..]
File: /config/webpack.production.js
const axios = require('axios');
const routesData = require('../static/routes.json'),
console.log("PRERENDER.io caching start for Crawler Bots to SEO indexing");
const host = 'your domain name with http or https';
const prerenderToken = '****';
const prerenderUrl = 'https://api.prerender.io/recache';
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
console.log("Start date time : "+dateTime);
for (const route of routesData) {
const fullRoute = host + route + '/index.html';
console.log(`Caching URL ${fullRoute}`);
var prerednerData = {
'prerenderToken': prerenderToken,
'url': fullRoute
};
axios.post(prerenderUrl, prerednerData)
.then((response) => {
console.log('Product URL: '+prerednerData['url']);
console.log('Status Code: '+response.status);
})
.catch((error) => {
console.log('Product URL: '+prerednerData['url']);
console.log('Error: '+error);
});
}
File: .htaccess
Options -Indexes
<IfModule mod_headers.c>
RequestHeader set X-Prerender-Token "****"
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
# Pre-render HTML for search crawler bots to indexing
<IfModule mod_proxy_http.c>
RewriteCond %{HTTP_USER_AGENT} googlebot|bingbot|yandex|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
# Only proxy the request to Prerender.io if it's a request for HTML
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff|\.svg))(.*) http://service.prerender.io/https://www.globalshop.com.au/$2 [NE,L,R=301]
</IfModule>
RewriteCond %{HTTP_HOST} www.domainname.com$ [NC]
RewriteRule ^(.*)$ https://www.domainname.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^robots\.txt$ robots-disallow.txt [L]
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.html
</IfModule>
干杯!