Skip to content

Commit b27c027

Browse files
committed
Test sitemap includes only public pages
1 parent 1e32636 commit b27c027

File tree

16 files changed

+223
-2
lines changed

16 files changed

+223
-2
lines changed

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ gem 'sentry-rails'
4242
gem 'sentry-ruby'
4343
gem 'stackprof'
4444

45+
# Sitemap generation
46+
gem 'sitemap_generator'
47+
4548
# Storext for easier json attributes, custom fork for Better Together
4649
gem 'storext', github: 'better-together-org/storext'
4750

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ PATH
5858
rswag (>= 2.3.1, < 2.17.0)
5959
ruby-openai
6060
simple_calendar
61+
sitemap_generator
6162
sprockets-rails
6263
stackprof
6364
stimulus-rails (~> 1.3)
@@ -733,6 +734,8 @@ GEM
733734
logger (>= 1.6.2)
734735
rack (>= 3.1.0)
735736
redis-client (>= 0.23.2)
737+
sitemap_generator (6.3.0)
738+
builder (~> 3.0)
736739
simple_calendar (3.1.0)
737740
rails (>= 6.1)
738741
simplecov (0.22.0)
@@ -864,6 +867,7 @@ DEPENDENCIES
864867
shoulda-callback-matchers
865868
shoulda-matchers
866869
sidekiq (~> 8.0.7)
870+
sitemap_generator
867871
simplecov
868872
spring
869873
spring-watcher-listen (~> 2.1.0)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
module BetterTogether
4+
# Serves the generated sitemap stored in Active Storage
5+
class SitemapsController < ApplicationController
6+
def show
7+
sitemap = Sitemap.current(helpers.host_platform)
8+
if sitemap.file.attached?
9+
redirect_to sitemap.file.url, allow_other_host: true
10+
else
11+
head :not_found
12+
end
13+
end
14+
end
15+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require 'rake'
4+
5+
module BetterTogether
6+
# Generates the sitemap in a background job so newly published pages are included
7+
class SitemapRefreshJob < ApplicationJob
8+
queue_as :default
9+
10+
def perform
11+
Rails.application.load_tasks unless Rake::Task.task_defined?('sitemap:refresh')
12+
Rake::Task['sitemap:refresh'].invoke
13+
Rake::Task['sitemap:refresh'].reenable
14+
end
15+
end
16+
end

app/models/better_together/page.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class Page < ApplicationRecord
4848
scope :published, -> { where.not(published_at: nil).where('published_at <= ?', Time.zone.now) }
4949
scope :by_publication_date, -> { order(published_at: :desc) }
5050

51+
after_commit :refresh_sitemap, on: %i[create update destroy]
52+
5153
def hero_block
5254
@hero_block ||= blocks.where(type: 'BetterTogether::Content::Hero').with_attached_background_image_file.with_translations.first
5355
end
@@ -96,5 +98,13 @@ def to_s
9698
def url
9799
"#{::BetterTogether.base_url_with_locale}/#{slug}"
98100
end
101+
102+
private
103+
104+
def refresh_sitemap
105+
return if Rails.env.test?
106+
107+
SitemapRefreshJob.perform_later
108+
end
99109
end
100110
end

app/models/better_together/platform.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class Platform < ApplicationRecord
3535
has_one_attached :profile_image
3636
has_one_attached :cover_image
3737

38+
has_one :sitemap, class_name: '::BetterTogether::Sitemap', dependent: :destroy
39+
3840
has_many :platform_blocks, dependent: :destroy, class_name: 'BetterTogether::Content::PlatformBlock'
3941
has_many :blocks, through: :platform_blocks
4042

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
module BetterTogether
4+
# Stores the generated sitemap in Active Storage for serving via S3
5+
class Sitemap < ApplicationRecord
6+
belongs_to :platform
7+
8+
has_one_attached :file
9+
10+
validates :platform_id, uniqueness: true
11+
12+
def self.current(platform)
13+
find_or_create_by!(platform: platform)
14+
end
15+
end
16+
end

app/views/layouts/better_together/application.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<meta name="viewport" content="width=device-width, initial-scale=1.0">
1616
<%= csrf_meta_tags %>
1717
<%= csp_meta_tag %>
18+
<link rel="sitemap" type="application/xml" href="<%= sitemap_path %>">
1819

1920
<!-- Default Stylesheets -->
2021
<link rel="stylesheet" type="text/css" href="https://unpkg.com/[email protected]/dist/trix.css">

better_together.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Gem::Specification.new do |spec|
6666
spec.add_dependency 'rswag', '>= 2.3.1', '< 2.17.0'
6767
spec.add_dependency 'ruby-openai'
6868
spec.add_dependency 'simple_calendar'
69+
spec.add_dependency 'sitemap_generator'
6970
spec.add_dependency 'sprockets-rails'
7071
spec.add_dependency 'stackprof'
7172
spec.add_dependency 'stimulus-rails', '~> 1.3'

config/routes.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
require 'sidekiq/web'
44

5-
BetterTogether::Engine.routes.draw do # rubocop:todo Metrics/BlockLength
5+
BetterTogether::Engine.routes.draw do # rubocop:todo Metrics/BlockLength
6+
get '/sitemap.xml.gz', to: 'sitemaps#show', as: :sitemap
7+
68
scope ':locale', # rubocop:todo Metrics/BlockLength
79
locale: /#{I18n.available_locales.join('|')}/ do
810
# bt base path

0 commit comments

Comments
 (0)