Skip to content

feat: Render full posts on RSS feed #934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions apps/svelte.dev/src/routes/blog/rss.xml/+server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { index } from '$lib/server/content';
import { render_content } from '$lib/server/renderer';

export const prerender = false; // TODO

Expand All @@ -25,8 +26,24 @@ function escapeHTML(html) {
}

/** @param {import('@sveltejs/site-kit').Document[]} posts */
const get_rss = (posts) =>
const get_rss = async (posts) => {
const renderedPosts = await Promise.all(
posts
.filter((post) => !post.metadata.draft)
.map(
async (post) => `
<item>
<title>${escapeHTML(post.metadata.title)}</title>
<link>https://svelte.dev/${post.slug}</link>
<author>${escapeHTML(post.metadata.author)}</author>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added author info too

<description>${escapeHTML(await render_content(post.file, post.body))}</description>
<pubDate>${formatPubdate(/** @type {string} */ (post.file.split('/').pop()).slice(0, 10))}</pubDate>
</item>
`
)
);

return `
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">

Expand All @@ -39,29 +56,18 @@ const get_rss = (posts) =>
<title>Svelte</title>
<link>https://svelte.dev/blog</link>
</image>
${posts
.filter((post) => !post.metadata.draft)
.map(
(post) => `
<item>
<title>${escapeHTML(post.metadata.title)}</title>
<link>https://svelte.dev/${post.slug}</link>
<description>${escapeHTML(post.metadata.description)}</description>
<pubDate>${formatPubdate(/** @type {string} */ (post.file.split('/').pop()).slice(0, 10))}</pubDate>
</item>
`
)
.join('')}
${renderedPosts.join('')}
</channel>

</rss>
`
.replace(/>[^\S]+/gm, '>')
.replace(/[^\S]+</gm, '<')
.trim();
};

export async function GET() {
return new Response(get_rss(index.blog.children), {
return new Response(await get_rss(index.blog.children), {
headers: {
'Cache-Control': `max-age=${30 * 60 * 1e3}`,
'Content-Type': 'application/rss+xml'
Expand Down
Loading