skip to Main Content

So I’m trying to set up prerender.io for my AngularJS application with a ExpressJS backend following this tutorial. I have done exactly as instructed, the only difference being I have enabled HTML5mode. I have included the meta(name="fragment" content="!") in my index.jade and the prerender token to my server.js file (using the prerender-node package) but somehow my pages do not seem to be cached or generate any crawl stats.

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 });
});

2

Answers


  1. You should remove this line from your config:

    .set('prerenderServiceUrl', 'http://www.mydomain.co.com/')
    

    The service URL should point to a Prerender server, so you shouldn’t set that to your website URL.

    Login or Signup to reply.
  2. I think you should make sure that you have implemented the correct prerender.io configuration in your Angular App.

    I tested the below code and correctly cached it in the prerender.io dashboard.

    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>
    

    Cheers!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search