@ascorbic/feed-loader

中文文档
2.0.1发布于 2025/06/26MITTypeScriptES Module近 30 天下载 2427

This package provides feed loaders for Astro. It allows you to load and parse RSS, RDF, and Atom feeds, and use the data in your Astro site.

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. Build-time Feed Loading (Static Collections)
  4. Live Feed Loader (Experimental)
  5. API Reference
  6. feedLoader(options)
  7. liveFeedLoader(options) ⚠️ Experimental
  8. Error Types

Astro feed loader

This package provides feed loaders for Astro. It allows you to load and parse RSS, RDF, and Atom feeds, and use the data in your Astro site.

The package includes two loaders:

  • feedLoader: Build-time feed loading for build-time content collections
  • liveFeedLoader: Experimental runtime feed loading for live content collections

Installation

npm install @ascorbic/feed-loader

Usage

Build-time Feed Loading (Static Collections)

You can use the feed loader in your content configuration like this:

// src/content/config.ts
import { defineCollection } from "astro:content";
import { feedLoader } from "@ascorbic/feed-loader";

const releases = defineCollection({
  loader: feedLoader({
    url: "https://github.com/withastro/astro/releases.atom",
  }),
});

const podcasts = defineCollection({
  loader: feedLoader({
    url: "https://feeds.99percentinvisible.org/99percentinvisible",
  }),
});

export const collections = { releases, podcasts };

You can then use these like any other collection in Astro:

---
import { getCollection } from "astro:content";
import Layout from "../layouts/Layout.astro";

const episodes = await getCollection("podcasts");
---

<Layout title="Episodes">
  <h2>Episodes</h2>
  <ul>
    {
      episodes.map((episode) => (
        <li>
          <a href={`/episodes/${episode.id.replace(/\W/g, "-")}`}>
            {episode.data.title}
          </a>
        </li>
      ))
    }
  </ul>
</Layout>

You can render the episode description using the render() function:

---
import { render, getEntry } from "astro:content";

const episode = getEntry("podcasts", Astro.params.id);

const { Content } = await render(episode);
---
<h1>{episode.data.title}</h1>

<Content />

<p>
    {
        episode.data.media.map((media) => (
            <audio controls>
                <source src={media.url} type={media.mimeType} />
            </audio>
        ))
    }
</p>

Live Feed Loader (Experimental)

⚠️ Experimental Feature: Live content collections require Astro 5.10.0 or later and are currently experimental. The API may change in future versions.

Live feed loading allows you to fetch RSS/Atom feeds at request time rather than build time. This is useful for frequently updated content that you want to display fresh data without rebuilding your site.

Setup

  1. Enable live content collections in your astro.config.mjs:
export default defineConfig({
  // ...
  experimental: {
    liveContentCollections: true,
  },
});
  1. Create a live configuration file at src/live.config.ts:
// src/live.config.ts
import { defineLiveCollection } from "astro:content";
import { liveFeedLoader } from "@ascorbic/feed-loader";

const news = defineLiveCollection({
  type: "live",
  loader: liveFeedLoader({
    url: "https://feeds.bbci.co.uk/news/science_and_environment/rss.xml",
  }),
});

export const collections = { news };
  1. Use live collections in your pages:
---
// src/pages/news/index.astro
import { getLiveCollection } from 'astro:content';
import Layout from '../layouts/Layout.astro';

export const prerender = false; // Required for live content, unless using output: "server"

const { entries: articles, error } = await getLiveCollection('news', { limit: 10 });

if (error) {
  console.error('Failed to load news:', error.message);
}
---

<Layout title="Latest News">
  {error ? (
    <p>Error loading news: {error.message}</p>
  ) : (
    <ul>
      {articles?.map((article) => (
        <li>
          <a href={`/news/${encodeURIComponent(article.id)}`}>
            {article.data.title}
          </a>
          <p>{article.data.description}</p>
        </li>
      ))}
    </ul>
  )}
