fix(ui): add defensive sorting for blog posts

Implement client-side sorting for blog post lists to ensure they are
displayed in descending order by numeric ID. This prevents issues
where API response ordering or file modification timestamps might
cause older posts to appear newer than recent ones.

Also add backend tests to verify blog ordering logic.
This commit is contained in:
Tomas Dvorak
2026-05-11 13:03:04 +02:00
parent a6b47de1a4
commit 76c447a395
5 changed files with 196 additions and 2 deletions
+7 -1
View File
@@ -234,7 +234,13 @@
s.textContent = 'Načítám…';
const res = await fetch('/api/blog/latest?limit=12');
if (!res.ok) throw new Error('HTTP '+res.status);
const items = await res.json();
let items = await res.json();
// Defensive sort: numeric ID descending ensures newest first regardless of API ordering
items.sort((a,b)=>{
const ai = parseInt(a.id,10); const bi = parseInt(b.id,10);
if (!isNaN(ai) && !isNaN(bi)) return bi-ai;
return (b.id||'').localeCompare(a.id||'');
});
grid.innerHTML='';
if (!Array.isArray(items) || items.length === 0) {
grid.innerHTML = '<div class="muted">Žádné příspěvky.</div>';