rendertron

Set up Rendertron with express.js

If you use expressjs you can use the [rendertron-middleware] to add Rendertron to your express.js application.

Install rendertron-middleware

Inside the root directory of your web application, run the following command:

npm install --save rendertron-middleware

Setup your express.js application to use the middleware

const express = require('express');
const rendertron = require('rendertron-middleware');

const app = express();

app.use(
  rendertron.makeMiddleware({
    // replace this with the web address of your rendertron instance
    proxyUrl: 'http://PUT-YOUR-RENDERTRON-URL-HERE/render',
  })
);

app.use(express.static('files'));
app.listen(8080);

Configure which user agents are pre-rendered with Rendertron

The middleware comes with a pre-configured bot list.

If you wish to use Rendertron for other bots, you can either replace or extend this list.

To replace the list with your own, configure the middleware like this:

// only use Rendertron for LinkedInBot and Twitterbot
const myBotList = ['linkedinbot', 'twitterbot'];

app.use(
  rendertron.makeMiddleware({
    // replace the default bot list with your own:
    userAgentPattern: new RegExp(myBotList.join('|'), 'i'),
    // replace this with the web address of your rendertron instance
    proxyUrl: 'http://PUT-YOUR-RENDERTRON-URL-HERE/render',
  })
);

You can also extend the bot list to include more bots:

// add googlebot and yolobot to bot list
const myBotList = rendertron.botUserAgents.concat(['googlebot', 'yolobot']);

app.use(
  rendertron.makeMiddleware({
    // use the extended bot list:
    userAgentPattern: new RegExp(myBotList.join('|'), 'i'),
    // replace this with the web address of your rendertron instance
    proxyUrl: 'http://PUT-YOUR-RENDERTRON-URL-HERE/render',
  })
);