From 57243bc4b4d28e80e42ec70e3656e5a94c8407e2 Mon Sep 17 00:00:00 2001 From: Taqib Date: Wed, 23 Jul 2025 15:11:10 +0100 Subject: feat: add an rss feed --- apps/web/src/app/rss.xml/route.ts | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 apps/web/src/app/rss.xml/route.ts (limited to 'apps/web/src/app/rss.xml') diff --git a/apps/web/src/app/rss.xml/route.ts b/apps/web/src/app/rss.xml/route.ts new file mode 100644 index 00000000..1567d4a7 --- /dev/null +++ b/apps/web/src/app/rss.xml/route.ts @@ -0,0 +1,40 @@ +import { Feed } from 'feed'; +import { getPosts } from '@/lib/blog-query'; +import { SITE_INFO } from '../metadata'; + +export async function GET() { + const { posts } = await getPosts(); + + const feed = new Feed({ + title: 'OpenCut Blog', + description: SITE_INFO.description, + id: `${SITE_INFO.url}`, + link: `${SITE_INFO.url}/blog/`, + language: 'en', + image: `${SITE_INFO.openGraphImage}`, + favicon: `${SITE_INFO.favicon}`, + copyright: `All rights reserved ${new Date().getFullYear()}, ${ + SITE_INFO.title + }`, + }); + + posts.forEach((article) => { + feed.addItem({ + title: article.title, + id: `${SITE_INFO.url}/blog/${article.slug}`, + link: `${SITE_INFO.url}/blog/${article.slug}`, + description: article.description, + author: article.authors.map((author) => ({ + name: author.name ?? 'OpenCut', + })), + date: new Date(article.publishedAt), + image: article.coverImage, + }); + }); + + return new Response(feed.rss2(), { + headers: { + 'Content-Type': 'text/xml', + }, + }); +} \ No newline at end of file -- cgit v1.3.1 From 184dcbabefa357b12c2cce3e6c4ab87a975a33c6 Mon Sep 17 00:00:00 2001 From: Taqib Ibrahim Date: Wed, 23 Jul 2025 15:45:07 +0100 Subject: Update apps/web/src/app/rss.xml/route.ts Co-authored-by: Dominik K. --- apps/web/src/app/rss.xml/route.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'apps/web/src/app/rss.xml') diff --git a/apps/web/src/app/rss.xml/route.ts b/apps/web/src/app/rss.xml/route.ts index 1567d4a7..8602a03f 100644 --- a/apps/web/src/app/rss.xml/route.ts +++ b/apps/web/src/app/rss.xml/route.ts @@ -18,17 +18,17 @@ export async function GET() { }`, }); - posts.forEach((article) => { + for (const post of posts) { feed.addItem({ - title: article.title, - id: `${SITE_INFO.url}/blog/${article.slug}`, - link: `${SITE_INFO.url}/blog/${article.slug}`, - description: article.description, - author: article.authors.map((author) => ({ - name: author.name ?? 'OpenCut', + title: post.title, + id: `${SITE_INFO.url}/blog/${post.slug}`, + link: `${SITE_INFO.url}/blog/${post.slug}`, + description: post.description, + author: post.authors.map((author) => ({ + name: post.name ?? 'OpenCut', })), - date: new Date(article.publishedAt), - image: article.coverImage, + date: new Date(post.publishedAt), + image: post.coverImage, }); }); -- cgit v1.3.1 From f39f46788f77e56efb8e8f53983519ece993de74 Mon Sep 17 00:00:00 2001 From: Taqib Date: Wed, 23 Jul 2025 15:59:47 +0100 Subject: fix: add error handling --- apps/web/src/app/rss.xml/route.ts | 68 +++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 31 deletions(-) (limited to 'apps/web/src/app/rss.xml') diff --git a/apps/web/src/app/rss.xml/route.ts b/apps/web/src/app/rss.xml/route.ts index 8602a03f..b16bd856 100644 --- a/apps/web/src/app/rss.xml/route.ts +++ b/apps/web/src/app/rss.xml/route.ts @@ -3,38 +3,44 @@ import { getPosts } from '@/lib/blog-query'; import { SITE_INFO } from '../metadata'; export async function GET() { - const { posts } = await getPosts(); + try { + const { posts } = await getPosts(); + + const feed = new Feed({ + title: 'OpenCut Blog', + description: SITE_INFO.description, + id: `${SITE_INFO.url}`, + link: `${SITE_INFO.url}/blog/`, + language: 'en', + image: `${SITE_INFO.openGraphImage}`, + favicon: `${SITE_INFO.favicon}`, + copyright: `All rights reserved ${new Date().getFullYear()}, ${ + SITE_INFO.title + }`, + }); - const feed = new Feed({ - title: 'OpenCut Blog', - description: SITE_INFO.description, - id: `${SITE_INFO.url}`, - link: `${SITE_INFO.url}/blog/`, - language: 'en', - image: `${SITE_INFO.openGraphImage}`, - favicon: `${SITE_INFO.favicon}`, - copyright: `All rights reserved ${new Date().getFullYear()}, ${ - SITE_INFO.title - }`, - }); + for (const post of posts) { + feed.addItem({ + title: post.title, + id: `${SITE_INFO.url}/blog/${post.slug}`, + link: `${SITE_INFO.url}/blog/${post.slug}`, + description: post.description, + author: post.authors.map((author) => ({ + name: author.name, + })), + date: new Date(post.publishedAt), + image: post.coverImage || SITE_INFO.openGraphImage, + }); + } - for (const post of posts) { - feed.addItem({ - title: post.title, - id: `${SITE_INFO.url}/blog/${post.slug}`, - link: `${SITE_INFO.url}/blog/${post.slug}`, - description: post.description, - author: post.authors.map((author) => ({ - name: post.name ?? 'OpenCut', - })), - date: new Date(post.publishedAt), - image: post.coverImage, + return new Response(feed.rss2(), { + headers: { + 'Content-Type': 'text/xml', + 'Cache-Control': 'public, max-age=86400, stale-while-revalidate', + }, }); - }); - - return new Response(feed.rss2(), { - headers: { - 'Content-Type': 'text/xml', - }, - }); + } catch (error) { + console.error('Error generating RSS feed', error); + return new Response('Internal Server Error', { status: 500 }); + } } \ No newline at end of file -- cgit v1.3.1