我正在使用Gatsby JS基于同一模板生成包含数千个页面的网站。为了生成所有这些页面,我对外部服务进行了一些调用,以获取填充它们的数据。
我的问题是,有时这些调用失败,但可能仅适用于1500页之一。
是否可以中止失败的特定页面的生成,以便不生成该页面,并且我可以安全地重新部署其他页面,而不会覆盖失败的页面?
我尝试使用onCreatePage,但没有运气。
答案 0 :(得分:1)
这是我之前回答的similar question。
是否可以中止失败的特定页面的生成,以便不生成
是的。您可以在gatsby-node.js
中做到这一点:
const path = require(`path`)
exports.createPages = ({ graphql, actions }) => {
const { createPage, deletePage } = actions
const template = path.resolve(`src/templates/template.js`)
return graphql(`
// your query
`, { limit: 10000 }).then(result => {
if (result.errors) {
throw result.errors
}
result.data.allMarkdownRemark.edges.forEach(edge => {
// ##### Abort page generation HERE #######
// Find a graphQL attribute that is undefined or null only when your call fails
// I use callSuccess as an example. It could be the frontmatter or whatever
if (edge.callSuccess != null) { // Only generate a page when the call is successful
createPage({
path: `${edge.node.frontmatter.slug}`,
component: template ,
context: {},
})
}
deletePage(page); // otherwise delete page
})
}
我可以安全地重新部署其他服务器,而不会覆盖失败的服务器吗?
没有简单的方法。 Gatsby每次构建都会重建所有页面。我不知道要检索以前版本的页面。也许有一种方法可以再次查询您的外部服务,并以此方式检索数据。