feat: update sitemap generation for multiple locales

- Refactor generate_sitemap method to support locale
- Add setup_sitemap_config method for configuration
- Implement sitemap generation for default and localized paths

This update enhances the sitemap generation process by supporting
multiple languages. Users can now access a sitemap with language
prefixes, improving SEO and usability for different locales.
Additionally, error handling has been improved to log specific
errors related to locale generation.
This commit is contained in:
songtianlun 2025-02-22 12:08:59 +08:00
parent 5b996bb64a
commit 09fa1ceea9
3 changed files with 77 additions and 20 deletions

View File

@ -10,7 +10,14 @@ class RefreshSitemapWorker
if redis.set(lock_key, Time.current.to_s, nx: true, ex: lock_ttl)
begin
generate_sitemap
setup_sitemap_config
# 生成默认的不带语言前缀的 sitemap
generate_sitemap(nil)
# 为每个可用语言生成带前缀的 sitemap
I18n.available_locales.each do |locale|
generate_sitemap(locale)
end
ensure
redis.del(lock_key)
end
@ -21,10 +28,11 @@ class RefreshSitemapWorker
private
def generate_sitemap
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 = ENV.fetch("RAILS_SITEMAP_DEFAULT_HOST", host)
def setup_sitemap_config
@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 = ENV.fetch("RAILS_SITEMAP_DEFAULT_HOST", @host)
if Rails.env.production?
SitemapGenerator::Sitemap.adapter = SitemapGenerator::AwsSdkAdapter.new(
ENV.fetch("AWS_BUCKET", Rails.application.credentials.dig(:minio, :bucket)),
@ -45,36 +53,77 @@ class RefreshSitemapWorker
)
end
SitemapGenerator::Sitemap.sitemaps_path = "sitemaps/"
SitemapGenerator::Sitemap.create do
add root_path, changefreq: "daily", priority: 1.0
add cities_path, changefreq: "daily", priority: 0.9
add arts_path, changefreq: "daily", priority: 0.9
City.find_each do |city|
add city_path(city),
changefreq: "daily",
priority: 0.8,
lastmod: city.updated_at
end
def generate_sitemap(locale = nil)
# 设置当前语言环境
I18n.locale = locale || I18n.default_locale
Rails.application.routes.default_url_options[:locale] = locale
# 设置 sitemap 路径
# path_prefix = locale ? "sitemaps/#{locale}/" : "sitemaps/"
# SitemapGenerator::Sitemap.sitemaps_path = path_prefix
filename = locale==nil ? "sitemap" : "sitemap_#{locale}"
SitemapGenerator::Sitemap.create(filename: filename) do
available_locales = I18n.available_locales
# 首页
add root_path(locale: locale),
changefreq: "daily",
priority: 1.0,
alternate: available_locales.map { |al|
{ lang: al, href: root_url(locale: al) }
}
# 城市列表页
add cities_path(locale: locale),
changefreq: "daily",
priority: 0.9,
alternate: available_locales.map { |al|
{ lang: al, href: cities_url(locale: al) }
}
# 艺术作品列表页
add arts_path(locale: locale),
changefreq: "daily",
priority: 0.9,
alternate: available_locales.map { |al|
{ lang: al, href: arts_url(locale: al) }
}
# 城市详情页
City.find_each do |city|
add city_path(city, locale: locale),
changefreq: "daily",
priority: 0.8,
lastmod: city.updated_at,
alternate: available_locales.map { |al|
{ lang: al, href: city_url(city, locale: al) }
}
end
# 天气艺术作品页
WeatherArt.includes(:city).find_each do |art|
if art.image.attached?
add city_weather_art_path(art.city, art),
add city_weather_art_path(art.city, art, locale: locale),
changefreq: "daily",
priority: 0.7,
lastmod: art.updated_at,
images: [ {
loc: url_for(art.image),
title: "#{art.city.name} Weather Art - #{art.weather_date.strftime('%B %d, %Y')}"
} ]
} ],
alternate: available_locales.map { |al|
{ lang: al, href: city_weather_art_url(art.city, art, locale: al) }
}
end
end
end
# SitemapGenerator::Sitemap.ping_search_engines if Rails.env.production?
Rails.logger.info "Sitemap has been generated and uploaded to S3 successfully"
Rails.logger.info "Generated sitemap for #{locale || 'default'} version"
rescue => e
Rails.logger.error "Error refreshing sitemap: #{e.message}"
Rails.logger.error "Error generating sitemap for #{locale || 'default'}: #{e.message}"
raise e
end
end

View File

@ -60,3 +60,7 @@ ja:
time_only: "%H:%M"
with_zone: "%{time} %{zone}"
date_and_time: "%{date} %{time}"
date:
formats:
short: "%Y-%m-%d"
long: "%Y 年 %m 月 %d 日"

View File

@ -60,3 +60,7 @@ ko:
time_only: "%H:%M"
with_zone: "%{time} %{zone}"
date_and_time: "%{date} %{time}"
date:
formats:
short: "%Y-%m-%d"
long: "%Y 년 %m 월 %d 일"