Skip to content

Commit f48d56e

Browse files
committed
Add basic sitemaps
The following article served as the basis for this first implementation of sitemaps: https://www.johnnunemaker.com/rails-easy-sitemaps/
1 parent 111a0b5 commit f48d56e

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class SitemapsController < ApplicationController
2+
def index
3+
respond_to do |format|
4+
format.xml
5+
end
6+
end
7+
8+
def pages
9+
@last_modified_at = Time.now.middle_of_day.utc
10+
@pages = Page.all.published.order(published_at: :desc)
11+
12+
@slash_pages = %w[
13+
about
14+
articles
15+
contact
16+
meta
17+
settings
18+
]
19+
20+
respond_to do |format|
21+
format.xml
22+
end
23+
end
24+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# index.xml.builder
2+
xml.instruct!
3+
xml.sitemapindex xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9" do
4+
xml.sitemap do
5+
xml.loc sitemap_pages_url(format: :xml)
6+
xml.lastmod Time.utc(2020, 12, 21, 11).strftime("%Y-%m-%dT%H:%M:%S+00:00")
7+
end
8+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
xml.instruct!
2+
xml.urlset xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9" do
3+
xml.url do
4+
xml.loc root_url
5+
xml.lastmod @last_modified_at.strftime("%Y-%m-%dT%H:%M:%S+00:00")
6+
end
7+
8+
@pages.each do |page|
9+
xml.url do
10+
xml.loc Addressable::URI.join(root_url, page.request_path)
11+
xml.lastmod @last_modified_at.strftime("%Y-%m-%dT%H:%M:%S+00:00")
12+
end
13+
end
14+
15+
@slash_pages.each do |path|
16+
xml.url do
17+
xml.loc Addressable::URI.join(root_url, path)
18+
xml.lastmod @last_modified_at.strftime("%Y-%m-%dT%H:%M:%S+00:00")
19+
end
20+
end
21+
end

config/routes.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,15 @@
125125
# admin_mission_control_jobs /admin/jobs MissionControl::Jobs::Engine
126126
# admin_litestream /admin/litestream Litestream::Engine
127127
# rails_admin /admin/data RailsAdmin::Engine
128+
# sitemaps GET /sitemap.xml(.:format) sitemaps#index
129+
# sitemap_pages GET /sitemap-pages.xml(.:format) sitemaps#pages
128130
# GET /404(.:format) errors#not_found
129131
# GET /500(.:format) errors#internal_server
130132
# GET /422(.:format) errors#unprocessable
131133
# pwa_serviceworker GET /serviceworker(.:format) rails/pwa#serviceworker {:format=>"js"}
132134
# pwa_manifest GET /manifest(.:format) rails/pwa#manifest {:format=>"json"}
133135
# rails_health_check GET /up(.:format) rails/health#show
136+
# deploy_hatchbox GET /deploy/hatchbox(.:format) redirect(301, meta/deployment/hatchbox)
134137
# turbo_recede_historical_location GET /recede_historical_location(.:format) turbo/native/navigation#recede
135138
# turbo_resume_historical_location GET /resume_historical_location(.:format) turbo/native/navigation#resume
136139
# turbo_refresh_historical_location GET /refresh_historical_location(.:format) turbo/native/navigation#refresh
@@ -323,6 +326,9 @@
323326

324327
mount RailsAdmin::Engine => "/admin/data", :as => "rails_admin", :constraints => Routes::AdminAccessConstraint.new
325328

329+
get "/sitemap" => "sitemaps#index", :as => :sitemaps, :constraints => {format: "xml"}
330+
get "/sitemap-pages" => "sitemaps#pages", :as => :sitemap_pages, :constraints => {format: "xml"}
331+
326332
get "/404", to: "errors#not_found"
327333
get "/500", to: "errors#internal_server"
328334
get "/422", to: "errors#unprocessable"

spec/requests/sitemaps_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Sitemaps", type: :request do
4+
describe "GET /sitemaps" do
5+
it "renders list of sitemaps" do
6+
get sitemaps_path(format: :xml)
7+
8+
expect(response).to have_http_status(200)
9+
expect(document).to have_xpath("//sitemapindex/sitemap/loc", text: "http://example.com/sitemap-pages.xml")
10+
end
11+
end
12+
13+
describe "GET /sitemap-pages.xml" do
14+
it "renders sitemap" do
15+
pages = Page.upsert_collection_from_sitepress!(limit: 3)
16+
get sitemap_pages_path(format: :xml)
17+
18+
expect(response).to have_http_status(200)
19+
expect(document).to have_xpath("//urlset/url/loc", text: "http://example.com/")
20+
expect(document).to have_xpath("//urlset/url/loc", text: "http://example.com/about")
21+
expect(document).to have_xpath("//urlset/url/loc", text: "http://example.com/articles")
22+
expect(document).to have_xpath("//urlset/url/loc", text: "http://example.com/contact")
23+
expect(document).to have_xpath("//urlset/url/loc", text: "http://example.com/meta")
24+
expect(document).to have_xpath("//urlset/url/loc", text: "http://example.com/settings")
25+
26+
pages.each do |page|
27+
expect(document).to have_xpath("//urlset/url/loc", text: "http://example.com#{page.request_path}")
28+
end
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)