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.
This commit is contained in:
songtianlun 2025-01-23 19:40:08 +08:00
parent 1f47ba59c9
commit f477f205ab
3 changed files with 9 additions and 1 deletions

2
.gitignore vendored
View File

@ -38,3 +38,5 @@
/node_modules
.idea
public/sitemap.xml.gz

View File

@ -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

View File

@ -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