🐛 Fix rss

This commit is contained in:
LittleSheep 2024-02-24 22:08:36 +08:00
parent b2094778f3
commit d72aa8ec51
2 changed files with 29 additions and 13 deletions

View File

@ -1,28 +1,44 @@
import RSS from "rss"; import { Feed } from "feed";
import { SITE_DESCRIPTION, SITE_NAME, SITE_URL } from "@/app/consts"; import { SITE_DESCRIPTION, SITE_NAME, SITE_URL } from "@/app/consts";
import { getSortedPosts } from "@/content/posts"; import { client } from "@/sanity/lib/client";
export async function GET() { export async function GET() {
const feed = new RSS({ const feed = new Feed({
title: SITE_NAME, title: SITE_NAME,
description: SITE_DESCRIPTION, description: SITE_DESCRIPTION,
site_url: SITE_URL, id: SITE_URL,
feed_url: `${SITE_URL}/feed`, link: SITE_URL,
favicon: `${SITE_URL}/favicon.png`,
feedLinks: { atom: `${SITE_URL}/feed` },
language: "zh-CN", language: "zh-CN",
copyright: `Copyright © ${new Date().getFullYear()} SmartSheep Studio`,
}); });
getSortedPosts().forEach((item) => { const posts = await client.fetch<any[]>(`*[_type == "post"] {
feed.item({ title, description, slug, publishedAt,
url: `${SITE_URL}/p/${item.id}`, mainImage {
asset -> {
_id,
url
},
alt
},
"categories": categories[]->title,
}`);
posts.forEach((item) => {
feed.addItem({
id: `${SITE_URL}/p/${item.slug.current}`,
link: `${SITE_URL}/p/${item.slug.current}`,
title: item.title, title: item.title,
description: item.description ?? "No description yet.", description: item.description ?? "No description yet.",
date: item.date, date: new Date(item.publishedAt)
}); });
}); });
return new Response(feed.xml(), { return new Response(feed.rss2(), {
headers: { headers: {
"content-type": "application/xml", "content-type": "application/xml"
}, }
}); });
} }

View File

@ -27,6 +27,7 @@
"@sanity/ui": "^2.0", "@sanity/ui": "^2.0",
"@sanity/vision": "3", "@sanity/vision": "3",
"@types/mdx": "^2.0.11", "@types/mdx": "^2.0.11",
"feed": "^4.2.2",
"gray-matter": "^4.0.3", "gray-matter": "^4.0.3",
"next": "^14.1", "next": "^14.1",
"next-sanity": "7.1.4", "next-sanity": "7.1.4",
@ -34,7 +35,6 @@
"react": "^18.2", "react": "^18.2",
"react-dom": "^18", "react-dom": "^18",
"react-markdown": "^9.0.1", "react-markdown": "^9.0.1",
"rss": "^1.2.2",
"sanity": "^3.25", "sanity": "^3.25",
"styled-components": "^6.0" "styled-components": "^6.0"
}, },