From f477f205abc00994c4a490e15e91bf6515f2500c Mon Sep 17 00:00:00 2001 From: songtianlun Date: Thu, 23 Jan 2025 19:40:08 +0800 Subject: [PATCH] fix: update default host in sitemap generator and refresh sitemap on worker - update default host in sitemap generator - refresh sitemap on worker with new host These changes allow the sitemap to be correctly generated for different environments and to handle the new host correctly, which was the motivation behind this update. No other side effects are expected. --- .gitignore | 2 ++ app/workers/refresh_sitemap_worker.rb | 4 ++++ config/sitemap.rb | 4 +++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index be844dd..bdebf4a 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,5 @@ /node_modules .idea + +public/sitemap.xml.gz diff --git a/app/workers/refresh_sitemap_worker.rb b/app/workers/refresh_sitemap_worker.rb index 6a5e32e..8ff7b7a 100644 --- a/app/workers/refresh_sitemap_worker.rb +++ b/app/workers/refresh_sitemap_worker.rb @@ -2,6 +2,9 @@ class RefreshSitemapWorker include Sidekiq::Worker def perform + host = Rails.env.production? ? "https://todayaiweather.com" : "http://127.0.0.1:3000" + Rails.application.routes.default_url_options[:host] = host + SitemapGenerator::Sitemap.default_host = host SitemapGenerator::Sitemap.create do add root_path, changefreq: "daily", priority: 1.0 add cities_path, changefreq: "daily", priority: 0.9 @@ -29,6 +32,7 @@ class RefreshSitemapWorker end SitemapGenerator::Sitemap.ping_search_engines if Rails.env.production? + Rails.logger.info "Sitemap has been generated successfully" rescue => e Rails.logger.error "Error refreshing sitemap: #{e.message}" end diff --git a/config/sitemap.rb b/config/sitemap.rb index cd2ec31..9fb27a2 100644 --- a/config/sitemap.rb +++ b/config/sitemap.rb @@ -1,5 +1,7 @@ # Set the host name for URL creation -SitemapGenerator::Sitemap.default_host = "https://todayaiweather.com" +host = Rails.env.production? ? "https://todayaiweather.com" : "http://127.0.0.1:3000" + +SitemapGenerator::Sitemap.default_host = host SitemapGenerator::Sitemap.create do add root_path, changefreq: "daily", priority: 1.0