Fix listing sort order: order by pubDate, matching the date displayed
Deploy / build (push) Successful in 36s

Listings render `pubDate` but sorted by `updatedDate ?? pubDate`, so any
post carrying both dates sorted by a date it never displayed. After the
backdate commit the two diverged by years and the list read out of order
("January 7, 2026" above "September 13, 2026", "March 11, 2026" below
"December 10, 2025").

Replace `sortByUpdated` with `sortForListing`, which orders by `pubDate`
and uses `updatedDate` only as a tiebreak. This matches the RSS feed,
which already sorted by `pubDate`.

Verified all three listings (EN, EN homepage, ZH) are monotonically
non-increasing across the full 2024-09 -> 2026-09 span.
This commit is contained in:
hoelee committed 2026-09-21 21:57:10 +08:00
1 parent ae2ccedaad
commit 2df67b6c50
6 files changed
+34 -17

No files matched your search

+24 -7
View File
@@ -2,16 +2,33 @@ import type { CollectionEntry } from 'astro:content';
type Post = CollectionEntry<'posts'>;
/**
* Newest-first ordering for listings.
*
* Ordered by `pubDate` — the date the listing actually *renders* — so the
* visible order always matches the visible dates. `updatedDate` is only a
* tiebreak, for stable ordering when two posts share a publish date.
*
* Do NOT sort by `updatedDate ?? pubDate` here: listings display `pubDate`,
* so a revised-and-backdated post would sort by a date it never shows and the
* list would read out of order (e.g. "January 7, 2026" appearing above
* "September 13, 2026").
*/
export function sortForListing(posts: Post[]): Post[] {
return [...posts].sort((a, b) => {
const byPub = b.data.pubDate.valueOf() - a.data.pubDate.valueOf();
if (byPub !== 0) return byPub;
const aUpd = a.data.updatedDate?.valueOf() ?? 0;
const bUpd = b.data.updatedDate?.valueOf() ?? 0;
return bUpd - aUpd;
});
}
/**
* Effective "last updated" timestamp for a post — `updatedDate` when present,
* else `pubDate`. Used to sort listings newest-first by *update time*, so a
* revised post resurfaces to the top.
* else `pubDate`. Used where *recency of editing* is what matters (e.g.
* deciding whether a post resurfaced), NOT for rendering order.
*/
export function updatedAt(post: Post): Date {
return post.data.updatedDate ?? post.data.pubDate;
}
/** Sort posts newest-first by update time (updatedDate ?? pubDate). */
export function sortByUpdated(posts: Post[]): Post[] {
return [...posts].sort((a, b) => updatedAt(b).valueOf() - updatedAt(a).valueOf());
}