From 6e1d62fe222e45b763b2b60b377b07e431950d54 Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Mon, 12 Sep 2022 15:05:57 -0400 Subject: [PATCH] [RSS] Fix failure when globbing index route (#4701) * fix: [rss] throw on undefined urls only * test: "" url, passing glob outside pages/ * chore: changeset --- .changeset/blue-seals-warn.md | 5 ++++ packages/astro-rss/src/index.ts | 2 +- packages/astro-rss/test/rss.test.js | 45 +++++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 .changeset/blue-seals-warn.md diff --git a/.changeset/blue-seals-warn.md b/.changeset/blue-seals-warn.md new file mode 100644 index 000000000000..1204481ee8eb --- /dev/null +++ b/.changeset/blue-seals-warn.md @@ -0,0 +1,5 @@ +--- +'@astrojs/rss': patch +--- + +Fix globs for homepage route diff --git a/packages/astro-rss/src/index.ts b/packages/astro-rss/src/index.ts index 20d509f14deb..feb4d258c642 100644 --- a/packages/astro-rss/src/index.ts +++ b/packages/astro-rss/src/index.ts @@ -56,7 +56,7 @@ function mapGlobResult(items: GlobResult): Promise { return Promise.all( Object.values(items).map(async (getInfo) => { const { url, frontmatter } = await getInfo(); - if (!Boolean(url)) { + if (url === undefined || url === null) { throw new Error( `[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` ); diff --git a/packages/astro-rss/test/rss.test.js b/packages/astro-rss/test/rss.test.js index 08d56fa92660..5c33905a5404 100644 --- a/packages/astro-rss/test/rss.test.js +++ b/packages/astro-rss/test/rss.test.js @@ -17,7 +17,8 @@ const phpFeedItem = { }; const web1FeedItem = { - link: '/web1', + // Should support empty string as a URL (possible for homepage route) + link: '', title: 'Web 1.0', pubDate: '1997-05-03', description: @@ -26,7 +27,7 @@ const web1FeedItem = { // note: I spent 30 minutes looking for a nice node-based snapshot tool // ...and I gave up. Enjoy big strings! -const validXmlResult = `<![CDATA[My RSS feed]]>https://example.com/<![CDATA[Remember PHP?]]>https://example.com/php/https://example.com/php/Tue, 03 May 1994 00:00:00 GMT<![CDATA[Web 1.0]]>https://example.com/web1/https://example.com/web1/Sat, 03 May 1997 00:00:00 GMT`; +const validXmlResult = `<![CDATA[My RSS feed]]>https://example.com/<![CDATA[Remember PHP?]]>https://example.com/php/https://example.com/php/Tue, 03 May 1994 00:00:00 GMT<![CDATA[Web 1.0]]>https://example.com/https://example.com/Sat, 03 May 1997 00:00:00 GMT`; describe('rss', () => { it('should generate on valid RSSFeedItem array', async () => { @@ -160,5 +161,45 @@ describe('rss', () => { chai.expect(err.message).to.contain('Required field [link] is missing'); } }); + + it('should provide a good error message when passing glob result form outside pages/', async () => { + const globResult = { + './posts/php.md': () => + new Promise((resolve) => + resolve({ + // "undefined" when outside pages/ + url: undefined, + frontmatter: { + title: phpFeedItem.title, + pubDate: phpFeedItem.pubDate, + description: phpFeedItem.description, + }, + }) + ), + './posts/nested/web1.md': () => + new Promise((resolve) => + resolve({ + url: undefined, + frontmatter: { + title: web1FeedItem.title, + pubDate: web1FeedItem.pubDate, + description: web1FeedItem.description, + }, + }) + ), + }; + + try { + await rss({ + title: 'Your Website Title', + description: 'Your Website Description', + site: 'https://astro-demo', + items: globResult, + }); + chai.expect(false).to.equal(true, 'Should have errored'); + } catch (err) { + chai.expect(err.message).to.contain('you can only glob ".md" files within /pages'); + } + }); }); });