diff --git a/Backend/MotoresArgentinosV2.Infrastructure/Services/SitemapGeneratorService.cs b/Backend/MotoresArgentinosV2.Infrastructure/Services/SitemapGeneratorService.cs index 65dfef4..713f373 100644 --- a/Backend/MotoresArgentinosV2.Infrastructure/Services/SitemapGeneratorService.cs +++ b/Backend/MotoresArgentinosV2.Infrastructure/Services/SitemapGeneratorService.cs @@ -22,14 +22,19 @@ public class SitemapGeneratorService : BackgroundService private const string BaseUrl = "https://motoresargentinos.com"; - // Rutas estáticas públicas del frontend con sus prioridades - private static readonly (string Path, string Priority, string ChangeFreq)[] StaticRoutes = + // Rutas estáticas cuyo contenido cambia con cada aviso nuevo (usan lastmod dinámico) + private static readonly (string Path, string Priority, string ChangeFreq)[] DynamicDateRoutes = [ - ("/", "1.0", "daily"), - ("/explorar", "0.8", "daily"), - ("/publicar", "0.6", "monthly"), - ("/vender", "0.6", "monthly"), - ("/condiciones","0.3", "yearly"), + ("/", "1.0", "daily"), + ("/explorar", "0.8", "daily"), + ]; + + // Rutas estáticas cuyo contenido rara vez cambia (sin lastmod para no mentirle a Google) + private static readonly (string Path, string Priority, string ChangeFreq)[] FixedRoutes = + [ + ("/publicar", "0.6", "monthly"), + ("/vender", "0.6", "monthly"), + ("/condiciones", "0.3", "yearly"), ]; public SitemapGeneratorService(IServiceProvider serviceProvider, ILogger logger) @@ -77,27 +82,42 @@ public class SitemapGeneratorService : BackgroundService .Select(a => new { a.AdID, a.PublishedAt }) .ToListAsync(); - var now = DateTime.UtcNow.ToString("yyyy-MM-dd"); + // Fecha del aviso más reciente (para rutas cuyo contenido cambia con nuevos avisos) + var latestPublished = activeAds + .Where(a => a.PublishedAt.HasValue) + .Max(a => a.PublishedAt) + ?.ToString("yyyy-MM-dd") ?? DateTime.UtcNow.ToString("yyyy-MM-dd"); + var sb = new StringBuilder(); sb.AppendLine(""); sb.AppendLine(""); - // 1. Rutas estáticas - foreach (var (path, priority, changeFreq) in StaticRoutes) + // 1. Rutas cuyo contenido cambia con cada aviso nuevo (/, /explorar) + foreach (var (path, priority, changeFreq) in DynamicDateRoutes) { sb.AppendLine(" "); sb.AppendLine($" {BaseUrl}{path}"); - sb.AppendLine($" {now}"); + sb.AppendLine($" {latestPublished}"); sb.AppendLine($" {changeFreq}"); sb.AppendLine($" {priority}"); sb.AppendLine(" "); } - // 2. Rutas dinámicas (vehículos activos) + // 2. Rutas fijas (sin lastmod — no mentirle a Google) + foreach (var (path, priority, changeFreq) in FixedRoutes) + { + sb.AppendLine(" "); + sb.AppendLine($" {BaseUrl}{path}"); + sb.AppendLine($" {changeFreq}"); + sb.AppendLine($" {priority}"); + sb.AppendLine(" "); + } + + // 3. Rutas dinámicas (vehículos activos — lastmod = fecha de publicación real) foreach (var ad in activeAds) { - var lastmod = ad.PublishedAt?.ToString("yyyy-MM-dd") ?? now; + var lastmod = ad.PublishedAt?.ToString("yyyy-MM-dd") ?? latestPublished; sb.AppendLine(" "); sb.AppendLine($" {BaseUrl}/vehiculo/{ad.AdID}"); @@ -120,8 +140,8 @@ public class SitemapGeneratorService : BackgroundService await File.WriteAllTextAsync(tempPath, sb.ToString(), Encoding.UTF8); File.Move(tempPath, outputPath, overwrite: true); - var totalUrls = StaticRoutes.Length + activeAds.Count; + var totalUrls = DynamicDateRoutes.Length + FixedRoutes.Length + activeAds.Count; _logger.LogInformation("Sitemap generado con {TotalUrls} URLs ({StaticCount} estáticas + {DynamicCount} vehículos). Archivo: {Path}", - totalUrls, StaticRoutes.Length, activeAds.Count, outputPath); + totalUrls, DynamicDateRoutes.Length + FixedRoutes.Length, activeAds.Count, outputPath); } }