Prerender adheres to google's
_escaped_fragment_ proposal, which we recommend you use. It's easy:
$ npm install prerender-node --save
And when you set up your express app, add:
or if you have an account on prerender.io and want to use your token:
Note If you're testing locally, you'll need to run the prerender server locally so that it has access to your server.
This middleware is tested with Express3 and Express4, but has no explicit dependency on either.
If your URLs use a hash-bang:
If you want to see `http://localhost:3000/#!/profiles/1234` Then go to `http://localhost:3000/?_escaped_fragment_=/profiles/1234`
If your URLs use push-state:
If you want to see `http://localhost:3000/profiles/1234` Then go to `http://localhost:3000/profiles/1234?_escaped_fragment_=`
_escaped_fragment_or agent string)
GETrequest to the prerender service (phantomjs server) for the page's prerendered HTML
Whitelist a single url path or multiple url paths. Compares using regex, so be specific when possible. If a whitelist is supplied, only urls containing a whitelist path will be prerendered.
Blacklist a single url path or multiple url paths. Compares using regex, so be specific when possible. If a blacklist is supplied, all url's will be prerendered except ones containing a blacklist path.
This method is intended to be used for caching, but could be used to save analytics or anything else you need to do for each crawler request. If you return a string from beforeRender, the middleware will serve that to the crawler (with status
200) instead of making a request to the prerender service. If you return an object the middleware will look for a
body property (defaulting to
"" respectively) and serve those instead.
appuserequire'prerender-node'set'beforeRender'// do whatever you need to dodone;;
This method is intended to be used for caching, but could be used to save analytics or anything else you need to do for each crawler request. This method is a noop and is called after the prerender service returns HTML.
appuserequire'prerender-node'set'afterRender'// do whatever you need to do;
Option to hard-set the protocol. Useful for sites that are available on both http and https.
When setting up the middleware, you can add a
beforeRender function and
afterRender function for caching.
Here's an example testing a local redis cache:
$ npm install redis
var redis = require"redis"client = rediscreateClient;prerenderset'beforeRender'clientgetrequrl done;set'afterRender'clientsetrequrl prerender_resbody;
var redis = require"redis"client = rediscreateClientcacheableStatusCodes = 200: true 302: true 404: true;prerenderset'beforeRender'clienthmgetrequrl 'body' 'status'if err return doneerr;doneerr body: fields0 status: fields1;;set'afterRender'// Don't cache responses that might be temporary like 500 or 504.if cacheableStatusCodesprerender_resstatusCodeclienthmsetrequrl 'body' prerender_resbody 'status' prerender_resstatusCode;;
We host a Prerender server at prerender.io so that you can work on more important things, but if you've deployed the prerender service on your own... set the
PRERENDER_SERVICE_URL environment variable so that this middleware points there instead. Otherwise, it will default to the service already deployed by prerender.io.
$ export PRERENDER_SERVICE_URL=<new url>
Or on heroku:
$ heroku config:set PRERENDER_SERVICE_URL=<new url>
As an alternative, you can pass
prerender_service_url in the options object during initialization of the middleware
appuserequire'prerender-node'set'prerenderServiceUrl' '<new url>';
We love any contributions! Feel free to create issues, pull requests, or middleware for other languages/frameworks!
The MIT License (MIT)
Copyright (c) 2013 Todd Hooper <email@example.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.