fix(seo): B23 — fiches du sitemap en fonction pure testée (jamais le repli seed, Id nul ignoré) — checkpoint AEP 22/10

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AB8CwgrRNDCUvEnhvL5wRP
This commit is contained in:
Jules Neny
2026-10-22 23:44:23 +02:00
co-authored by Claude Fable 5.1
parent f9ce7a495a
commit 902517bd3c
3 changed files with 28 additions and 10 deletions
+8 -1
View File
@@ -13,7 +13,7 @@ import { dirname, join } from 'node:path'
const __dirname = dirname(fileURLToPath(import.meta.url))
const jiti = createJiti(import.meta.url)
const { canonique, robots, redirection, robotsTxt, cheminsSitemap, sitemapXml, dateSitemap } = await jiti.import(
const { canonique, robots, redirection, robotsTxt, cheminsSitemap, sitemapXml, dateSitemap, entreesFichesSitemap } = await jiti.import(
join(__dirname, '..', 'utils', 'seo.ts'),
)
@@ -90,6 +90,13 @@ egal('sitemap XML échappé', xml.includes('fiche/1?a=1&amp;b=2'), true)
egal('sitemap XML en-tête', xml.startsWith('<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'), true)
egal('dateSitemap', [dateSitemap('2026-10-13 08:12:00+00:00'), dateSitemap(''), dateSitemap('n/a')], ['2026-10-13', undefined, undefined])
// Fiches du sitemap : jamais le repli seed, au moins une /fiche/ quand NocoDB répond, Id nul ignoré (checkpoint 22/10)
const fiches = [{ Id: 108, UpdatedAt: '2026-10-01 10:00:00+00:00' }, { Id: null }, { Id: 7 }]
egal('fiches sitemap : repli seed → aucune', entreesFichesSitemap({ source: 'seed', list: fiches }), [])
egal('fiches sitemap : nocodb → les fiches avec Id', entreesFichesSitemap({ source: 'nocodb', list: fiches }, A),
[{ loc: `${A}/fiche/108`, lastmod: '2026-10-01' }, { loc: `${A}/fiche/7`, lastmod: undefined }])
egal('fiches sitemap : réponse absente → aucune', [entreesFichesSitemap(null), entreesFichesSitemap({ source: 'nocodb' })], [[], []])
// ── HTTP (optionnel, serveur lancé) ─────────────────────────────
const base = process.argv[2]
if (base) {
+4 -9
View File
@@ -5,7 +5,7 @@
*/
// @ts-expect-error module virtuel posé par modules/seo-routes.ts
import routes from '#seo-routes'
import { SITE_AEP, SITE_EVENT, cheminsSitemap, dateSitemap, estHoteEvent, sitemapXml, type EntreeSitemap } from '../../utils/seo'
import { SITE_AEP, SITE_EVENT, cheminsSitemap, entreesFichesSitemap, estHoteEvent, sitemapXml, type EntreeSitemap } from '../../utils/seo'
export default defineCachedEventHandler(async (event) => {
const host = getRequestHost(event, { xForwardedHost: true })
@@ -13,14 +13,9 @@ export default defineCachedEventHandler(async (event) => {
const entrees: EntreeSitemap[] = cheminsSitemap(routes as string[], host).map(p => ({ loc: site + (p === '/' ? '/' : p) }))
if (!estHoteEvent(host)) {
const data: any = await $fetch('/api/organisations').catch(() => null)
// Le repli « seed » porte des Id fictifs (1000+) : jamais dans le sitemap.
if (data?.source === 'nocodb') {
for (const r of data.list ?? []) {
if (r?.Id == null) continue
entrees.push({ loc: `${SITE_AEP}/fiche/${r.Id}`, lastmod: dateSitemap(r.UpdatedAt ?? r.updated_at ?? r.CreatedAt) })
}
}
const data = await $fetch('/api/organisations').catch(() => null)
// Le repli « seed » porte des Id fictifs (1000+) : jamais dans le sitemap (règle portée et testée par entreesFichesSitemap).
entrees.push(...entreesFichesSitemap(data, SITE_AEP))
}
setHeader(event, 'Content-Type', 'application/xml; charset=utf-8')
+16
View File
@@ -173,6 +173,22 @@ export function sitemapXml(entrees: EntreeSitemap[]): string {
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${lignes.join('\n')}\n</urlset>\n`
}
/**
* Entrées /fiche/<Id> du sitemap depuis la réponse de /api/organisations (B23).
* Seule la source `nocodb` compte : le repli « seed » porte des Id fictifs (1000+) et ne sort jamais.
* Une fiche sans Id est ignorée. (Fonction pure, testée : checkpoint du 22/10.)
*/
export function entreesFichesSitemap(data: unknown, site: string = SITE_AEP): EntreeSitemap[] {
const d = data as { source?: unknown; list?: unknown } | null | undefined
if (!d || d.source !== 'nocodb' || !Array.isArray(d.list)) return []
const out: EntreeSitemap[] = []
for (const r of d.list as Array<Record<string, unknown>>) {
if (r?.Id == null) continue
out.push({ loc: `${site}/fiche/${r.Id}`, lastmod: dateSitemap(r.UpdatedAt ?? r.updated_at ?? r.CreatedAt) })
}
return out
}
/** Date AAAA-MM-JJ depuis un champ de date NocoDB, ou undefined. */
export function dateSitemap(v: unknown): string | undefined {
if (!v) return undefined