Skip to content

Commit

Permalink
Fix: make RSS canonicalUrl required (#3301)
Browse files Browse the repository at this point in the history
* chore: make canonicalUrl required

* docs: explain env variable on required canonicalUrl

* refactor: rename "canonicalUrl" to "site"

* chore: changeset
  • Loading branch information
bholmesdev authored May 5, 2022
1 parent 9b98633 commit 0efaf11
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-numbers-destroy.md
Original file line number Diff line number Diff line change
@@ -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
19 changes: 11 additions & 8 deletions packages/astro-rss/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
});
```
Expand All @@ -44,6 +46,8 @@ rss({
title: 'Buzz’s Blog',
// `<description>` field in output xml
description: 'A humble Astronaut’s guide to the stars',
// provide a base URL for RSS <item> links
site: import.meta.env.SITE,
// list of `<item>`s in output xml
items: import.meta.glob('./**/*.md'),
// (optional) absolute path to XSL stylesheet in your project
Expand All @@ -52,9 +56,6 @@ rss({
customData: '<language>en-us</language>',
// (optional) add arbitrary metadata to opening <rss> 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',
});
```

Expand All @@ -70,6 +71,12 @@ Type: `string (required)`

The `<description>` 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)`
Expand Down Expand Up @@ -135,11 +142,7 @@ Will inject the following XML:
<rss xmlns:h="http://www.w3.org/TR/html4/"...
```

### canonicalUrl

Type: `string (optional)`

The base URL to use when generating RSS item links. This defaults to the [`site` configured in your project's `astro.config`](https://docs.astro.build/en/reference/configuration-reference/#site). We recommend using `site` instead of `canonicalUrl`, though we provide this option if an override is necessary.
---

For more on building with Astro, [visit the Astro docs][astro-rss].

Expand Down
22 changes: 8 additions & 14 deletions packages/astro-rss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
site: string;
/**
* List of RSS feed items to render. Accepts either:
* a) list of RSSFeedItems
Expand All @@ -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 = {
Expand All @@ -43,7 +44,6 @@ type RSSFeedItem = {
};

type GenerateRSSArgs = {
site: string;
rssOptions: RSSOptions;
items: RSSFeedItem[];
};
Expand Down Expand Up @@ -76,27 +76,21 @@ function mapGlobResult(items: GlobResult): Promise<RSSFeedItem[]> {
}

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,
}),
};
}

/** Generate RSS 2.0 feed */
export async function generateRSS({ site, rssOptions, items }: GenerateRSSArgs): Promise<string> {
export async function generateRSS({ rssOptions, items }: GenerateRSSArgs): Promise<string> {
const { site } = rssOptions;
let xml = `<?xml version="1.0" encoding="UTF-8"?>`;
if (typeof rssOptions.stylesheet === 'string') {
xml += `<?xml-stylesheet href="${rssOptions.stylesheet}" type="text/xsl"?>`;
Expand Down
20 changes: 5 additions & 15 deletions packages/astro-rss/test/rss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -29,22 +29,12 @@ const web1FeedItem = {
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>`;

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);
Expand Down Expand Up @@ -81,7 +71,7 @@ describe('rss', () => {
title,
description,
items: globResult,
canonicalUrl,
site,
});

chai.expect(body).to.equal(validXmlResult);
Expand All @@ -105,7 +95,7 @@ describe('rss', () => {
title,
description,
items: globResult,
canonicalUrl,
site,
})
).to.be.rejected;
});
Expand All @@ -128,7 +118,7 @@ describe('rss', () => {
title,
description,
items: globResult,
canonicalUrl,
site,
})
).to.be.rejected;
});
Expand Down

0 comments on commit 0efaf11

Please sign in to comment.