Skip to content

Commit 7d8fbde

Browse files
committed
add rss handler
1 parent d8fa5d3 commit 7d8fbde

File tree

4 files changed

+70
-94
lines changed

4 files changed

+70
-94
lines changed

scripts/generateRss.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/app/[[...markdownPath]]/page.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import sidebarReference from '../../sidebarReference.json';
77
import sidebarCommunity from '../../sidebarCommunity.json';
88
import sidebarBlog from '../../sidebarBlog.json';
99
import {generateMDX} from '../../utils/generateMDX';
10-
import {generateRssFeed} from '../../utils/rss';
10+
1111
import {RouteItem} from 'components/Layout/getRouteMeta';
1212

1313
function getActiveSection(pathname: string) {
@@ -56,11 +56,6 @@ async function getPageContent(markdownPath: any[]) {
5656
mdx = await fs.readFile(path.join(rootDir, mdxPath, 'index.md'), 'utf8');
5757
}
5858

59-
// Generate RSS feed during build time
60-
if (process.env.NODE_ENV === 'production') {
61-
await generateRssFeed();
62-
}
63-
6459
return await generateMDX(mdx, mdxPath, {});
6560
}
6661

src/app/rss.xml/route.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import Feed from 'rss';
2+
import fs from 'fs';
3+
import path from 'path';
4+
import matter from 'gray-matter';
5+
6+
const getAllFiles = (dirPath, arrayOfFiles = []) => {
7+
const files = fs.readdirSync(dirPath);
8+
9+
files.forEach((file) => {
10+
const filePath = path.join(dirPath, file);
11+
if (fs.statSync(filePath).isDirectory()) {
12+
arrayOfFiles = getAllFiles(filePath, arrayOfFiles);
13+
} else {
14+
arrayOfFiles.push(filePath);
15+
}
16+
});
17+
18+
return arrayOfFiles;
19+
};
20+
21+
export async function GET() {
22+
const feed = new Feed({
23+
title: 'React Blog',
24+
description:
25+
'This blog is the official source for the updates from the React team. Anything important, including release notes or deprecation notices, will be posted here first.',
26+
feed_url: 'https://react.dev/rss.xml',
27+
site_url: 'https://react.dev/',
28+
language: 'en',
29+
favicon: 'https://react.dev/favicon.ico',
30+
pubDate: new Date(),
31+
generator: 'react.dev rss module',
32+
});
33+
34+
const dirPath = path.join(process.cwd(), 'src/content/blog');
35+
const filesByOldest = getAllFiles(dirPath);
36+
const files = filesByOldest.reverse();
37+
38+
for (const filePath of files) {
39+
const id = path.basename(filePath);
40+
if (id !== 'index.md') {
41+
const content = fs.readFileSync(filePath, 'utf-8');
42+
const {data} = matter(content);
43+
const slug = filePath.split('/').slice(-4).join('/').replace('.md', '');
44+
45+
if (!data.title || !data.author || !data.date || !data.description) {
46+
throw new Error(
47+
`${id}: Blog posts must include title, author, date, and description in metadata.`
48+
);
49+
}
50+
51+
feed.item({
52+
id,
53+
title: data.title,
54+
author: data.author,
55+
date: new Date(data.date),
56+
url: `https://react.dev/blog/${slug}`,
57+
description: data.description,
58+
});
59+
}
60+
}
61+
62+
return new Response(feed.xml({indent: true}), {
63+
headers: {
64+
'Content-Type': 'application/rss+xml',
65+
},
66+
});
67+
}
68+
69+
export const dynamic = 'force-static';

src/utils/rss.js

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)