Author image

ぺんすけブログ

リモートワークと子育てとTechな日常

Cover image

GridsomeのブログでRSSフィードを設定する

プラグインの候補

上記2つがありましたが、直近で更新されているonecrayon/gridsome-plugin-feedの方にしました。こちらの方がatomやjson形式にも対応しているようです。

ちなみにベースとなっているのはこちらなので困った時はこちらを見る方が良いかも知れません。

プラグインインストール

yarn add gridsome-plugin-feed

gridsome.config.jsの設定

プラグインの設定をgridsome.config.jsに書きます。今回はrssとatom、2つを有効にしてみました。

plugins [
	{
      use: 'gridsome-plugin-feed',
      options: {
        // Required: array of `GraphQL` type names you wish to include
        contentTypes: ['Post'],
        // Optional: any properties you wish to set for `Feed()` constructor
        // See https://www.npmjs.com/package/feed#example for available properties
        feedOptions: {
          title: siteName,
          description: siteDescription
        },
        rss: {
          enabled: true,
          output: '/feed.xml'
        },
        atom: {
          enabled: true,
          output: '/feed.atom'
        },
        // Optional: the maximum number of items to include in your feed
        maxItems: 25,
        // Optional: an array of properties passed to `Feed.addItem()` that will be parsed for
        // URLs in HTML (ensures that URLs are full `http` URLs rather than site-relative).
        // To disable this functionality, set to `null`.
        htmlFields: ['description', 'content'],
        // Optional: a method that accepts a node and returns true (include) or false (exclude)
        // Example: only past-dated nodes: `filterNodes: (node) => node.fields.date <= new Date()`
        filterNodes: (node) => true,
        // Optional: a method that accepts a node and returns an object for `Feed.addItem()`
        // See https://www.npmjs.com/package/feed#example for available properties
        // NOTE: `date` field MUST be a Javascript `Date` object
        nodeToFeedItem: (node) => ({
          id: node.id,
          title: node.title,
          updated_at: 2000-01-01
date: node.fields.date,
          description: node.fields.description,
          image: node.fields.cover_image
        })
      }
]

そして出力されるfeedがこちらです。

このプラグインではatomの拡張子は必ず .atom にrssの拡張子は .xml になるようです。