diff --git a/src/pages/rss.xml.js b/src/pages/rss.xml.js deleted file mode 100644 index ae5e4c4..0000000 --- a/src/pages/rss.xml.js +++ /dev/null @@ -1,16 +0,0 @@ -import rss from '@astrojs/rss'; -import { getCollection } from 'astro:content'; -import { SITE_TITLE, SITE_DESCRIPTION } from '../consts'; - -export async function GET(context) { - const posts = await getCollection('blog'); - return rss({ - title: SITE_TITLE, - description: SITE_DESCRIPTION, - site: context.site, - items: posts.map((post) => ({ - ...post.data, - link: `/blog/${post.id}/`, - })), - }); -} diff --git a/src/pages/rss.xml.ts b/src/pages/rss.xml.ts new file mode 100644 index 0000000..4bcf084 --- /dev/null +++ b/src/pages/rss.xml.ts @@ -0,0 +1,25 @@ +import rss from "@astrojs/rss"; +import { SITE_TITLE, SITE_DESCRIPTION } from "../consts"; +import { getCollection, type CollectionEntry } from "astro:content"; +import type { AstroUserConfig } from "astro"; + +function publishedOnly( + p: CollectionEntry<"blog">, +): p is CollectionEntry<"blog"> & { data: { pubDate: Date } } { + return p.data.pubDate !== undefined; +} + +export async function GET(context: AstroUserConfig) { + const posts = (await getCollection("blog", publishedOnly)).sort( + (a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf(), + ); + return rss({ + title: SITE_TITLE, + description: SITE_DESCRIPTION, + site: context.site as string, + items: posts.map((post) => ({ + ...post.data, + link: `/blog/${post.id}/`, + })), + }); +}