You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update RSS config for readability and consistency with Astro 2.0.
6
+
7
+
#### Migration - `import.meta.glob()` handling
8
+
9
+
We have deprecated `items: import.meta.glob(...)` handling in favor of a separate `pagesGlobToRssItems()` helper. This simplifies our `items` configuration option to accept a single type, without losing existing functionality.
10
+
11
+
If you rely on our `import.meta.glob()` handling, we suggest adding the `pagesGlobToRssItems()` wrapper to your RSS config:
12
+
13
+
```diff
14
+
// src/pages/rss.xml.js
15
+
import rss, {
16
+
+ pagesGlobToRssItems
17
+
} from '@astrojs/rss';
18
+
19
+
export function get(context) {
20
+
return rss({
21
+
+ items: pagesGlobToRssItems(
22
+
import.meta.glob('./blog/*.{md,mdx}'),
23
+
+ ),
24
+
});
25
+
}
26
+
```
27
+
28
+
#### New `rssSchema` for content collections
29
+
30
+
`@astrojs/rss` now exposes an `rssSchema` for use with content collections. This ensures all RSS feed properties are present in your frontmatter:
The `@astrojs/rss` package provides helpers for generating RSS feeds within [Astro endpoints][astro-endpoints]. This unlocks both static builds _and_ on-demand generation when using an [SSR adapter](https://docs.astro.build/en/guides/server-side-rendering/#enabling-ssr-in-your-project).
21
21
22
-
For instance, say you need to generate an RSS feed for all posts under `src/pages/blog/`. Start by [adding a `site` to your project's `astro.config` for link generation](https://docs.astro.build/en/reference/configuration-reference/#site). Then, create an `rss.xml.js` file under your project's `src/pages/` directory, and use [Vite's `import.meta.glob` helper](https://vitejs.dev/guide/features.html#glob-import) like so:
22
+
For instance, say you need to generate an RSS feed for all posts under `src/content/blog/` using content collections.
23
+
24
+
Start by [adding a `site` to your project's `astro.config` for link generation](https://docs.astro.build/en/reference/configuration-reference/#site). Then, create an `rss.xml.js` file under your project's `src/pages/` directory, and [use `getCollection()`](https://docs.astro.build/en/guides/content-collections/#getcollection) to generate a feed from all documents in the `blog` collection:
23
25
24
26
```js
25
27
// src/pages/rss.xml.js
26
28
importrssfrom'@astrojs/rss';
29
+
import { getCollection } from'astro:content';
27
30
28
-
exportconstget= () =>rss({
31
+
exportasyncfunctionget(context) {
32
+
constposts=awaitgetCollection('blog');
33
+
returnrss({
29
34
title:'Buzz’s Blog',
30
35
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,
33
-
items:import.meta.glob('./blog/**/*.md'),
36
+
// Pull in your project "site" from the endpoint context
@@ -77,13 +93,22 @@ The `<description>` attribute of your RSS feed's output xml.
77
93
78
94
Type: `string (required)`
79
95
80
-
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.
96
+
The base URL to use when generating RSS item links. We recommend using the [endpoint context object](https://docs.astro.build/en/reference/api-reference/#contextsite), which includes the `site` configured in your project's `astro.config.*`:
97
+
98
+
```ts
99
+
importrssfrom'@astrojs/rss';
100
+
101
+
exportconst get = (context) =>rss({
102
+
site: context.site,
103
+
...
104
+
});
105
+
```
81
106
82
107
### items
83
108
84
-
Type: `RSSFeedItem[] | GlobResult (required)`
109
+
Type: `RSSFeedItem[] (required)`
85
110
86
-
Either a list of formatted RSS feed items or the result of [Vite's `import.meta.glob` helper](https://vitejs.dev/guide/features.html#glob-import). See [Astro's RSS items documentation](https://docs.astro.build/en/guides/rss/#generating-items) for usage examples to choose the best option for you.
111
+
A list of formatted RSS feed items. See [Astro's RSS items documentation](https://docs.astro.build/en/guides/rss/#generating-items) for usage examples to choose the best option for you.
87
112
88
113
When providing a formatted RSS item list, see the `RSSFeedItem` type reference below:
89
114
@@ -152,6 +177,62 @@ Will inject the following XML:
The `content` key contains the full content of the post as HTML. This allows you to make your entire post content available to RSS feed readers.
183
+
184
+
**Note:** Whenever you're using HTML content in XML, we suggest using a package like [`sanitize-html`](https://www.npmjs.com/package/sanitize-html) in order to make sure that your content is properly sanitized, escaped, and encoded.
185
+
186
+
[See our RSS documentation](https://docs.astro.build/en/guides/rss/#including-full-post-content) for examples using content collections and glob imports.
187
+
188
+
## `rssSchema`
189
+
190
+
When using content collections, you can configure your collection schema to enforce expected [`RSSFeedItem`](#items) properties. Import and apply `rssSchema` to ensure that each collection entry produces a valid RSS feed item:
191
+
192
+
```ts "schema: rssSchema,"
193
+
import { defineCollection } from 'astro:content';
194
+
import { rssSchema } from '@astrojs/rss';
195
+
196
+
const blog = defineCollection({
197
+
schema: rssSchema,
198
+
});
199
+
200
+
export const collections = { blog };
201
+
```
202
+
203
+
If you have an existing schema, you can merge extra properties using `extends()`:
To create an RSS feed from documents in `src/pages/`, use the `pagesGlobToRssItems()` helper. This accepts an `import.meta.glob` result ([see Vite documentation](https://vitejs.dev/guide/features.html#glob-import)) and outputs an array of valid [`RSSFeedItem`s](#items).
217
+
218
+
This function assumes, but does not verify, you are globbing for items inside `src/pages/`, and all necessary feed properties are present in each document's frontmatter. If you encounter errors, verify each page frontmatter manually.
219
+
220
+
```ts "pagesGlobToRssItems"
221
+
// src/pages/rss.xml.js
222
+
import rss, { pagesGlobToRssItems } from '@astrojs/rss';
223
+
224
+
export async function get(context) {
225
+
return rss({
226
+
title: 'Buzz’s Blog',
227
+
description: 'A humble Astronaut’s guide to the stars',
228
+
site: context.site,
229
+
items: await pagesGlobToRssItems(
230
+
import.meta.glob('./blog/*.{md,mdx}'),
231
+
),
232
+
});
233
+
}
234
+
```
235
+
155
236
---
156
237
157
238
For more on building with Astro, [visit the Astro docs][astro-rss].
0 commit comments