Skip to content

Commit 6e1d62f

Browse files
authored
[RSS] Fix failure when globbing index route (#4701)
* fix: [rss] throw on undefined urls only * test: "" url, passing glob outside pages/ * chore: changeset
1 parent d614be4 commit 6e1d62f

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

.changeset/blue-seals-warn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/rss': patch
3+
---
4+
5+
Fix globs for homepage route

packages/astro-rss/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function mapGlobResult(items: GlobResult): Promise<RSSFeedItem[]> {
5656
return Promise.all(
5757
Object.values(items).map(async (getInfo) => {
5858
const { url, frontmatter } = await getInfo();
59-
if (!Boolean(url)) {
59+
if (url === undefined || url === null) {
6060
throw new Error(
6161
`[RSS] When passing an import.meta.glob result directly, you can only glob ".md" files within /pages! Consider mapping the result to an array of RSSFeedItems. See the RSS docs for usage examples: https://docs.astro.build/en/guides/rss/#2-list-of-rss-feed-objects`
6262
);

packages/astro-rss/test/rss.test.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const phpFeedItem = {
1717
};
1818

1919
const web1FeedItem = {
20-
link: '/web1',
20+
// Should support empty string as a URL (possible for homepage route)
21+
link: '',
2122
title: 'Web 1.0',
2223
pubDate: '1997-05-03',
2324
description:
@@ -26,7 +27,7 @@ const web1FeedItem = {
2627

2728
// note: I spent 30 minutes looking for a nice node-based snapshot tool
2829
// ...and I gave up. Enjoy big strings!
29-
const validXmlResult = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title><![CDATA[My RSS feed]]></title><description><![CDATA[This sure is a nice RSS feed]]></description><link>https://example.com/</link><item><title><![CDATA[Remember PHP?]]></title><link>https://example.com/php/</link><guid>https://example.com/php/</guid><description><![CDATA[PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994.]]></description><pubDate>Tue, 03 May 1994 00:00:00 GMT</pubDate></item><item><title><![CDATA[Web 1.0]]></title><link>https://example.com/web1/</link><guid>https://example.com/web1/</guid><description><![CDATA[Web 1.0 is the term used for the earliest version of the Internet as it emerged from its origins with Defense Advanced Research Projects Agency (DARPA) and became, for the first time, a global network representing the future of digital communications.]]></description><pubDate>Sat, 03 May 1997 00:00:00 GMT</pubDate></item></channel></rss>`;
30+
const validXmlResult = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title><![CDATA[My RSS feed]]></title><description><![CDATA[This sure is a nice RSS feed]]></description><link>https://example.com/</link><item><title><![CDATA[Remember PHP?]]></title><link>https://example.com/php/</link><guid>https://example.com/php/</guid><description><![CDATA[PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994.]]></description><pubDate>Tue, 03 May 1994 00:00:00 GMT</pubDate></item><item><title><![CDATA[Web 1.0]]></title><link>https://example.com/</link><guid>https://example.com/</guid><description><![CDATA[Web 1.0 is the term used for the earliest version of the Internet as it emerged from its origins with Defense Advanced Research Projects Agency (DARPA) and became, for the first time, a global network representing the future of digital communications.]]></description><pubDate>Sat, 03 May 1997 00:00:00 GMT</pubDate></item></channel></rss>`;
3031

3132
describe('rss', () => {
3233
it('should generate on valid RSSFeedItem array', async () => {
@@ -160,5 +161,45 @@ describe('rss', () => {
160161
chai.expect(err.message).to.contain('Required field [link] is missing');
161162
}
162163
});
164+
165+
it('should provide a good error message when passing glob result form outside pages/', async () => {
166+
const globResult = {
167+
'./posts/php.md': () =>
168+
new Promise((resolve) =>
169+
resolve({
170+
// "undefined" when outside pages/
171+
url: undefined,
172+
frontmatter: {
173+
title: phpFeedItem.title,
174+
pubDate: phpFeedItem.pubDate,
175+
description: phpFeedItem.description,
176+
},
177+
})
178+
),
179+
'./posts/nested/web1.md': () =>
180+
new Promise((resolve) =>
181+
resolve({
182+
url: undefined,
183+
frontmatter: {
184+
title: web1FeedItem.title,
185+
pubDate: web1FeedItem.pubDate,
186+
description: web1FeedItem.description,
187+
},
188+
})
189+
),
190+
};
191+
192+
try {
193+
await rss({
194+
title: 'Your Website Title',
195+
description: 'Your Website Description',
196+
site: 'https://astro-demo',
197+
items: globResult,
198+
});
199+
chai.expect(false).to.equal(true, 'Should have errored');
200+
} catch (err) {
201+
chai.expect(err.message).to.contain('you can only glob ".md" files within /pages');
202+
}
203+
});
163204
});
164205
});

0 commit comments

Comments
 (0)