Skip to content

Commit a1ce36e

Browse files
fix: update RSS feed generation to use full site URL and adjust blog post links with base path
1 parent 6c76395 commit a1ce36e

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

src/pages/blog/index.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const posts = (await getCollection('blog')).sort(
9393
{
9494
posts.map((post) => (
9595
<li>
96-
<a href={`/blog/${post.id}/`}>
96+
<a href={`/cookbook/blog/${post.id}/`}>
9797
<img width={720} height={360} src={post.data.heroImage} alt="" />
9898
<h4 class="title">{post.data.title}</h4>
9999
<p class="date">

src/pages/rss.xml.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
1-
import rss from '@astrojs/rss';
1+
import rss, { pagesGlobToRssItems } from '@astrojs/rss';
22
import { getCollection } from 'astro:content';
33
import { SITE_TITLE, SITE_DESCRIPTION } from '../consts';
44

55
export async function GET(context) {
6-
const posts = await getCollection('blog');
7-
return rss({
8-
title: SITE_TITLE,
9-
description: SITE_DESCRIPTION,
10-
site: context.site,
11-
items: posts.map((post) => ({
12-
...post.data,
13-
link: `/blog/${post.id}/`,
14-
})),
15-
});
16-
}
6+
// Get the raw site URL and remove a trailing slash, if any.
7+
const rawSite =
8+
typeof context.site === 'string' ? context.site : context.site.href;
9+
const trimmedSite = rawSite.replace(/\/$/, '');
10+
11+
// Append your base from astro.config.mjs.
12+
const basePath = '/cookbook/';
13+
const fullSiteUrl = trimmedSite + basePath; // e.g. "https://nicholasdbrady.github.io/cookbook/"
14+
15+
// Fetch your blog posts from your content collection.
16+
const posts = await getCollection('blog', ({ data }) => !data.draft);
17+
18+
// Process each post to create an RSS item.
19+
const items = await Promise.all(
20+
posts.map(async (post) => {
21+
const { Content } = await post.render();
22+
// Use fullSiteUrl to resolve heroImage.
23+
const heroImageHTML = post.data.heroImage
24+
? `<p><img src="${new URL(post.data.heroImage, fullSiteUrl).href}" alt="${post.data.title} Hero Image" /></p>`
25+
: '';
26+
return {
27+
title: post.data.title,
28+
// Use fullSiteUrl to construct the item link.
29+
link: new URL(`blog/${post.slug}/`, fullSiteUrl).href,
30+
pubDate: post.data.pubDate,
31+
description: post.data.description,
32+
content: heroImageHTML + post.body,
33+
categories: post.data.tags || [],
34+
author: post.data.author || undefined,
35+
};
36+
})
37+
);
38+
39+
return rss({
40+
title: SITE_TITLE,
41+
description: SITE_DESCRIPTION,
42+
site: fullSiteUrl, // This ensures the channel <link> is "https://nicholasdbrady.github.io/cookbook/"
43+
items,
44+
trailingSlash: true, // To match your astro.config.mjs trailingSlash: "always"
45+
customData: `<language>en-us</language>`,
46+
});
47+
}

0 commit comments

Comments
 (0)