</Layout>
  1. Create individual article pages with server-side rendering:
---
// src/pages/news/[id].astro
import { getLiveEntry } from 'astro:content';
import Layout from '../../layouts/Layout.astro';

export const prerender = false; // Required for live content, unless using output: "server"

const { id } = Astro.params;
const { entry: article, error } = await getLiveEntry('news', decodeURIComponent(id!));

if (error || !article) {
  return Astro.redirect('/news');
}
---

<Layout title={article.data.title}>
  <h1>{article.data.title}</h1>
  <div set:html={article.data.description} />
  <a href={article.data.url} target="_blank">Read full article →</a>
</Layout>

Live Loader Options

The liveFeedLoader supports the same options as feedLoader:

liveFeedLoader({
  url: "https://example.com/feed.xml",
  requestOptions: {
    headers: {
      "User-Agent": "My Astro Site",
    },
  },
});

Filtering Live Collections

You can filter live collections when fetching them:

// Get latest 5 articles
const { entries } = await getLiveCollection("news", { limit: 5 });

// Filter by category
const { entries } = await getLiveCollection("news", { category: "science" });

// Filter by author
const { entries } = await getLiveCollection("news", { author: "john" });

// Filter by date range
const { entries } = await getLiveCollection("news", {
  since: new Date("2024-01-01"),
  until: new Date("2024-12-31"),
});

Error Handling

Live feed loaders return structured errors that you can handle appropriately:

import { FeedLoadError, FeedValidationError } from "@ascorbic/feed-loader";

const { entries, error } = await getLiveCollection("news");

if (error) {
  if (error instanceof FeedLoadError) {
    console.error(`Feed loading failed: ${error.message} (${error.code})`);
  } else if (error instanceof FeedValidationError) {
    console.error(`Feed validation failed: ${error.message}`);
  }
}

When to Use Live vs Static Loading

Use live loading when:

  • Content updates frequently (multiple times per day)
  • You need real-time data
  • You want to avoid rebuilds for content changes
  • You're building preview functionality

Use static loading when:

  • Content is relatively static
  • Performance is critical (pre-rendered)
  • You want build-time optimization
  • You need to process MDX or images

API Reference

feedLoader(options)

Static content collections loader for build-time feed processing.

liveFeedLoader(options) ⚠️ Experimental

Live content collections loader for runtime feed processing.

Options

  • url (required): Feed URL to fetch from
  • requestOptions: Custom fetch options (headers, etc.)

Error Types

  • FeedError: Base error class
  • FeedLoadError: Network/HTTP errors
  • FeedValidationError: Feed parsing/validation errors

版本与发布记录

@ascorbic/feed-loader 共发布 10 个版本,首次发布于 2024/07/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.1latest2025/06/261.4 年前
2.0.02025/06/221.4 年前
1.0.42024/10/282 年前
1.0.32024/10/212 年前
1.0.22024/10/192 年前
1.0.12024/09/132 年前

关于 @ascorbic/feed-loader 的常见问题

@ascorbic/feed-loader 是什么?

This package provides feed loaders for Astro. It allows you to load and parse RSS, RDF, and Atom feeds, and use the data in your Astro site.

如何安装 @ascorbic/feed-loader?

在项目目录运行 npm install @ascorbic/feed-loader,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:astro(^4.14.0 || ^5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ascorbic/feed-loader 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ascorbic/feed-loader 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ascorbic/feed-loader 可以商用吗?

@ascorbic/feed-loader 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ascorbic/feed-loader 的下载量有多少?

根据 npm 官方下载统计,@ascorbic/feed-loader 在 2026/10/07 – 2026/11/05 这 30 天内被下载 2,427 次,日均约 81 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ascorbic/feed-loader 还在维护吗?

最新版本 2.0.1 发布于 2025/06/26(1.4 年前);至今共发布 10 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/07/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。