Skip to content

Commit 777124a

Browse files
fix: update blog routing to use post IDs instead of slugs; simplify RSS feed generation
1 parent 3671ac5 commit 777124a

File tree

4 files changed

+20
-46
lines changed

4 files changed

+20
-46
lines changed

astro.config.mjs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { defineConfig } from 'astro/config';
22
import mdx from '@astrojs/mdx';
3-
43
import sitemap from '@astrojs/sitemap';
54

65
// https://astro.build/config
76
export default defineConfig({
87
site: 'https://nicholasdbrady.github.io',
98
base: '/cookbook/',
10-
trailingSlash: 'always',
119
integrations: [mdx(), sitemap()]
1210
});

src/pages/blog/[...slug].astro

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
---
22
import { type CollectionEntry, getCollection } from 'astro:content';
33
import BlogPost from '../../layouts/BlogPost.astro';
4+
//import { render } from 'astro:content';
45
56
export async function getStaticPaths() {
67
const posts = await getCollection('blog');
78
return posts.map((post) => ({
8-
params: { slug: post.slug },
9+
params: { slug: post.id },
910
props: post,
1011
}));
1112
}
1213
type Props = CollectionEntry<'blog'>;
1314
1415
const post = Astro.props;
15-
const { Content } = await post.render();
16+
const { Content } = await render(post);
1617
---
1718

1819
<BlogPost {...post.data}>
1920
<Content />
20-
</BlogPost>
21+
</BlogPost>

src/pages/blog/index.astro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getCollection } from 'astro:content';
77
import FormattedDate from '../../components/FormattedDate.astro';
88
99
const posts = (await getCollection('blog')).sort(
10-
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf()
10+
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf(),
1111
);
1212
---
1313

@@ -93,7 +93,7 @@ const posts = (await getCollection('blog')).sort(
9393
{
9494
posts.map((post) => (
9595
<li>
96-
<a href={`/cookbook/blog/${post.slug}/`}>
96+
<a href={`/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">
@@ -108,4 +108,4 @@ const posts = (await getCollection('blog')).sort(
108108
</main>
109109
<Footer />
110110
</body>
111-
</html>
111+
</html>

src/pages/rss.xml.js

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,16 @@
1-
import rss, { pagesGlobToRssItems } from '@astrojs/rss';
1+
import rss from '@astrojs/rss';
2+
import { getCollection } from 'astro:content';
23
import { SITE_TITLE, SITE_DESCRIPTION } from '../consts';
34

45
export async function GET(context) {
5-
// Ensure context.site is a string and remove any trailing slash.
6-
const rawSite = typeof context.site === 'string' ? context.site : context.site.href;
7-
const trimmedSite = rawSite.replace(/\/$/, '');
8-
const basePath = '/cookbook/';
9-
// Construct the full site URL: "https://nicholasdbrady.github.io/cookbook/"
10-
const fullSiteUrl = trimmedSite + basePath;
11-
12-
// Use a glob pattern array to include only blog pages and exclude the dynamic route file.
13-
let items = await pagesGlobToRssItems(
14-
import.meta.glob([
15-
'./blog/*.{astro,md,mdx}',
16-
'!./blog/[...slug].astro'
17-
])
18-
);
19-
20-
// Post-process each item so that its link and guid are built correctly.
21-
items = items.map(item => {
22-
// Remove a leading slash from the item.link if present.
23-
const relativePath = item.link.startsWith('/') ? item.link.substring(1) : item.link;
24-
// Construct the fixed link using the fullSiteUrl as the base.
25-
const fixedLink = new URL(relativePath, fullSiteUrl).href;
26-
return {
27-
...item,
28-
link: fixedLink,
29-
guid: fixedLink,
30-
};
31-
});
32-
33-
return rss({
34-
title: SITE_TITLE,
35-
description: SITE_DESCRIPTION,
36-
site: fullSiteUrl, // Ensures the channel <link> includes the "/cookbook/" base.
37-
items,
38-
trailingSlash: true, // Matches your astro.config.mjs trailingSlash: "always"
39-
customData: `<language>en-us</language>`,
40-
});
41-
}
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+
}

0 commit comments

Comments
 (0)