From 893232063095f7bfcff1c0058af8d35d32c44635 Mon Sep 17 00:00:00 2001 From: Finn Date: Thu, 24 Aug 2023 12:18:57 +0100 Subject: [PATCH] Replace sitemap gem with one that is maintained Add collections to sitemap Provide real `lastmod` dates --- Gemfile | 2 +- Gemfile.lock | 11 ++---- config/schedule.rb | 11 ++---- config/sitemap.rb | 90 ++++++++++++++-------------------------------- 4 files changed, 34 insertions(+), 80 deletions(-) diff --git a/Gemfile b/Gemfile index 88cdfdd26..c023ef1cb 100644 --- a/Gemfile +++ b/Gemfile @@ -52,7 +52,7 @@ gem 'activerecord-session_store' gem 'gravtastic', '~> 3.2.6' -gem 'dynamic_sitemaps', github: 'lassebunk/dynamic_sitemaps', branch: 'master' +gem 'sitemap_generator' gem 'whenever', '~> 1.0.0' diff --git a/Gemfile.lock b/Gemfile.lock index 473edb9f8..21ce56c2c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,13 +5,6 @@ GIT tess_rdf_extractors (0.1.0) linkeddata (~> 3.2.0) -GIT - remote: https://github.com/lassebunk/dynamic_sitemaps.git - revision: 37beeba143e8202218ee814056231b0e6974bafb - branch: master - specs: - dynamic_sitemaps (2.0.0) - GIT remote: https://github.com/radar/by_star revision: d7811c0c50f30262b4862b384c50f22f36e54f07 @@ -687,6 +680,8 @@ GEM sitemap-parser (0.5.6) nokogiri (~> 1.6) typhoeus (>= 0.6, < 2.0) + sitemap_generator (6.3.0) + builder (~> 3.0) sixarm_ruby_unaccent (1.2.0) slim (4.1.0) temple (>= 0.7.6, < 0.9) @@ -800,7 +795,6 @@ DEPENDENCIES country_select (<= 4.0.0) devise devise_invitable (~> 2.0.5) - dynamic_sitemaps! eventbrite_sdk font-awesome-sass (~> 4.7.0) friendly_id (~> 5.2.4) @@ -863,6 +857,7 @@ DEPENDENCIES simplecov simplecov-lcov sitemap-parser (~> 0.5.6) + sitemap_generator slim sunspot_rails! terser diff --git a/config/schedule.rb b/config/schedule.rb index b8dac7fcb..0b42568be 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -15,14 +15,9 @@ end # Generate a new sitemap... -if !schedules['sitemap'].nil? - every :"#{schedules['sitemap']['every']}", at: "#{schedules['sitemap']['at']}" do - rake "sitemap:generate" - end -else - every :day, at: '1am' do - rake "sitemap:generate" - end +every schedules.dig('sitemap', 'every')&.to_sym || :day, + at: schedules.dig('sitemap', 'at') || '1am' do + rake "sitemap:refresh" end # Process subscriptions... diff --git a/config/sitemap.rb b/config/sitemap.rb index c2f14a7f1..2731466ee 100644 --- a/config/sitemap.rb +++ b/config/sitemap.rb @@ -1,66 +1,30 @@ -# Change this to your host. See the readme at https://github.com/lassebunk/dynamic_sitemaps -# for examples of multiple hosts and folders. -base_url = URI.parse(TeSS::Config.base_url) -host_with_port = base_url.host -if base_url.port != base_url.default_port - host_with_port += ":#{base_url.port}" -end - -protocol base_url.scheme -host host_with_port - -sitemap :site do - url root_url, last_mod: Time.now, change_freq: 'daily', priority: 1.0 - url about_url, change_freq: 'weekly', priority: 0.4 - if TeSS::Config.feature['materials'] - url materials_url, last_mod: Time.now, change_freq: 'daily', priority: 0.7 - end - if TeSS::Config.feature['events'] - url events_url, last_mod: Time.now, change_freq: 'daily', priority: 0.7 +# Set the host name for URL creation +SitemapGenerator::Sitemap.default_host = TeSS::Config.base_url + +SitemapGenerator::Sitemap.create(compress: false, sitemaps_path: 'sitemaps/', include_root: false) do + types = { + materials: { resources: Material.from_verified_users, changefreq: 'daily', priority: 0.7 }, + events: { resources: Event.from_verified_users, changefreq: 'daily', priority: 0.7 }, + content_providers: { resources: ContentProvider, changefreq: 'weekly', priority: 0.4 }, + workflows: { resources: Workflow.from_verified_users.visible_by(nil), changefreq: 'daily', priority: 0.6 }, + collections: { resources: Collection.from_verified_users.visible_by(nil), changefreq: 'daily', priority: 0.6 } + } + + group(filename: :site) do + add root_path, changefreq: 'daily', priority: 1.0 + add about_path, priority: 0.4 + types.each do |type, options| + next unless TeSS::Config.feature[type.to_s] + add polymorphic_path(type), lastmod: options[:resources].maximum(:updated_at), **options.except(:resources) + end end - if TeSS::Config.feature['workflows'] - url workflows_url, last_mod: Time.now, change_freq: 'daily', priority: 0.6 - end - if TeSS::Config.feature['content_providers'] - url content_providers_url, last_mod: Time.now, change_freq: 'weekly', priority: 0.4 + + types.each do |type, options| + next unless TeSS::Config.feature[type.to_s] + group(filename: type) do + options[:resources].find_each do |resource| + add polymorphic_path(resource), lastmod: resource.updated_at + end + end end end - -# You can have multiple sitemaps like the above – just make sure their names are different. - -# Automatically link to all pages using the routes specified -# using "resources :pages" in config/routes.rb. This will also -# automatically set to the date and time in page.updated_at: -# -# sitemap_for Page.scoped - -sitemap_for Material.from_verified_users if TeSS::Config.feature['materials'] -sitemap_for Event.from_verified_users if TeSS::Config.feature['events'] -sitemap_for ContentProvider if TeSS::Config.feature['content_providers'] -sitemap_for Workflow.from_verified_users.visible_by(nil) if TeSS::Config.feature['workflows'] - -# For products with special sitemap name and priority, and link to comments: -# -# sitemap_for Product.published, name: :published_products do |product| -# url product, last_mod: product.updated_at, priority: (product.featured? ? 1.0 : 0.7) -# url product_comments_url(product) -# end - -# If you want to generate multiple sitemaps in different folders (for example if you have -# more than one domain, you can specify a folder before the sitemap definitions: -# -# Site.all.each do |site| -# folder "sitemaps/#{site.domain}" -# host site.domain -# -# sitemap :site do -# url root_url -# end -# -# sitemap_for site.products.scoped -# end - -# Ping search engines after sitemap generation: -# - -ping_with "#{base_url.scheme}://#{host}/sitemap.xml" if Rails.env.production?