From e24fdbc55826016571a54c2498eca9388d616905 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 5 May 2022 11:19:53 -0400 Subject: [PATCH 1/4] chore: make canonicalUrl required --- packages/astro-rss/src/index.ts | 26 ++++++++++---------------- packages/astro-rss/test/rss.test.js | 10 ---------- 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/packages/astro-rss/src/index.ts b/packages/astro-rss/src/index.ts index c9e53f8ce55d..80a75d87be08 100644 --- a/packages/astro-rss/src/index.ts +++ b/packages/astro-rss/src/index.ts @@ -8,6 +8,12 @@ type RSSOptions = { title: string; /** (required) Description of the RSS Feed */ description: string; + /** + * Specify the base URL to use for RSS feed links. + * We recommend "import.meta.env.SITE" to pull in the "site" + * from your project's astro.config. + */ + canonicalUrl: string; /** * List of RSS feed items to render. Accepts either: * a) list of RSSFeedItems @@ -22,11 +28,6 @@ type RSSOptions = { stylesheet?: string | boolean; /** Specify custom data in opening of file */ customData?: string; - /** - * Specify the base URL to use for RSS feed links. - * Defaults to "site" in your project's astro.config - */ - canonicalUrl?: string; }; type RSSFeedItem = { @@ -43,7 +44,6 @@ type RSSFeedItem = { }; type GenerateRSSArgs = { - site: string; rssOptions: RSSOptions; items: RSSFeedItem[]; }; @@ -76,19 +76,12 @@ function mapGlobResult(items: GlobResult): Promise { } 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 { canonicalUrl } = rssOptions; let xml = ``; if (typeof rssOptions.stylesheet === 'string') { xml += ``; @@ -115,7 +109,7 @@ export async function generateRSS({ site, rssOptions, items }: GenerateRSSArgs): // title, description, customData xml += `<![CDATA[${rssOptions.title}]]>`; xml += ``; - xml += `${createCanonicalURL(site).href}`; + xml += `${createCanonicalURL(canonicalUrl).href}`; if (typeof rssOptions.customData === 'string') xml += rssOptions.customData; // items for (const result of items) { @@ -124,7 +118,7 @@ export async function generateRSS({ site, rssOptions, items }: GenerateRSSArgs): // If the item's link is already a valid URL, don't mess with it. const itemLink = isValidURL(result.link) ? result.link - : createCanonicalURL(result.link, site).href; + : createCanonicalURL(result.link, canonicalUrl).href; xml += `${itemLink}`; xml += `${itemLink}`; if (result.description) xml += ``; diff --git a/packages/astro-rss/test/rss.test.js b/packages/astro-rss/test/rss.test.js index 2943709e30fd..9227b44ddb63 100644 --- a/packages/astro-rss/test/rss.test.js +++ b/packages/astro-rss/test/rss.test.js @@ -29,16 +29,6 @@ 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, From 4ebccf8c2c3692767334057b38168b3ee0d77131 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 5 May 2022 11:24:27 -0400 Subject: [PATCH 2/4] docs: explain env variable on required canonicalUrl --- packages/astro-rss/README.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/astro-rss/README.md b/packages/astro-rss/README.md index 4e1edd42c435..58120a05a894 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 + canonicalUrl: 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 + canonicalUrl: 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. +### canonicalUrl + +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: Date: Thu, 5 May 2022 17:39:09 -0400 Subject: [PATCH 3/4] refactor: rename "canonicalUrl" to "site" --- packages/astro-rss/README.md | 6 +++--- packages/astro-rss/src/index.ts | 8 ++++---- packages/astro-rss/test/rss.test.js | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/astro-rss/README.md b/packages/astro-rss/README.md index 58120a05a894..931e9200b718 100644 --- a/packages/astro-rss/README.md +++ b/packages/astro-rss/README.md @@ -29,7 +29,7 @@ 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 - canonicalUrl: import.meta.env.SITE, + site: import.meta.env.SITE, items: import.meta.glob('./blog/**/*.md'), }); ``` @@ -47,7 +47,7 @@ rss({ // `` field in output xml description: 'A humble Astronaut’s guide to the stars', // provide a base URL for RSS links - canonicalUrl: import.meta.env.SITE, + 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 @@ -71,7 +71,7 @@ Type: `string (required)` The `` attribute of your RSS feed's output xml. -### canonicalUrl +### site Type: `string (required)` diff --git a/packages/astro-rss/src/index.ts b/packages/astro-rss/src/index.ts index 80a75d87be08..5f7e63b892a6 100644 --- a/packages/astro-rss/src/index.ts +++ b/packages/astro-rss/src/index.ts @@ -13,7 +13,7 @@ type RSSOptions = { * We recommend "import.meta.env.SITE" to pull in the "site" * from your project's astro.config. */ - canonicalUrl: string; + site: string; /** * List of RSS feed items to render. Accepts either: * a) list of RSSFeedItems @@ -90,7 +90,7 @@ export default async function getRSS(rssOptions: RSSOptions) { /** Generate RSS 2.0 feed */ export async function generateRSS({ rssOptions, items }: GenerateRSSArgs): Promise { - const { canonicalUrl } = rssOptions; + const { site } = rssOptions; let xml = ``; if (typeof rssOptions.stylesheet === 'string') { xml += ``; @@ -109,7 +109,7 @@ export async function generateRSS({ rssOptions, items }: GenerateRSSArgs): Promi // title, description, customData xml += `<![CDATA[${rssOptions.title}]]>`; xml += ``; - xml += `${createCanonicalURL(canonicalUrl).href}`; + xml += `${createCanonicalURL(site).href}`; if (typeof rssOptions.customData === 'string') xml += rssOptions.customData; // items for (const result of items) { @@ -118,7 +118,7 @@ export async function generateRSS({ rssOptions, items }: GenerateRSSArgs): Promi // If the item's link is already a valid URL, don't mess with it. const itemLink = isValidURL(result.link) ? result.link - : createCanonicalURL(result.link, canonicalUrl).href; + : createCanonicalURL(result.link, site).href; xml += `${itemLink}`; xml += `${itemLink}`; if (result.description) xml += ``; diff --git a/packages/astro-rss/test/rss.test.js b/packages/astro-rss/test/rss.test.js index 9227b44ddb63..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', @@ -34,7 +34,7 @@ describe('rss', () => { title, description, items: [phpFeedItem, web1FeedItem], - canonicalUrl, + site, }); chai.expect(body).to.equal(validXmlResult); @@ -71,7 +71,7 @@ describe('rss', () => { title, description, items: globResult, - canonicalUrl, + site, }); chai.expect(body).to.equal(validXmlResult); @@ -95,7 +95,7 @@ describe('rss', () => { title, description, items: globResult, - canonicalUrl, + site, }) ).to.be.rejected; }); @@ -118,7 +118,7 @@ describe('rss', () => { title, description, items: globResult, - canonicalUrl, + site, }) ).to.be.rejected; }); From fb3bf9f7e76c032984b6266a8d553a010192bdd0 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 5 May 2022 17:43:52 -0400 Subject: [PATCH 4/4] chore: changeset --- .changeset/tasty-numbers-destroy.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tasty-numbers-destroy.md 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