Last active
December 21, 2015 15:28
-
-
Save kitwalker12/6326564 to your computer and use it in GitHub Desktop.
Sitemap Generation in Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#... | |
match 'sitemap.xml' => 'sitemap#sitemap' | |
#... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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