diff --git a/.changeset/tasty-numbers-destroy.md b/.changeset/tasty-numbers-destroy.md new file mode 100644 index 000000000000..3c07ed14f98a --- /dev/null +++ b/.changeset/tasty-numbers-destroy.md @@ -0,0 +1,5 @@ +--- +'@astrojs/rss': minor +--- + +Change the optional "canonicalUrl" argument to a required "site" argument. This fixes problems with import.meta.env.SITE. If you want to use your project's "site" field for your RSS feeds, set site: import.meta.env.SITE in the rss function options diff --git a/packages/astro-rss/README.md b/packages/astro-rss/README.md index 4e1edd42c435..931e9200b718 100644 --- a/packages/astro-rss/README.md +++ b/packages/astro-rss/README.md @@ -28,6 +28,8 @@ import rss from '@astrojs/rss'; export const get = () => rss({ title: 'Buzz’s Blog', description: 'A humble Astronaut’s guide to the stars', + // pull in the "site" from your project's astro.config + site: import.meta.env.SITE, items: import.meta.glob('./blog/**/*.md'), }); ``` @@ -44,6 +46,8 @@ rss({ title: 'Buzz’s Blog', // `` field in output xml description: 'A humble Astronaut’s guide to the stars', + // provide a base URL for RSS links + site: import.meta.env.SITE, // list of ``s in output xml items: import.meta.glob('./**/*.md'), // (optional) absolute path to XSL stylesheet in your project @@ -52,9 +56,6 @@ rss({ customData: 'en-us', // (optional) add arbitrary metadata to opening tag xmlns: { h: 'http://www.w3.org/TR/html4/' }, - // (optional) provide a canonical URL - // defaults to the "site" configured in your project's astro.config - canonicalUrl: 'https://stargazers.club', }); ``` @@ -70,6 +71,12 @@ Type: `string (required)` The `` attribute of your RSS feed's output xml. +### site + +Type: `string (required)` + +The base URL to use when generating RSS item links. We recommend using `import.meta.env.SITE` to pull in the "site" from your project's astro.config. Still, feel free to use a custom base URL if necessary. + ### items Type: `RSSFeedItem[] | GlobResult (required)` @@ -135,11 +142,7 @@ Will inject the following XML: { } export default async function getRSS(rssOptions: RSSOptions) { - const site = rssOptions.canonicalUrl ?? (import.meta as any).env.SITE; - if (!site) { - throw new Error( - `RSS requires a canonical URL. Either add a "site" to your project's astro.config, or supply the canonicalUrl argument.` - ); - } let { items } = rssOptions; if (isGlobResult(items)) { items = await mapGlobResult(items); } return { body: await generateRSS({ - site, rssOptions, items, }), @@ -96,7 +89,8 @@ export default async function getRSS(rssOptions: RSSOptions) { } /** Generate RSS 2.0 feed */ -export async function generateRSS({ site, rssOptions, items }: GenerateRSSArgs): Promise { +export async function generateRSS({ rssOptions, items }: GenerateRSSArgs): Promise { + const { site } = rssOptions; let xml = ``; if (typeof rssOptions.stylesheet === 'string') { xml += ``; diff --git a/packages/astro-rss/test/rss.test.js b/packages/astro-rss/test/rss.test.js index 2943709e30fd..9962e83f94fe 100644 --- a/packages/astro-rss/test/rss.test.js +++ b/packages/astro-rss/test/rss.test.js @@ -6,7 +6,7 @@ chai.use(chaiPromises); const title = 'My RSS feed'; const description = 'This sure is a nice RSS feed'; -const canonicalUrl = 'https://example.com'; +const site = 'https://example.com'; const phpFeedItem = { link: '/php', @@ -29,22 +29,12 @@ const web1FeedItem = { 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`; describe('rss', () => { - it('should fail on missing "site" and/or "canonicalUrl"', () => { - return chai.expect( - rss({ - title, - description, - items: [], - }) - ).to.be.rejected; - }); - it('should generate on valid RSSFeedItem array', async () => { const { body } = await rss({ title, description, items: [phpFeedItem, web1FeedItem], - canonicalUrl, + site, }); chai.expect(body).to.equal(validXmlResult); @@ -81,7 +71,7 @@ describe('rss', () => { title, description, items: globResult, - canonicalUrl, + site, }); chai.expect(body).to.equal(validXmlResult); @@ -105,7 +95,7 @@ describe('rss', () => { title, description, items: globResult, - canonicalUrl, + site, }) ).to.be.rejected; }); @@ -128,7 +118,7 @@ describe('rss', () => { title, description, items: globResult, - canonicalUrl, + site, }) ).to.be.rejected; });