Skip to content

Commit

Permalink
warn if the serialize function isn't there/valid
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurie committed Feb 10, 2021
1 parent 07ebe2c commit 27bc3c7
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions packages/gatsby-plugin-feed/src/gatsby-node.js
Expand Up @@ -23,7 +23,7 @@ const serialize = ({ query: { site, allMarkdownRemark } }) =>
}
})

exports.onPostBuild = async ({ graphql }, pluginOptions) => {
exports.onPostBuild = async ({ graphql }, pluginOptions, reporter) => {
/*
* Run the site settings query to gather context, then
* then run the corresponding feed for each query.
Expand All @@ -47,21 +47,27 @@ exports.onPostBuild = async ({ graphql }, pluginOptions) => {
...feed,
}

const serializer =
feed.serialize && typeof feed.serialize === `function`
? feed.serialize
: serialize
if (!feed.serialize || typeof feed.serialize !== `function`) {
reporter.warn(
`You did not pass in a valid serialize function. Your feed will not be generated.`
)
} else {
const serializer =
feed.serialize && typeof feed.serialize === `function`
? feed.serialize
: serialize

const rssFeed = (await serializer(locals)).reduce((merged, item) => {
merged.item(item)
return merged
}, new RSS(setup(locals)))
const rssFeed = (await serializer(locals)).reduce((merged, item) => {
merged.item(item)
return merged
}, new RSS(setup(locals)))

const outputPath = path.join(publicPath, feed.output)
const outputDir = path.dirname(outputPath)
if (!(await fs.exists(outputDir))) {
await fs.mkdirp(outputDir)
const outputPath = path.join(publicPath, feed.output)
const outputDir = path.dirname(outputPath)
if (!(await fs.exists(outputDir))) {
await fs.mkdirp(outputDir)
}
await fs.writeFile(outputPath, rssFeed.xml())
}
await fs.writeFile(outputPath, rssFeed.xml())
}
}

0 comments on commit 27bc3c7

Please sign in to comment.