{"id":13517424,"url":"https://github.com/prerender/prerender-node","last_synced_at":"2025-04-11T11:37:34.142Z","repository":{"id":10858387,"uuid":"13142168","full_name":"prerender/prerender-node","owner":"prerender","description":"Express middleware for prerendering javascript-rendered pages on the fly for SEO","archived":false,"fork":false,"pushed_at":"2024-02-20T12:42:54.000Z","size":362,"stargazers_count":914,"open_issues_count":39,"forks_count":181,"subscribers_count":28,"default_branch":"master","last_synced_at":"2024-10-29T15:24:14.739Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prerender.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-09-27T05:24:09.000Z","updated_at":"2024-08-12T19:22:58.000Z","dependencies_parsed_at":"2024-02-20T12:47:14.366Z","dependency_job_id":null,"html_url":"https://github.com/prerender/prerender-node","commit_stats":{"total_commits":226,"total_committers":53,"mean_commits":4.264150943396227,"dds":0.7389380530973451,"last_synced_commit":"faccb9dfd9b6a2896e60d7199e7633236fe32d16"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prerender%2Fprerender-node","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prerender%2Fprerender-node/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prerender%2Fprerender-node/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prerender%2Fprerender-node/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prerender","download_url":"https://codeload.github.com/prerender/prerender-node/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248384825,"owners_count":21094776,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T05:01:33.572Z","updated_at":"2025-04-11T11:37:34.113Z","avatar_url":"https://github.com/prerender.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","⚙️ Backend \u0026 APIs"],"sub_categories":[],"readme":"Prerender Node [![Build Status](https://travis-ci.org/prerender/prerender-node.png)](https://travis-ci.org/prerender/prerender-node) [![NPM version](https://badge.fury.io/js/prerender-node.png)](http://badge.fury.io/js/prerender-node)\n===========================\n\nGoogle, Facebook, Twitter, and Bing are constantly trying to view your website... but Google is the only crawler that executes a meaningful amount of JavaScript and Google even admits that they can execute JavaScript weeks after actually crawling. Prerender allows you to serve the full HTML of your website back to Google and other crawlers so that they don't have to execute any JavaScript. [Google recommends using Prerender.io](https://developers.google.com/search/docs/guides/dynamic-rendering) to prevent indexation issues on sites with large amounts of JavaScript.\n\nPrerender is perfect for Angular SEO, React SEO, Vue SEO, and any other JavaScript framework.\n\nThis 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. That HTML is then returned to the crawler.\n\nvia npm:\n\n    $ npm install prerender-node --save\n\nAnd when you set up your express app, add:\n\n```js\napp.use(require('prerender-node'));\n```\n\nor if you have an account on [prerender.io](https://prerender.io/) and want to use your token:\n\n```js\napp.use(require('prerender-node').set('prerenderToken', 'YOUR_TOKEN'));\n```\n\n`Note` If you're testing locally, you'll need to run the [prerender server](https://github.com/prerender/prerender) locally so that it has access to your server.\n\nThis middleware is tested with Express3 and Express4, but has no explicit dependency on either.\n\n## Testing\n\nThe best way to test the prerendered page is to [set the User Agent of your browser to Googlebot's user agent](https://developers.google.com/web/tools/chrome-devtools/device-mode/override-user-agent) and visit your URL directly. If you View Source on that URL, you should see the static HTML version of the page with the `\u003cscript\u003e` tags removed from the page. If you still see `\u003cscript\u003e` tags then that means the middleware isn't set up properly yet.\n\n`Note` If you're testing locally, you'll need to run the [prerender server](https://github.com/prerender/prerender) locally so that it has access to your server.\n\n## How it works\n1. The middleware checks to make sure we should show a prerendered page\n\t1. The middleware checks if the request is from a crawler by checking the user agent string against a default list of crawler user agents\n\t2. The middleware checks to make sure we aren't requesting a resource (js, css, etc...)\n\t3. (optional) The middleware checks to make sure the url is in the whitelist\n\t4. (optional) The middleware checks to make sure the url isn't in the blacklist\n2. The middleware makes a `GET` request to the [prerender service](https://github.com/prerender/prerender) for the page's prerendered HTML\n3. Return that HTML to the crawler from your server\n\n# Customization\n\n### Whitelist\n\nWhitelist 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.\n```js\napp.use(require('prerender-node').whitelisted('^/search'));\n```\n```js\napp.use(require('prerender-node').whitelisted(['/search', '/users/.*/profile']));\n```\n\n### Blacklist\n\nBlacklist 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.\n```js\napp.use(require('prerender-node').blacklisted('^/search'));\n```\n```js\napp.use(require('prerender-node').blacklisted(['/search', '/users/.*/profile']));\n```\n\n### beforeRender\n\nThis 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 `status` and `body` property (defaulting to `200` and `\"\"` respectively) and serve those instead.\n```js\napp.use(require('prerender-node').set('beforeRender', function(req, done) {\n\t// do whatever you need to do\n\tdone();\n}));\n```\n\n### afterRender\n\nThis 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 called after the prerender service returns HTML.\n```js\napp.use(require('prerender-node').set('afterRender', function(err, req, prerender_res) {\n\t// do whatever you need to do\n}));\n```\n\nYou can also use `afterRender` to cancel the prerendered response and skip to the next middleware. For example, you may want to implement your own fallback behaviour for when Prerender returns errors or if the HTML is missing expected content. To cancel the render, return an object containing `cancelRender: true` from `afterRender`:\n\n```js\napp.use(require('prerender-node').set('afterRender', function(err, req, prerender_res) {\n\tif (err) {\n\t\treturn { cancelRender: true };\n\t}\n}));\n```\n\n### protocol\n\nOption to hard-set the protocol. Useful for sites that are available on both http and https.\n```js\napp.use(require('prerender-node').set('protocol', 'https'));\n```\n\n### host\n\nOption to hard-set the host. Useful for sites that are behind a load balancer or internal reverse proxy.\nFor example, your internal URL looks like `http://internal-host.com/` and you might want it to instead send\na request to Prerender.io with your real domain in place of `internal-host.com`.\n```js\napp.use(require('prerender-node').set('host', 'example.com'));\n```\n\n### forwardHeaders\n\nOption to forward headers from request to prerender.\n```js\napp.use(require('prerender-node').set('forwardHeaders', true));\n```\n\n### prerenderServerRequestOptions\n\nOption to add options to the request sent to the prerender server.\n```js\napp.use(require('prerender-node').set('prerenderServerRequestOptions', {}));\n```\n\n\n\n## Caching\n\nThis express middleware is ready to be used with [redis](http://redis.io/) or [memcached](http://memcached.org/) to return prerendered pages in milliseconds.\n\nWhen setting up the middleware, you can add a `beforeRender` function and `afterRender` function for caching.\n\nHere's an example testing a local redis cache:\n\n\t$ npm install redis\n\n```js\nvar redis = require(\"redis\"),\n\tclient = redis.createClient();\n\nprerender.set('beforeRender', function(req, done) {\n\tclient.get(req.url, done);\n}).set('afterRender', function(err, req, prerender_res) {\n\tclient.set(req.url, prerender_res.body)\n});\n```\n\nor\n\n```js\nvar redis = require(\"redis\"),\nclient = redis.createClient(),\ncacheableStatusCodes = {200: true, 302: true, 404: true};\n\nprerender.set('beforeRender', function(req, done) {\n  client.hmget(req.url, 'body', 'status', function (err, fields) {\n    if (err) return done(err);\n    done(err, {body: fields[0], status: fields[1]});\n  });\n}).set('afterRender', function(err, req, prerender_res) {\n  // Don't cache responses that might be temporary like 500 or 504.\n  if (cacheableStatusCodes[prerender_res.statusCode]) {\n    client.hmset(req.url, 'body', prerender_res.body, 'status', prerender_res.statusCode);\n  }\n});\n```\n\n\n## Using your own prerender service\n\nWe host a Prerender server at [prerender.io](https://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](https://prerender.io/).\n\n\t$ export PRERENDER_SERVICE_URL=\u003cnew url\u003e\n\nOr on heroku:\n\n\t$ heroku config:set PRERENDER_SERVICE_URL=\u003cnew url\u003e\n\nAs an alternative, you can pass `prerenderServiceUrl` in the options object during initialization of the middleware\n\n```js\napp.use(require('prerender-node').set('prerenderServiceUrl', '\u003cnew url\u003e'));\n```\n\n## Contributing\n\nWe love any contributions! Feel free to create issues, pull requests, or middleware for other languages/frameworks!\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2013 Todd Hooper \u0026lt;todd@prerender.io\u0026gt;\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprerender%2Fprerender-node","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprerender%2Fprerender-node","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprerender%2Fprerender-node/lists"}