Skip to content

Commit 0efaf11

Browse files
authored
Fix: make RSS canonicalUrl required (#3301)
* chore: make canonicalUrl required * docs: explain env variable on required canonicalUrl * refactor: rename "canonicalUrl" to "site" * chore: changeset
1 parent 9b98633 commit 0efaf11

4 files changed

Lines changed: 29 additions & 37 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/rss': minor
3+
---
4+
5+
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

packages/astro-rss/README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import rss from '@astrojs/rss';
2828
export const get = () => rss({
2929
title: 'Buzz’s Blog',
3030
description: 'A humble Astronaut’s guide to the stars',
31+
// pull in the "site" from your project's astro.config
32+
site: import.meta.env.SITE,
3133
items: import.meta.glob('./blog/**/*.md'),
3234
});
3335
```
@@ -44,6 +46,8 @@ rss({
4446
title: 'Buzz’s Blog',
4547
// `<description>` field in output xml
4648
description: 'A humble Astronaut’s guide to the stars',
49+
// provide a base URL for RSS <item> links
50+
site: import.meta.env.SITE,
4751
// list of `<item>`s in output xml
4852
items: import.meta.glob('./**/*.md'),
4953
// (optional) absolute path to XSL stylesheet in your project
@@ -52,9 +56,6 @@ rss({
5256
customData: '<language>en-us</language>',
5357
// (optional) add arbitrary metadata to opening <rss> tag
5458
xmlns: { h: 'http://www.w3.org/TR/html4/' },
55-
// (optional) provide a canonical URL
56-
// defaults to the "site" configured in your project's astro.config
57-
canonicalUrl: 'https://stargazers.club',
5859
});
5960
```
6061

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

7172
The `<description>` attribute of your RSS feed's output xml.
7273

74+
### site
75+
76+
Type: `string (required)`
77+
78+
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.
79+
7380
### items
7481

7582
Type: `RSSFeedItem[] | GlobResult (required)`
@@ -135,11 +142,7 @@ Will inject the following XML:
135142
<rss xmlns:h="http://www.w3.org/TR/html4/"...
136143
```
137144

138-
### canonicalUrl
139-
140-
Type: `string (optional)`
141-
142-
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.
145+
---
143146

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

packages/astro-rss/src/index.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ type RSSOptions = {
88
title: string;
99
/** (required) Description of the RSS Feed */
1010
description: string;
11+
/**
12+
* Specify the base URL to use for RSS feed links.
13+
* We recommend "import.meta.env.SITE" to pull in the "site"
14+
* from your project's astro.config.
15+
*/
16+
site: string;
1117
/**
1218
* List of RSS feed items to render. Accepts either:
1319
* a) list of RSSFeedItems
@@ -22,11 +28,6 @@ type RSSOptions = {
2228
stylesheet?: string | boolean;
2329
/** Specify custom data in opening of file */
2430
customData?: string;
25-
/**
26-
* Specify the base URL to use for RSS feed links.
27-
* Defaults to "site" in your project's astro.config
28-
*/
29-
canonicalUrl?: string;
3031
};
3132

3233
type RSSFeedItem = {
@@ -43,7 +44,6 @@ type RSSFeedItem = {
4344
};
4445

4546
type GenerateRSSArgs = {
46-
site: string;
4747
rssOptions: RSSOptions;
4848
items: RSSFeedItem[];
4949
};
@@ -76,27 +76,21 @@ function mapGlobResult(items: GlobResult): Promise<RSSFeedItem[]> {
7676
}
7777

7878
export default async function getRSS(rssOptions: RSSOptions) {
79-
const site = rssOptions.canonicalUrl ?? (import.meta as any).env.SITE;
80-
if (!site) {
81-
throw new Error(
82-
`RSS requires a canonical URL. Either add a "site" to your project's astro.config, or supply the canonicalUrl argument.`
83-
);
84-
}
8579
let { items } = rssOptions;
8680
if (isGlobResult(items)) {
8781
items = await mapGlobResult(items);
8882
}
8983
return {
9084
body: await generateRSS({
91-
site,
9285
rssOptions,
9386
items,
9487
}),
9588
};
9689
}
9790

9891
/** Generate RSS 2.0 feed */
99-
export async function generateRSS({ site, rssOptions, items }: GenerateRSSArgs): Promise<string> {
92+
export async function generateRSS({ rssOptions, items }: GenerateRSSArgs): Promise<string> {
93+
const { site } = rssOptions;
10094
let xml = `<?xml version="1.0" encoding="UTF-8"?>`;
10195
if (typeof rssOptions.stylesheet === 'string') {
10296
xml += `<?xml-stylesheet href="${rssOptions.stylesheet}" type="text/xsl"?>`;

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ chai.use(chaiPromises);
66

77
const title = 'My RSS feed';
88
const description = 'This sure is a nice RSS feed';
9-
const canonicalUrl = 'https://example.com';
9+
const site = 'https://example.com';
1010

1111
const phpFeedItem = {
1212
link: '/php',
@@ -29,22 +29,12 @@ const web1FeedItem = {
2929
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>`;
3030

3131
describe('rss', () => {
32-
it('should fail on missing "site" and/or "canonicalUrl"', () => {
33-
return chai.expect(
34-
rss({
35-
title,
36-
description,
37-
items: [],
38-
})
39-
).to.be.rejected;
40-
});
41-
4232
it('should generate on valid RSSFeedItem array', async () => {
4333
const { body } = await rss({
4434
title,
4535
description,
4636
items: [phpFeedItem, web1FeedItem],
47-
canonicalUrl,
37+
site,
4838
});
4939

5040
chai.expect(body).to.equal(validXmlResult);
@@ -81,7 +71,7 @@ describe('rss', () => {
8171
title,
8272
description,
8373
items: globResult,
84-
canonicalUrl,
74+
site,
8575
});
8676

8777
chai.expect(body).to.equal(validXmlResult);
@@ -105,7 +95,7 @@ describe('rss', () => {
10595
title,
10696
description,
10797
items: globResult,
108-
canonicalUrl,
98+
site,
10999
})
110100
).to.be.rejected;
111101
});
@@ -128,7 +118,7 @@ describe('rss', () => {
128118
title,
129119
description,
130120
items: globResult,
131-
canonicalUrl,
121+
site,
132122
})
133123
).to.be.rejected;
134124
});

0 commit comments

Comments
 (0)