Skip to content

Commit

Permalink
Add and implement sitemap_generator gem (#52)
Browse files Browse the repository at this point in the history
* Implement sitemap generation using the 'sitemap_generator' gem

This commit adds sitemap generation for our Sitepress-enabled app. It iterates through all PageModel instances, adding their paths to the sitemap with appropriate change frequencies and priorities. The sitemap helps improve SEO by providing search engines with structured access to our site's pages.
  • Loading branch information
joancodes authored Oct 24, 2023
1 parent c396566 commit 4a71f10
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ gem "erb_lint", "~> 0.5.0"
# Search Engine Optimization (SEO) for Ruby on Rails applications.
gem "meta-tags"

# A sitemap generator gem for Rails
gem "sitemap_generator", "~> 6.3"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[mri windows]
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
sitemap_generator (6.3.0)
builder (~> 3.0)
sitepress-core (3.2.2)
mime-types (>= 2.99)
sitepress-rails (3.2.2)
Expand Down Expand Up @@ -356,6 +358,7 @@ DEPENDENCIES
redis (>= 4.0.1)
rouge (~> 4.1)
selenium-webdriver
sitemap_generator (~> 6.3)
sitepress-rails
sprockets-rails
standard (~> 1.31)
Expand Down
36 changes: 36 additions & 0 deletions config/sitemap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Set the host name for URL creation
SitemapGenerator::Sitemap.default_host = "https://hotwire.io"

SitemapGenerator::Sitemap.create do
# Put links creation logic here.
#
# The root path '/' and sitemap index file are added automatically for you.
# Links are added to the Sitemap in the order they are specified.
#
# Usage: add(path, options={})
# (default options are used if you don't specify)
#
# Defaults: :priority => 0.5, :changefreq => 'weekly',
# :lastmod => Time.now, :host => default_host
#
# Examples:
#
# Add '/articles'
#
# add articles_path, :priority => 0.7, :changefreq => 'daily'
#
# Add all articles:
#
# Article.find_each do |article|
# add article_path(article), :lastmod => article.updated_at
# end

add "/", changefreq: "weekly"

PageModel.all.each do |page|
nest_level = page.request_path.split("/").count.to_f
priority = nest_level.zero? ? 0.9 : (1 / nest_level)

add page_path(resource_path: page.request_path), changefreq: "weekly", priority: priority
end
end
1 change: 1 addition & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
Sitemap: https://hotwire.io/sitemap.xml.gz
Binary file added public/sitemap.xml.gz
Binary file not shown.

0 comments on commit 4a71f10

Please sign in to comment.