I was in charge of creating RSS feeds for SEO measures on my site, so make a note of how to do it.
A sitemap is a "file that you put on your server to make it easier for crawlers to find your page." There are the following types of site maps.
--Describe all the URLs you want to crawl index. ――The size is large accordingly. ――Therefore, it seems that ** crawlers don't come to see us so much **. Pien
--Write only recent changes. --Therefore, the size is small. ――So ** crawlers come to see more often than XML sitemaps **.
The site I'm in charge of developing is a site that posts event information and attracts customers, so ** More new pages per day ** → I want you to crawl the new and added pages ** as soon as possible ** → I decided to set up an RSS / Atom feed as well as a site map.
Rails has a standard library (https://docs.ruby-lang.org/ja/latest/library/rss.html) for handling RSS, and you can use it just by requiring it.
require 'rss'
And where you want to make an RSS feed
##
# [param] event_list :List of entries to put in the feed
#
def create_rss_feed(event_list)
#This time Atom1.Created in 0 format
feed = RSS::Maker.make('atom') do |maker|
maker.channel.about = 'tag:sample.com:feed' #An ID that uniquely identifies the feed. Maybe anything unique.
maker.channel.title = "New event" #Feed title
maker.channel.description = "Introducing the latest information on the event." #Feed description
maker.channel.link = 'https://sample.com/' #Site URL
maker.channel.author = 'Event secretariat' #Feed creator
maker.channel.date = .strftime('%Y-%m-%d %H:%M:%S') #Last updated date and time of the feed
event_list.each_with_index do |event|
maker.items.new_item do |item|
item.link = event.url #URL of detail page
item.title = event.title #Detail page title
item.date = event.start_time.strftime('%Y-%m-%d %H:%M:%S') #Dates for events, publication dates for articles, etc.
end
end
end
render xml: feed.to_xml
end
If you write and execute such a process The RSS feed is displayed on the browser like this! This time I created it in a format called Atom, but when creating it in a different format
RSS::Maker.make('atom')
For example, if RSS 2.0, this "atom" part should be changed to "2.0".
## in conclusion
The standard library was prepared, so it was completed in no time.
I just think Rails is amazing.
Recommended Posts