Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: New RSS package! #430

Merged
merged 26 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
586b7ee
feat: add @astrojs/rss package reference
bholmesdev May 3, 2022
2bffa56
edit: reword legacy styling section for clarity
bholmesdev May 3, 2022
257edd1
edit: add opening notes on env variables for skimmer-friendliness
bholmesdev May 3, 2022
d10d9b8
fix: remove VITE_ callout (was incorrect!
bholmesdev May 3, 2022
b80886d
feat: add default env variable list with new SITE variable
bholmesdev May 3, 2022
53cdcda
nit: change "we" to "you" for consistency
bholmesdev May 3, 2022
829a202
fix: make folder paths consistent
bholmesdev May 3, 2022
7924674
edit: remove xmlns example for simplicity. This is documented on README!
bholmesdev May 3, 2022
1e1466a
fix: semicolon
bholmesdev May 3, 2022
8bb1673
edit: reorder `items` to put simpler option first
bholmesdev May 3, 2022
801f67c
nit: add parens
bholmesdev May 3, 2022
209c7c7
edit: add note of "site" requirement
bholmesdev May 3, 2022
2e0a453
add importmetaenv component
bholmesdev May 3, 2022
8c5dd7e
add importmetaenv, again!
bholmesdev May 3, 2022
3945616
add importmetaenv, again again!
bholmesdev May 3, 2022
6794904
add importmetaenv, again again again!
bholmesdev May 3, 2022
ae16552
edit: within src/pages/
bholmesdev May 3, 2022
e1dfdad
Merge branch 'feat/new-rss-package' of github.com:withastro/docs into…
bholmesdev May 3, 2022
aeedd1b
edit: consistent voicing on rss helper
bholmesdev May 3, 2022
9d0d606
importmetaenv, last time
bholmesdev May 3, 2022
987865c
importmetaenv - i lied
bholmesdev May 3, 2022
7a9dc52
nit: say your .md posts
bholmesdev May 3, 2022
e1330e0
Merge branch 'feat/new-rss-package' of github.com:withastro/docs into…
bholmesdev May 3, 2022
bc3e7a3
fix: add deprecation note to getStaticPaths
bholmesdev May 3, 2022
d4a07c2
edit: remove super clever and amazing getStylesheet
bholmesdev May 3, 2022
f5eab9f
Merge branch 'feat/new-rss-package' of github.com:withastro/docs into…
bholmesdev May 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/pages/en/guides/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ setup: |
import ImportMetaEnv from '~/components/ImportMetaEnv.astro';
---

Astro uses Vite for environment variables, and allows you to use any of its methods to get and set environment variables.
Astro uses Vite for environment variables, and allows you to [use any of its methods](https://vitejs.dev/guide/env-and-mode.html) to get and set environment variables.

Note that while _all_ environment variables are available in server-side code, only environment variables prefixed with `PUBLIC_` are available in client-side code for security purposes.

Expand All @@ -17,7 +17,20 @@ SECRET_PASSWORD=password123
PUBLIC_ANYBODY=there
```

In this example, `PUBLIC_ANYBODY` will be available in server or client code, while `SECRET_PASSWORD` will not.
In this example, `PUBLIC_ANYBODY` (accessible via `<ImportMetaEnv path=".PUBLIC_ANYBODY" />`) will be available in server or client code, while `SECRET_PASSWORD` (accessible via `<ImportMetaEnv path=".SECRET_PASSWORD" />`) will be server-side only.

## Default environment Variables

Astro includes a few environment variables out-of-the-box:

- `<ImportMetaEnv path=".MODE" /> ('development' | 'production')`: the mode your site is running in. This is `development` when running `astro dev` and `production` when running `astro build`.

- `<ImportMetaEnv path=".BASE_URL" /> (string)`: the base url your site is being served from. This is determined by the [base config option](/en/reference/configuration-reference/#base).

- `<ImportMetaEnv path=".PROD" /> (boolean)`: whether your site is running in production.

- `<ImportMetaEnv path=".DEV" /> (boolean)`: whether your site is running in development (always the opposite of `<ImportMetaEnv path=".PROD" />`).
- `<ImportMetaEnv path=".SITE" /> (string)`: [The `site` option](/en/reference/configuration-reference/#site) specified in your project's `astro.config`.

## Setting environment variables

Expand Down
124 changes: 114 additions & 10 deletions src/pages/en/guides/rss.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,117 @@ description: An intro to RSS in Astro

Astro supports fast, automatic RSS feed generation for blogs and other content websites. For more information about RSS feeds in general, see [aboutfeeds.com](https://aboutfeeds.com/).

You can create an RSS feed from any Astro page that uses a `getStaticPaths()` function for routing. Only dynamic routes can use `getStaticPaths()` today (see [Routing](/en/core-concepts/routing)).
## Using `@astrojs/rss` (recommended)

> We hope to make this feature available to all other pages before v1.0. As a workaround, you can convert a static route to a dynamic route that only generates a single page. See [Routing](/en/core-concepts/routing) for more information about dynamic routes.
The `@astrojs/rss` package provides helpers for generating RSS feeds using [API endpoints](/en/core-concepts/astro-pages/#non-html-pages). This unlocks both static builds _and_ on-demand generation when using an [SSR adapter](/en/guides/server-side-rendering/#enabling-ssr-in-your-project).

Create an RSS Feed by calling the `rss()` function that is passed as an argument to `getStaticPaths()`. This will create an `rss.xml` file in your final build based on the data that you provide using the `items` array.
First, install `@astrojs/rss` using your preferred package manager:

```bash
# npm
npm i @astrojs/rss
# yarn
yarn add @astrojs/rss
# pnpm
pnpm i @astrojs/rss
```

Then, ensure you've [configured a `site`](/en/reference/configuration-reference/#site) in your project's `astro.config`. We will use this to generate links in your RSS feed.

Now, let's generate our first RSS feed! Create an `rss.xml.js` file under your `src/pages/` directory. `rss.xml` will be the output URL, so feel free to rename this if you prefer.

Now, import the `rss` helper from the `@astrojs/rss` package and call with the following parameters:

```js
// src/pages/rss.xml.js
import rss from '@astrojs/rss';

export const get = () => rss({
// `<title>` field in output xml
title: 'Buzz’s Blog',
// `<description>` field in output xml
description: 'A humble Astronaut’s guide to the stars',
// list of `<item>`s in output xml
// simple example: generate items for every md file in /src/pages
// see "Generating items" section for required frontmatter and advanced use cases
items: import.meta.glob('./**/*.md'),
// (optional) inject custom xml
customData: `<language>en-us</language>`,
});
```

### Generating `items`

The `items` field accepts either:
1. [An `import.meta.glob(...)` result](#2-importmetaglob-result) **(only use this for `.md` files within the `src/pages/` directory!)**
2. [A list of RSS feed objects](#1-list-of-rss-feed-objects), each with a `link`, `title`, `pubDate`, and optional `description` and `customData` fields.

#### 1. `import.meta.glob` result

We recommend this option as a convenient shorthand for `.md` files under `src/pages/`. Each post should have a `title`, `pubDate`, and optional `description` and `customData` fields in its frontmatter. If this isn't possible, or you'd prefer to generate this frontmatter in code, [see option 2](#2-list-of-rss-feed-objects).

Say your blog posts are stored under the `src/pages/blog/` directory. You can generate an RSS feed like so:

```js
// src/pages/rss.xml.js
import rss from '@astrojs/rss';

export const get = () => rss({
title: 'Buzz’s Blog',
description: 'A humble Astronaut’s guide to the stars',
items: import.meta.glob('./blog/**/*.md'),
});
```

See [Vite's glob import documentation](https://vitejs.dev/guide/features.html#glob-import) for more on this import syntax.

#### 2. List of RSS feed objects

We recommend this option for `.md` files outside of the `pages` directory. This is common when generating routes [via `getStaticPaths`](/en/reference/api-reference/#getstaticpaths).

For instance, say your `.md` posts are stored under a `src/posts/` directory. Each post has a `title`, `pubDate`, and `slug` in its frontmatter, where `slug` corresponds to the output URL on your site. We can generate an RSS feed using [Vite's `import.meta.globEager` helper](https://vitejs.dev/guide/features.html#glob-import) like so:

```js
// src/pages/rss.xml.js
import rss from '@astrojs/rss';

const postImportResult = import.meta.globEager('../posts/**/*.md');
const posts = Object.values(postImportResult);

export const get = () => rss({
title: 'Buzz’s Blog',
description: 'A humble Astronaut’s guide to the stars',
items: posts.map((post) => ({
link: post.frontmatter.slug,
title: post.frontmatter.title,
pubDate: post.frontmatter.pubDate,
}))
});
```

### Adding a stylesheet
bholmesdev marked this conversation as resolved.
Show resolved Hide resolved

You can style your RSS feed for a more pleasant user experience when viewing the file in your browser.

Use the `rss` function's `stylesheet` option to specify an absolute path to your stylesheet.

```js
rss({
// ex. use your stylesheet from "public/rss/styles.xsl"
stylesheet: '/rss/styles.xsl',
// ...
});
```

If you don't have an RSS stylesheet in mind, we recommend the [Pretty Feed v3 default stylesheet](https://github.com/genmon/aboutfeeds/blob/main/tools/pretty-feed-v3.xsl), which you can download from GitHub and save into your project's `public/` directory.

## Using `getStaticPaths()`

> **Note:** This method has been deprecated, and will be removed before the official `v1.0.0` release. Please use `@astrojs/rss` instead.

You can also create an RSS feed from any Astro page that uses a `getStaticPaths()` function for routing. Only dynamic routes can use `getStaticPaths()` today (see [Routing](/en/core-concepts/routing)).

Create an RSS Feed by calling the `rss()` function that is passed as an argument to `getStaticPaths()`. This will create an `rss.xml` file in your final build (or whatever you specify using `dest`) based on the data that you provide using the `items` array.

```js
// Example: /src/pages/posts/[...page].astro
Expand All @@ -35,19 +141,17 @@ export async function getStaticPaths({rss}) {
})),
// Optional: Customize where the file is written to.
// Otherwise, defaults to "/rss.xml"
dest: "/my/custom/feed.xml",
dest: '/my/custom/feed.xml',
});
// Return your paths
return [...];
}
```

Note: RSS feeds will **not** be built during development. Currently, RSS feeds are only generated during your final build.

### Styling
Note: RSS feeds will **not** be built during development when using this method.

RSS Feeds can be styled with an XSL stylesheet for a more pleasant user experience when they are opened directly in a browser. By default, Astro does not set a stylesheet for RSS feeds, but it can be enabled by setting the `stylesheet` option.
### Adding a stylesheet

Astro can automatically use [Pretty Feed](https://github.com/genmon/aboutfeeds/blob/main/tools/pretty-feed-v3.xsl), a popular open-source XSL stylesheet. To enable this behavior, pass `stylesheet: true`.
When using the `getStaticPaths` method to RSS, we will optionally generate a stylesheet for you. Pass `stylesheet: true` as an option to pull in the [Pretty Feed](https://github.com/genmon/aboutfeeds/blob/main/tools/pretty-feed-v3.xsl) XSL stylesheet.

If you'd like to use a custom XSL stylesheet, you can pass a string value like `stylesheet: '/my-custom-stylesheet.xsl'`. This file should be in your `public/` directory (in this case, `public/my-custom-stylesheet.xsl`).
If you'd like to use a custom XSL stylesheet, you can pass a string value like `stylesheet: '/my-custom-stylesheet.xsl'`. This file should be in your `public/` directory (in this case, `/public/my-custom-stylesheet.xsl`).