Generate one sitemap per tenant

Note that the `sitemap:refresh` task only pings search engines at the
end, so it only does so for the `Sitemap.default_host` defined last. So
we're using the `sitemap:refresh:no_ping` task instead and pinging
search engines after creating each sitemap.

Note we're pinging search engines in staging and preproduction
environments. I'm leaving it that way because that's what we've done
until now, but I wonder whether we should only do so on production.

Since we're creating a new method to get the current url_options, we're
also using it in the dev_seeds.
This commit is contained in:
Javi Martín
2022-09-26 22:20:27 +02:00
parent cc87bca500
commit 5100884110
7 changed files with 98 additions and 42 deletions

View File

@@ -20,6 +20,7 @@ storage/
# Files generated by scripts or compiled # Files generated by scripts or compiled
public/sitemap.xml public/sitemap.xml
public/tenants/*/sitemap.xml
public/assets/ public/assets/
public/machine_learning/data/ public/machine_learning/data/

1
.gitignore vendored
View File

@@ -22,6 +22,7 @@ tmp/
# Files generated by scripts or compiled # Files generated by scripts or compiled
/public/sitemap.xml /public/sitemap.xml
/public/tenants/*/sitemap.xml
/public/assets/ /public/assets/
/public/machine_learning/data/ /public/machine_learning/data/

View File

@@ -35,6 +35,10 @@ class Tenant < ApplicationRecord
Apartment::Tenant.current Apartment::Tenant.current
end end
def self.current_url_options
ApplicationMailer.new.default_url_options
end
def self.default? def self.default?
current_schema == "public" current_schema == "public"
end end

View File

@@ -24,7 +24,7 @@ every 1.minute do
end end
every 1.day, at: "5:00 am" do every 1.day, at: "5:00 am" do
rake "-s sitemap:refresh" rake "-s sitemap:refresh:no_ping"
end end
every 2.hours do every 2.hours do

View File

@@ -6,13 +6,18 @@ class SitemapGenerator::FileAdapter
end end
end end
SitemapGenerator::Sitemap.namer = SitemapGenerator::SimpleNamer.new(:sitemap, extension: ".xml") SitemapGenerator::Sitemap.namer = SitemapGenerator::SimpleNamer.new(:sitemap, extension: ".xml")
# default host
SitemapGenerator::Sitemap.verbose = false if Rails.env.test? SitemapGenerator::Sitemap.verbose = false if Rails.env.test?
SitemapGenerator::Sitemap.default_host = root_url(ActionMailer::Base.default_url_options)
# sitemap generator Tenant.run_on_each do
SitemapGenerator::Sitemap.create do SitemapGenerator::Sitemap.default_host = root_url(Tenant.current_url_options)
if Tenant.default?
SitemapGenerator::Sitemap.sitemaps_path = nil
else
SitemapGenerator::Sitemap.sitemaps_path = "tenants/#{Tenant.current_schema}"
end
SitemapGenerator::Sitemap.create do
add help_path add help_path
add how_to_use_path add how_to_use_path
add faq_path add faq_path
@@ -51,4 +56,9 @@ SitemapGenerator::Sitemap.create do
add legislation_process_path(process), lastmod: process.start_date add legislation_process_path(process), lastmod: process.start_date
end end
end end
end
unless Rails.env.development? || Rails.env.test?
SitemapGenerator::Sitemap.ping_search_engines
end
end end

View File

@@ -5,7 +5,7 @@ section "Creating Admin Notifications & Templates" do
-> { I18n.t("seeds.admin_notifications.proposal.#{attribute}") } -> { I18n.t("seeds.admin_notifications.proposal.#{attribute}") }
end end
).merge( ).merge(
link: Rails.application.routes.url_helpers.proposals_url(ActionMailer::Base.default_url_options), link: Rails.application.routes.url_helpers.proposals_url(Tenant.current_url_options),
segment_recipient: "administrators" segment_recipient: "administrators"
) )
).deliver ).deliver

View File

@@ -2,6 +2,7 @@ require "rails_helper"
describe "rake sitemap:create", type: :system do describe "rake sitemap:create", type: :system do
let(:file) { Rails.root.join("public", "sitemap.xml") } let(:file) { Rails.root.join("public", "sitemap.xml") }
let(:run_rake_task) { Rake.application.invoke_task("sitemap:create") }
before do before do
FileUtils.rm_f(file) FileUtils.rm_f(file)
@@ -9,7 +10,7 @@ describe "rake sitemap:create", type: :system do
end end
describe "when processes are enabled" do describe "when processes are enabled" do
before { Rake.application.invoke_task("sitemap:create") } before { run_rake_task }
it "generates a valid sitemap" do it "generates a valid sitemap" do
sitemap = Nokogiri::XML(File.open(file)) sitemap = Nokogiri::XML(File.open(file))
@@ -44,7 +45,7 @@ describe "rake sitemap:create", type: :system do
Setting["process.polls"] = nil Setting["process.polls"] = nil
Setting["process.legislation"] = nil Setting["process.legislation"] = nil
Rake.application.invoke_task("sitemap:create") run_rake_task
end end
it "generates a valid sitemap" do it "generates a valid sitemap" do
@@ -71,4 +72,43 @@ describe "rake sitemap:create", type: :system do
expect(sitemap).not_to have_content("daily") expect(sitemap).not_to have_content("daily")
end end
end end
it "generates a sitemap for every tenant" do
allow(ActionMailer::Base).to receive(:default_url_options).and_return({ host: "consul.dev" })
FileUtils.rm_f(Dir.glob(Rails.root.join("public", "tenants", "*", "sitemap.xml")))
create(:tenant, schema: "debates")
create(:tenant, schema: "proposals")
Setting["process.debates"] = false
Setting["process.proposals"] = false
Tenant.switch("debates") do
Setting["process.budgets"] = false
Setting["process.proposals"] = false
end
Tenant.switch("proposals") do
Setting["process.budgets"] = false
Setting["process.debates"] = false
end
run_rake_task
public_sitemap = File.read(file)
debates_sitemap = File.read(Rails.root.join("public", "tenants", "debates", "sitemap.xml"))
proposals_sitemap = File.read(Rails.root.join("public", "tenants", "proposals", "sitemap.xml"))
expect(public_sitemap).to have_content budgets_url(host: "consul.dev")
expect(public_sitemap).not_to have_content debates_path
expect(public_sitemap).not_to have_content proposals_path
expect(debates_sitemap).to have_content debates_url(host: "debates.consul.dev")
expect(debates_sitemap).not_to have_content budgets_path
expect(debates_sitemap).not_to have_content proposals_path
expect(proposals_sitemap).to have_content proposals_url(host: "proposals.consul.dev")
expect(proposals_sitemap).not_to have_content budgets_path
expect(proposals_sitemap).not_to have_content debates_path
end
end end