Skip to content

Commit

Permalink
Update feed to use route handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton committed Sep 10, 2023
1 parent dd420ef commit d2ce36c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ out/

# Generated files
.vscode/
public/rss/

# Shiki files have to be copied when deploying to Vercel
lib/shiki/*
Expand Down
12 changes: 12 additions & 0 deletions app/rss/feed.json/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getFeed } from "rss/utils"

export async function GET() {
const feed = await getFeed()
const content = feed.json1()

return new Response(content, {
headers: {
"Content-Type": "application/json",
},
})
}
12 changes: 12 additions & 0 deletions app/rss/feed.xml/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getFeed } from "rss/utils"

export async function GET() {
const feed = await getFeed()
const content = feed.rss2()

return new Response(content, {
headers: {
"Content-Type": "application/rss+xml",
},
})
}
47 changes: 47 additions & 0 deletions app/rss/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Feed } from "feed"
import { cache } from "react"
import { getAllPosts } from "lib/posts"
import { siteMeta } from "lib/siteMeta"

// Revalidate the data at most every hour
export const revalidate = 3600

export const getFeed = cache(async () => {
const posts = await getAllPosts()
const author = { email: siteMeta.email, name: "Mark Skelton" }

const feed = new Feed({
author,
copyright: `All rights reserved ${new Date().getFullYear()}`,
description: siteMeta.tagline,
favicon: `${siteMeta.url}/favicon.ico`,
feedLinks: {
json: `${siteMeta.url}/rss/feed.json`,
rss2: `${siteMeta.url}/rss/feed.xml`,
},
id: siteMeta.url,
image: `${siteMeta.url}/favicon.ico`,
link: siteMeta.url,
title: author.name,
})

for (const post of posts) {
const url = `${siteMeta.url}/blog/${post.slug}`
// const html = ReactDOMServer.renderToStaticMarkup(
// createElement(post.component, { isRssFeed: true })
// )

feed.addItem({
author: [author],
// content: html,
contributor: [author],
date: new Date(post.date),
description: post.description,
id: post.slug,
link: url,
title: post.title,
})
}

return feed
})
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"type": "module",
"scripts": {
"build": "prisma generate && bun shiki && next build",
"build:rss": "bun ./scripts/generateRssFeed.ts",
"db:generate": "prisma generate",
"db:push": "prisma db push",
"db:studio": "prisma studio",
Expand Down

0 comments on commit d2ce36c

Please sign in to comment.