portfolio/app/feed.xml/route.ts

50 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

2023-08-07 13:58:08 +00:00
import {NextRequest} from 'next/server'
import {Feed} from 'feed'
import {getAllArticles} from '@/lib/getAllArticles'
export async function GET(req: NextRequest) {
let siteUrl = process.env.NEXT_PUBLIC_SITE_URL ?? ""
let articles = await getAllArticles()
let author = {
name: 'Ryan Freeman',
email: 'hello@ryanfreeman.dev'
}
let feed = new Feed({
title: author.name,
description: 'Full-stack software engineer who enjoys building cloud-native applications.',
author,
id: siteUrl,
link: siteUrl,
image: `${siteUrl}/favicon.ico`,
favicon: `${siteUrl}/favicon.ico`,
copyright: `Copyright ${(new Date).getFullYear()} Ryan Freeman. All rights reserved.`,
feedLinks: {
rss2: `${siteUrl}/feed.xml`
}
})
for (let article of articles) {
let url = `${siteUrl}/writing/${article.slug}`
2024-09-17 14:51:45 +00:00
let title = article.title
let date = new Date(article.date)
let description = article.description
2023-08-07 13:58:08 +00:00
feed.addItem({
title,
id: url,
link: url,
2024-09-17 14:51:45 +00:00
description,
2023-08-07 13:58:08 +00:00
author: [author],
contributor: [author],
2024-09-17 14:51:45 +00:00
date
2023-08-07 13:58:08 +00:00
})
}
return new Response(feed.rss2(), {
status: 200,
headers: {
2023-08-07 20:12:34 +00:00
'Content-Type': 'application/xml'
2023-08-07 13:58:08 +00:00
}
})
}