Fix: UpdatedAt del Sitemap
This commit is contained in:
@@ -22,14 +22,19 @@ public class SitemapGeneratorService : BackgroundService
|
|||||||
|
|
||||||
private const string BaseUrl = "https://motoresargentinos.com";
|
private const string BaseUrl = "https://motoresargentinos.com";
|
||||||
|
|
||||||
// Rutas estáticas públicas del frontend con sus prioridades
|
// Rutas estáticas cuyo contenido cambia con cada aviso nuevo (usan lastmod dinámico)
|
||||||
private static readonly (string Path, string Priority, string ChangeFreq)[] StaticRoutes =
|
private static readonly (string Path, string Priority, string ChangeFreq)[] DynamicDateRoutes =
|
||||||
[
|
[
|
||||||
("/", "1.0", "daily"),
|
("/", "1.0", "daily"),
|
||||||
("/explorar", "0.8", "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"),
|
("/publicar", "0.6", "monthly"),
|
||||||
("/vender", "0.6", "monthly"),
|
("/vender", "0.6", "monthly"),
|
||||||
("/condiciones","0.3", "yearly"),
|
("/condiciones", "0.3", "yearly"),
|
||||||
];
|
];
|
||||||
|
|
||||||
public SitemapGeneratorService(IServiceProvider serviceProvider, ILogger<SitemapGeneratorService> logger)
|
public SitemapGeneratorService(IServiceProvider serviceProvider, ILogger<SitemapGeneratorService> logger)
|
||||||
@@ -77,27 +82,42 @@ public class SitemapGeneratorService : BackgroundService
|
|||||||
.Select(a => new { a.AdID, a.PublishedAt })
|
.Select(a => new { a.AdID, a.PublishedAt })
|
||||||
.ToListAsync();
|
.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();
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
sb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
sb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
||||||
sb.AppendLine("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
|
sb.AppendLine("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
|
||||||
|
|
||||||
// 1. Rutas estáticas
|
// 1. Rutas cuyo contenido cambia con cada aviso nuevo (/, /explorar)
|
||||||
foreach (var (path, priority, changeFreq) in StaticRoutes)
|
foreach (var (path, priority, changeFreq) in DynamicDateRoutes)
|
||||||
{
|
{
|
||||||
sb.AppendLine(" <url>");
|
sb.AppendLine(" <url>");
|
||||||
sb.AppendLine($" <loc>{BaseUrl}{path}</loc>");
|
sb.AppendLine($" <loc>{BaseUrl}{path}</loc>");
|
||||||
sb.AppendLine($" <lastmod>{now}</lastmod>");
|
sb.AppendLine($" <lastmod>{latestPublished}</lastmod>");
|
||||||
sb.AppendLine($" <changefreq>{changeFreq}</changefreq>");
|
sb.AppendLine($" <changefreq>{changeFreq}</changefreq>");
|
||||||
sb.AppendLine($" <priority>{priority}</priority>");
|
sb.AppendLine($" <priority>{priority}</priority>");
|
||||||
sb.AppendLine(" </url>");
|
sb.AppendLine(" </url>");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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(" <url>");
|
||||||
|
sb.AppendLine($" <loc>{BaseUrl}{path}</loc>");
|
||||||
|
sb.AppendLine($" <changefreq>{changeFreq}</changefreq>");
|
||||||
|
sb.AppendLine($" <priority>{priority}</priority>");
|
||||||
|
sb.AppendLine(" </url>");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Rutas dinámicas (vehículos activos — lastmod = fecha de publicación real)
|
||||||
foreach (var ad in activeAds)
|
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(" <url>");
|
sb.AppendLine(" <url>");
|
||||||
sb.AppendLine($" <loc>{BaseUrl}/vehiculo/{ad.AdID}</loc>");
|
sb.AppendLine($" <loc>{BaseUrl}/vehiculo/{ad.AdID}</loc>");
|
||||||
@@ -120,8 +140,8 @@ public class SitemapGeneratorService : BackgroundService
|
|||||||
await File.WriteAllTextAsync(tempPath, sb.ToString(), Encoding.UTF8);
|
await File.WriteAllTextAsync(tempPath, sb.ToString(), Encoding.UTF8);
|
||||||
File.Move(tempPath, outputPath, overwrite: true);
|
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}",
|
_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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user