by Ryan Walker

Implementing sitemap_generator

We needed to add a sitemap.xml in a rails 2.3 app. Googling turned up this blog post, this one, this one, and even this old one.

Finally I googled “Rails 3 Sitemap” and turned up this link which lead me to the sitemap_generator gem.

Was super easy to setup. We use bundler in all our Rails projects, so was as simple as adding the gem dependency, implementing a sitemap.rb config file:

SitemapGenerator::Sitemap.default_host = "https://recruitmilitary.com"
SitemapGenerator::Sitemap.yahoo_app_id = "XXX"

SitemapGenerator::Sitemap.add_links do |sitemap|

  sitemap.add sign_up_path
  sitemap.add sign_in_path
  sitemap.add help_path
  sitemap.add terms_path
  sitemap.add privacy_path
  sitemap.add disclaimer_path
  sitemap.add api_v1_docs_path

  Job.featured.find_each do |job|
    sitemap.add job_path(job),
      :lastmod => job.updated_at
  end

  Organization.featured.find_each do |organization|
    sitemap.add organization_path(job),
      :lastmod => organization.updated_at
  end

end

We use resque-scheduler for our scheduled tasks, so I implemented a simple class:

module Board

  class Job::RefreshSitemap < Job

    @queue = :low

    def self.perform
      SitemapGenerator::Sitemap.create
      SitemapGenerator::Sitemap.ping_search_engines
    end

  end

end

And added it to the resque_schedule.yml config:

update_sitemap:
  cron: "0 0 * * *"
  class: Board::Job::RefreshSitemap
  args:
  description: "Regenerate sitemap and ping search engines"

And voilà we have a sitemap with exactly the content we want generated nightly. Thanks Kyle for your stewardship on the project. It made for a very enjoyable tomato this afternoon (yes, I got it implemented start to finish in about 28 minutes!)