2024-08-24 06:28:25 +02:00
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
import RSS from "rss";
|
2024-09-08 12:31:47 +02:00
|
|
|
import { client, urlFor } from "@/lib/sanity";
|
2024-08-24 06:28:25 +02:00
|
|
|
import { toHTML } from "@portabletext/to-html";
|
|
|
|
|
2024-09-08 12:28:53 +02:00
|
|
|
export const dynamic = "force-static";
|
|
|
|
|
2024-08-24 06:28:25 +02:00
|
|
|
export async function GET() {
|
2024-09-07 09:12:48 +02:00
|
|
|
// Define the site URL based on the environment
|
|
|
|
const SITE_URL =
|
|
|
|
process.env.NODE_ENV === "production"
|
2024-09-08 13:16:43 +02:00
|
|
|
? `${process.env.WEBSITE_URL}`
|
2024-09-07 09:12:48 +02:00
|
|
|
: "http://localhost:3000";
|
2024-08-24 06:44:39 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const postsQuery = `*[_type == 'blog'] | order(_createdAt desc) {
|
2024-08-24 06:28:25 +02:00
|
|
|
title,
|
|
|
|
"slug": slug.current,
|
|
|
|
content,
|
|
|
|
titleImage,
|
|
|
|
_createdAt
|
|
|
|
}`;
|
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const posts = await client.fetch(postsQuery);
|
2024-08-24 06:28:25 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const feed = new RSS({
|
2024-09-07 09:39:26 +02:00
|
|
|
title: "SVR.JS Blog",
|
|
|
|
description: "Explore the latest blog posts from SVR.JS",
|
2024-09-07 09:12:48 +02:00
|
|
|
feed_url: `${SITE_URL}/rss.xml`,
|
|
|
|
site_url: `${SITE_URL}`,
|
|
|
|
image_url: `${SITE_URL}/metadata/svrjs-cover.png`,
|
|
|
|
language: "en-US",
|
|
|
|
pubDate: new Date().toUTCString()
|
|
|
|
});
|
2024-08-24 06:28:25 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
posts.forEach((post: any) => {
|
|
|
|
feed.item({
|
|
|
|
title: post.title,
|
|
|
|
description: toHTML(post.content),
|
|
|
|
url: `${SITE_URL}/blog/${post.slug}`,
|
2024-09-08 12:31:47 +02:00
|
|
|
date: new Date(post._createdAt).toUTCString(),
|
|
|
|
enclosure: {
|
|
|
|
url: post.titleImage
|
|
|
|
? urlFor(post.titleImage).url()
|
|
|
|
: `${SITE_URL}/blog-missing.png`
|
|
|
|
},
|
|
|
|
author: "SVR.JS"
|
2024-09-07 09:12:48 +02:00
|
|
|
});
|
|
|
|
});
|
2024-08-24 06:28:25 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
return new NextResponse(feed.xml({ indent: true }), {
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/xml"
|
|
|
|
}
|
|
|
|
});
|
2024-08-24 06:28:25 +02:00
|
|
|
}
|