1

The internals for gatsby-plugin-feed seem to indicate that we can set a limit on the number of posts that show up in the feed:

{
  // Create a default RSS feed. Others may be added by using the format below.
  feeds: [
    {
      query: `
      {
        allMarkdownRemark(
          limit: 1000,
          sort: {
            order: DESC,
            fields: [frontmatter___date]
          }
        ) {
          edges {
            node {
              frontmatter {
                title
                date
              }
              fields {
                slug
              }
              excerpt
              html
            }
          }
        }
      }
    `,
    ...
  ]
}

When I try to change limit to 10 in my own definition within gatsby-config.js, it seems to have no effect. Is there a standard way to limit the number of items in the RSS feed so that I don't have 250+ posts in there?

Chris Peters
  • 17,918
  • 6
  • 49
  • 65
  • 1
    Did you also add the `serialize` part in the feeds plugin options as mentioned in the [documentation](https://www.gatsbyjs.org/docs/adding-an-rss-feed/)? – Albert Skibinski Apr 02 '20 at 07:38
  • @AlbertSkibinski Your link to the documentation was very helpful. I don't think I had read it through in its entirely. It helped me see that the RSS feed is only generated on `gatsby build`, not `gatsby develop`. – Chris Peters Apr 10 '20 at 03:37

2 Answers2

1

I am using gatsby-plugin-feed in my personal project. I tried using limit in the GraphQL query as you suggested in your question and it worked in my case.

My guess is that some other plugin or code in your project interferes with the xml generation. The only way to debug your issue might be to create a barebone project and try limiting your items there first. I remember that I had listed gatsby-plugin-feed twice by mistake thus elimating the instructions from the first listing.

This is my code. Maybe it might help you:

// gatsby-config.js

{
  resolve: `gatsby-plugin-feed`,
  options: {
    query: `
      {
        site {
          siteMetadata {
            title
            description
            siteUrl
            site_url: siteUrl
          }
        }
      }
    `,
    feeds: [
      {
        serialize: ({ query: { site, allMarkdownRemark } }) => {
          return allMarkdownRemark.edges.map(edge => {
            return Object.assign({}, edge.node.frontmatter, {
              description: edge.node.excerpt,
              url: site.siteMetadata.siteUrl + "/blog" + edge.node.fields.slug,
              guid: site.siteMetadata.siteUrl + "/blog" + edge.node.fields.slug,
              categories: ["Startup"],
              custom_elements: [
                { "content:encoded": edge.node.html },
                { "author": edge.node.frontmatter.author },
                { "language": "en" },
              ],
            });
          });
        },
        query: `
          {
            allMarkdownRemark(
              sort: { order: DESC, fields: [frontmatter___date] },
              filter: {fileAbsolutePath: {regex: "content/blog/"}},
              limit: 2
            ) {
              edges {
                node {
                  excerpt
                  html
                  fields { slug }
                  frontmatter {
                    title
                    date
                    author
                  }
                }
              }
            }
          }
        `,
        output: "/rss.xml",
        title: "Blog",
        match: "^/blog/",
      },
    ],
  },
},

EliteRaceElephant
  • 7,744
  • 4
  • 47
  • 62
  • Thanks so much for sharing your code, Thomas! I took another route after beating my head on the desk for hours. However, your working code provided me with a much-needed sanity check! – Chris Peters Apr 10 '20 at 04:05
0

@AlbertSkibinski's documentation link in his comment helped me to realize that the RSS feed is only generated on gatsby build. I was making changes and trying to run gatsby develop, which had no effect on the output that I was examining in the public folder.

I am not a huge fan of overwriting default values that don't need to be overridden, so I pieced together my own config code by importing the defaults from gatsby-plugin-feed/internals:

const gatsbyFeedOpts = require('gatsby-plugin-feed/internals')

// Copy and replace limit in default options
let feedOpts = gatsbyFeedOpts.defaultOptions
feedOpts.feeds[0].query = feedOpts.feeds[0].query.replace(
  'limit: 1000',
  'limit: 10'
)

module.exports = {
  siteMetadata: {
    // Excluded for brevity
  },
  plugins: [
    // Other plugins excluded for brevity
    {
      resolve: `gatsby-plugin-feed`,
      options: feedOpts,
    },
  ],
}

It's interesting to note that even though the documentation says we need to implement a serialize method within the feed config, it wasn't included in the imported defaults that I'm using here. This could cause problems in future versions of gatsby-plugin-feed, but it works fine with the version I'm currently using (2.4.1).

Chris Peters
  • 17,918
  • 6
  • 49
  • 65