Skip to content

Commit

Permalink
add sitemap controller
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienpoly committed Oct 22, 2023
1 parent 08ab8a0 commit 9093470
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
40 changes: 40 additions & 0 deletions app/controllers/sitemaps_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class SitemapsController < ApplicationController
def show
render xml: generate_sitemap_string
end

private

def generate_sitemap_string
Rails.cache.fetch("sitemap", expires_in: 24.hours) do
adapter = SitemapStringAdapter.new

SitemapGenerator::Sitemap.default_host = "https://www.rubyvideo.dev"

SitemapGenerator::Sitemap.create(adapter: adapter) do
add talks_path, priority: 0.9, changefreq: "daily"

Talk.find_each do |talk|
add talk_path(talk), priority: 0.8, lastmod: talk.updated_at
end

add speakers_path, priority: 0.7, changefreq: "daily"

Speaker.find_each do |speaker|
add speaker_path(speaker), priority: 0.8, lastmod: speaker.updated_at
end
end

adapter.sitemap_content
end
end

class SitemapStringAdapter
attr_reader :sitemap_content

def write(location, raw_data)
@sitemap_content ||= ""
@sitemap_content << raw_data
end
end
end
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
namespace :admin, if: -> { Current.user & admin? } do
resources :suggestions, only: %i[index update destroy]
end

get "/sitemap.xml", to: "sitemaps#show", defaults: {format: "xml"}

# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
Expand Down
5 changes: 4 additions & 1 deletion public/robots.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
User-agent: *
Disallow:

Sitemap: https://www.rubyvideo.com/sitemap.xml

0 comments on commit 9093470

Please sign in to comment.