DEV Community

loading...

sitemap_generator using lastmod date from git repo

Thomas R. Koll
Ruby on Rails dev since 2007, and a few more years of php before that. Drop me a mail if you want me to work on an interesting project.
・1 min read

For my new project Budget Fox I added sitemap_generator even though it's just a few pages that I want indexed for now.

One thing I noticed when looking at the output was the lastmod date being the same for all entries. Can't be hard to fix that by pulling the date from the git repo I though.
Here's the code for this and as you can see, I did monkey-patch SitemapGenerator::Interpreter.

class SitemapGenerator::Interpreter
  def lastmod(view)
    date = `git log --date iso  -n 1 --format="%ad" app/views/#{view}*`
    if date.blank?
      raise "Missing file #{view}"
    end
    return date
  end
end

SitemapGenerator::Sitemap.create(default_host: 'https://budget-fox.com', compress: false, include_root: false) do
  add root_path, changefreq: 'weekly', lastmod: lastmod('welcome/index')
  add team_path, priority: 0.5, lastmod: lastmod('welcome/team')
  add features_path, lastmod: lastmod('welcome/features.html')
  add pricing_path, lastmod: lastmod('purchase/new.html')
end

Discussion (0)