Skip to content

Commit

Permalink
DOP-4083: Prevent page creation when missing metadata (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
rayangler authored Oct 18, 2023
1 parent 9100bc2 commit ed8e9f5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions plugins/gatsby-source-snooty-preview/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ const { parser } = require(`stream-json/jsonl/Parser`);
const { sourceNodes } = require(`./other-things-to-source`);
const { fetchClientAccessToken } = require('./utils/kanopy-auth.js');
const { callPostBuildWebhook } = require('./utils/post-build.js');
const { consumeData, KEY_LAST_FETCHED, KEY_LAST_CLIENT_ACCESS_TOKEN } = require('./utils/data-consumer.js');
const {
consumeData,
createSnootyMetadataId,
KEY_LAST_FETCHED,
KEY_LAST_CLIENT_ACCESS_TOKEN,
} = require('./utils/data-consumer.js');

// Global variable to allow webhookBody from sourceNodes step to be passed down
// to other Gatsby build steps that might not pass webhookBody natively.
Expand Down Expand Up @@ -181,7 +186,7 @@ exports.onCreateWebpackConfig = ({ plugins, actions }) => {
});
};

exports.createPages = async ({ actions, graphql, reporter }) => {
exports.createPages = async ({ actions, createNodeId, getNode, graphql, reporter }) => {
const { createPage } = actions;
const templatePath = path.join(__dirname, `../../src/components/DocumentBodyPreview.js`);
const result = await graphql(`
Expand Down Expand Up @@ -211,6 +216,16 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
// Slices off leading slash to ensure slug matches an entry within the toctreeOrder and renders InternalPageNav components
if (slug !== '/' && slug[0] === '/') slug = slug.slice(1);

const metadataNodeId = createSnootyMetadataId({ createNodeId, branch: node.branch, project: node.project });
if (!getNode(metadataNodeId)) {
// Take into account the possibility of having new page data available through the API,
// but no metadata yet due to async uploads
console.warn(
`Skipping node creation for page "${node.page_id}", in project "${node.project}" on branch "${node.branch}". No metadata node "${metadataNodeId}" found.`
);
return;
}

createPage({
path: pagePath,
component: templatePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,4 @@ const consumeData = async (
}
};

module.exports = { consumeData, KEY_LAST_FETCHED, KEY_LAST_CLIENT_ACCESS_TOKEN };
module.exports = { consumeData, createSnootyMetadataId, KEY_LAST_FETCHED, KEY_LAST_CLIENT_ACCESS_TOKEN };

0 comments on commit ed8e9f5

Please sign in to comment.