Become a Pro / website · becomeapro.ai / src / app / feed.xml / route.ts
| 1 | // RSS feed of published tutorials, newest first, for feed readers and anything that watches for new posts |
| 2 | import { AUTHOR, SITE_DESCRIPTION, SITE_NAME, SITE_URL } from "@/lib/seo"; |
| 3 | import { getTrack } from "@/lib/tracks"; |
| 4 | import { getPublishedTutorials } from "@/lib/tutorials"; |
| 5 | |
| 6 | export const dynamic = "force-static"; |
| 7 | |
| 8 | const escape = (text: string) => |
| 9 | text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """); |
| 10 | |
| 11 | const rfc822 = (day: string) => new Date(`${day}T12:00:00Z`).toUTCString(); |
| 12 | |
| 13 | export function GET() { |
| 14 | const tutorials = getPublishedTutorials(); |
| 15 | const items = tutorials |
| 16 | .map((t) => { |
| 17 | const url = `${SITE_URL}/${t.track}/${t.slug}`; |
| 18 | return [ |
| 19 | " <item>", |
| 20 | ` <title>${escape(t.title)}</title>`, |
| 21 | ` <link>${url}</link>`, |
| 22 | ` <guid isPermaLink="true">${url}</guid>`, |
| 23 | ` <description>${escape(t.description)}</description>`, |
| 24 | ` <category>${escape(getTrack(t.track)?.name ?? t.track)}</category>`, |
| 25 | ` <dc:creator>${escape(AUTHOR.name)}</dc:creator>`, |
| 26 | t.publishedAt ? ` <pubDate>${rfc822(t.publishedAt)}</pubDate>` : "", |
| 27 | " </item>", |
| 28 | ] |
| 29 | .filter(Boolean) |
| 30 | .join("\n"); |
| 31 | }) |
| 32 | .join("\n"); |
| 33 | |
| 34 | const xml = `<?xml version="1.0" encoding="UTF-8"?> |
| 35 | <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"> |
| 36 | <channel> |
| 37 | <title>${escape(SITE_NAME)}</title> |
| 38 | <link>${SITE_URL}</link> |
| 39 | <description>${escape(SITE_DESCRIPTION)}</description> |
| 40 | <language>en</language> |
| 41 | <atom:link href="${SITE_URL}/feed.xml" rel="self" type="application/rss+xml"/> |
| 42 | ${tutorials[0]?.publishedAt ? ` <lastBuildDate>${rfc822(tutorials[0].publishedAt)}</lastBuildDate>\n` : ""}${items} |
| 43 | </channel> |
| 44 | </rss> |
| 45 | `; |
| 46 | return new Response(xml, { headers: { "Content-Type": "application/rss+xml; charset=utf-8" } }); |
| 47 | } |