Skip to content

KOA middleware for prerendering javascript-rendered pages on the fly for SEO

Notifications You must be signed in to change notification settings

mitom/koa-prerender

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa-prerender Build Status

NPM

KOA middleware for prerendering javascript-rendered pages on the fly for SEO

This koa middleware intercepts requests to your Node.js website from crawlers, and then makes a call to the (external) Prerender service to get the static HTML instead of the javascript for that page.

Setup

Prerequisites

Install Prerender on a server of your choice.

Install

Install the package with npm:

$ npm install koa-prerender`

Usage

var prerender = require('koa-prerender');

// Options
var options = {
  prerender: PRERENDER_SERVER_URL   // optional, default:'http://service.prerender.io/'
  protocol: 'http',                 // optional, default: this.protocol
  host: 'www.risingstack.com'       // optional, default: this.host,
  prerenderToken: ''                // optional or process.env.PRERENDER_TOKEN
  beforePrerender: function *() {   // optional before hook
    this.body = 'foo';
  },
  afterPrerender: function *() {    // optional after hook
    this.body = 'bar';
  }
};

// Use as middleware
app.use(prerender(options));

When prerendering happens the request will not yield further down the stack but return the rendered page instead.

Hooks

If beforePrerender or afterPrerender is set in the options, they will be called before and after the prerendering is supposed to happen. If beforePrerender sets a body, it will be used and the actual prerender server will not be called for that request.

This makes it possible to reliably tell when a request was or is going to be prerendered and therefore the perfect place to handle caching if needed.

License

ISC

About

KOA middleware for prerendering javascript-rendered pages on the fly for SEO

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%