Skip to content

Commit

Permalink
js: added rss feed
Browse files Browse the repository at this point in the history
  • Loading branch information
gorenburg committed Dec 28, 2024
1 parent 46c5668 commit 9f56349
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.jpg" />
<link rel="stylesheet" href="%sveltekit.assets%/styles.css" />
<link rel="alternate" type="application/atom+xml" href="/rss.xml" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/requests/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function getPosts(params?: GetPostsParams): Promise<GetPostsRespons
if (file && typeof file === 'object' && 'metadata' in file && slug) {
const metadata = file.metadata as Omit<Post, 'slug'>
const post = { ...metadata, slug } satisfies Post
post.date = new Date(post.date).toString()
post.date = new Date(post.date).toUTCString()
posts.push(post)
}
}
Expand Down
39 changes: 39 additions & 0 deletions src/routes/rss.xml/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as config from '$lib/config'
import { getPosts } from "$lib/requests/posts"
import { error } from '@sveltejs/kit'

export const prerender = true

export async function GET({ fetch }) {
try {
const posts = await getPosts({ page: 1 })
const body = `
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>${config.title}</title>
<description>${config.description}</description>
<link>${config.url}</link>
<atom:link href="${config.url}/rss.xml" rel="self" type="application/rss+xml"/>
${posts.items
.map(
(post) => `
<item>
<title>${post.title}</title>
<description>${post.description}</description>
<link>${config.url}/${post.slug}</link>
<guid isPermaLink="true">${config.url}/${post.slug}</guid>
<pubDate>${new Date(post.date).toUTCString()}</pubDate>
</item>
`
)
.join('')}
</channel>
</rss>
`.trim()
const response = new Response(body)
response.headers.set('Content-Type', 'application/xml')
return response
} catch (e) {
error(404, `Could not find sitemap.xml`)
}
}

0 comments on commit 9f56349

Please sign in to comment.