Sitemap builder for Rails app
As you know sitemap is one of important elements of the website. Here's the easy way to generate sitemap.xml using Rails based app/website:
# Gemfile
gem 'big_sitemap'
# lib/tasks/sitemap.rake
require 'big_sitemap'
namespace :custom do
desc "Generate sitemap"
task :sitemap => :environment do
include Rails.application.routes.url_helpers
sitemap_options = {
document_root: Rails.root.join('public'),
url_options: { host: 'example.com' },
ping_google: true,
ping_bing: true,
gzip: true
}
if Rails.env.development?
sitemap_options = {
document_root: Rails.root.join('public'),
url_options: { host: 'localhost', port: 3000 },
ping_google: false,
ping_bing: false,
gzip: false
}
end
BigSitemap.generate(sitemap_options) do
add root_path, change_frequency: 'daily', priority: 1.0
Stuff.all.each do |stuff|
add stuff_path(stuff), change_frequency: 'daily', priority: 0.5
end
end
end
end
Then we can generate using command:
rake custom:sitemap
Capistrano integration is quite useful to generate sitemap and ping search engines automatically during deployment.
# config/deploy.rb
namespace :deploy do
desc "Generate sitemap"
task :sitemap do
run "cd '#{current_path}' && #{rake} custom:sitemap RAILS_ENV=#{rails_env}"
end
end
after 'deploy:update', 'deploy:sitemap'
Written by Alif Rachmawadi
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#