{"id":13484577,"url":"https://github.com/prerender/prerender_rails","last_synced_at":"2025-04-15T03:49:23.626Z","repository":{"id":46245466,"uuid":"13127843","full_name":"prerender/prerender_rails","owner":"prerender","description":"Rails middleware gem for prerendering javascript-rendered pages on the fly for SEO","archived":false,"fork":false,"pushed_at":"2024-02-20T12:42:51.000Z","size":107,"stargazers_count":358,"open_issues_count":7,"forks_count":87,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-04-15T03:49:19.988Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prerender.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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-26T16:25:22.000Z","updated_at":"2024-09-20T21:10:43.000Z","dependencies_parsed_at":"2024-05-01T13:20:02.835Z","dependency_job_id":"d2e68fee-7c71-4e37-a2ec-b6710cd1f6fa","html_url":"https://github.com/prerender/prerender_rails","commit_stats":{"total_commits":104,"total_committers":20,"mean_commits":5.2,"dds":0.625,"last_synced_commit":"d66f2872e54fbc292e5f61adc526304f5b9cc8d0"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prerender%2Fprerender_rails","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prerender%2Fprerender_rails/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prerender%2Fprerender_rails/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prerender%2Fprerender_rails/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prerender","download_url":"https://codeload.github.com/prerender/prerender_rails/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249003942,"owners_count":21196794,"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-07-31T17:01:26.436Z","updated_at":"2025-04-15T03:49:23.609Z","avatar_url":"https://github.com/prerender.png","language":"Ruby","readme":"Prerender Rails [![Build Status](https://travis-ci.org/prerender/prerender_rails.png)](https://travis-ci.org/prerender/prerender_rails) [![Gem Version](https://badge.fury.io/rb/prerender_rails.png)](http://badge.fury.io/rb/prerender_rails)\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\n`Note` Make sure you have more than one webserver thread/process running because the prerender service will make a request to your server to render the HTML.\n\nAdd this line to your application's Gemfile:\n\n    gem 'prerender_rails'\n\nAnd in `config/environment/production.rb`, add this line:\n\n```ruby\n\tconfig.middleware.use Rack::Prerender\n```\n\nor if you have an account on [prerender.io](https://prerender.io/) and want to use your token:\n\n```ruby\n\tconfig.middleware.use Rack::Prerender, prerender_token: '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\n## Testing\n\nWhen testing make sure you're not using a single threaded application server like default WEBrick one, use Puma or Unicorn to prevent a deadlock when the Prerender Service needs to render your HTML on the fly.\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)(phantomjs server) for the page's prerendered HTML\n3. Return that HTML to the crawler\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 url's containing a whitelist path will be prerendered.\n```ruby\nconfig.middleware.use Rack::Prerender, whitelist: '^/search'\n```\n```ruby\nconfig.middleware.use Rack::Prerender, whitelist: ['/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```ruby\nconfig.middleware.use Rack::Prerender, blacklist: '^/search'\n```\n```ruby\nconfig.middleware.use Rack::Prerender, blacklist: ['/search', '/users/.*/profile']\n```\n\n### before_render\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 before_render, the middleware will serve that to the crawler instead of making a request to the prerender service.\n```ruby\nconfig.middleware.use Rack::Prerender,\n\tbefore_render: (Proc.new do |env|\n\t\t# do whatever you need to do.\n\tend)\n```\n\n### after_render\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 a noop and is called after the prerender service returns HTML.\n```ruby\nconfig.middleware.use Rack::Prerender,\n\tafter_render: (Proc.new do |env, response|\n\t\t# do whatever you need to do.\n\tend)\n```\n\n### build_rack_response_from_prerender\n\nThis method is intended to be used to modify the response before it is sent to the crawler. Use this method to add/remove response headers, or do anything else before the request is sent.\n```ruby\nconfig.middleware.use Rack::Prerender,\n\tbuild_rack_response_from_prerender: (Proc.new do |response, prerender_response|\n\t\t# response is already populated with the prerender status code, html, and headers\n\t\t# prerender_response is the response that came back from the prerender service\n\tend)\n```\n\n### protocol\n\nOption to hard-set the protocol for Prerender accessing your site instead of the middleware figuring out the protocol based on the request.\n```ruby\nconfig.middleware.use Rack::Prerender, protocol: 'https'\n```\n\n## Caching\n\nThis rails 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 in `config/environment/production.rb`, you can add a `before_render` method and `after_render` method for caching.\n\nHere's an example testing a local redis cache:\n\n_Put this in `config/environment/development.rb`, and add `gem 'redis'` to your Gemfile._\n\n```ruby\nrequire 'redis'\n@redis = Redis.new\nconfig.middleware.use Rack::Prerender,\n  before_render: (Proc.new do |env|\n    @redis.get(Rack::Request.new(env).url)\n  end),\n  after_render: (Proc.new do |env, response|\n    @redis.set(Rack::Request.new(env).url, response.body)\n  end)\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:add PRERENDER_SERVICE_URL=\u003cnew url\u003e\n\nAs an alternative, you can pass `prerender_service_url` in the options object during initialization of the middleware\n\n``` ruby\nconfig.middleware.use Rack::Prerender, prerender_service_url: '\u003cnew url\u003e'\n```\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","funding_links":[],"categories":["Gems","Ruby","SEO"],"sub_categories":["Articles"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprerender%2Fprerender_rails","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprerender%2Fprerender_rails","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprerender%2Fprerender_rails/lists"}