Skip to content

Instantly share code, notes, and snippets.

@kitwalker12
Last active December 21, 2015 15:28
Show Gist options
  • Save kitwalker12/6326564 to your computer and use it in GitHub Desktop.
Save kitwalker12/6326564 to your computer and use it in GitHub Desktop.
Sitemap Generation in Rails
#...
match 'sitemap.xml' => 'sitemap#sitemap'
#...
class SitemapController < ApplicationController
def sitemap
full_url = "/public/sitemap.xml"
out = Net::HTTP.get_response("<your_cdn>",full_url)
render xml: out.body
end
end
class SitemapWorker
include Sidekiq::Worker
def perform()
xml = Builder::XmlMarkup.new
xml.instruct!
xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
posts = Page.where(sitemap: true).all
posts.each do |post|
xml.url do
xml.loc ...
xml.lastmod ...
xml.changefreq ...
xml.priority ...
#...
end
end
#...
end
xml_data = xml.target!
sitemap = File.new(Rails.root.join('public/sitemap.xml'), "w")
sitemap.write(xml_data)
sitemap.close
#...
# Upload sitemap to some CDN
#...
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment