From c16785f74edcbfb392c98f6bf1cf1ee7ffa14ba0 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Thu, 10 Sep 2020 12:40:41 +0200 Subject: [PATCH] fix(client): do not build Intro for missing blocks This creates an empty page, but ideally it should be a 404 --- client/gatsby-node.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/client/gatsby-node.js b/client/gatsby-node.js index bf3b034838..9e8e0c80cc 100644 --- a/client/gatsby-node.js +++ b/client/gatsby-node.js @@ -1,6 +1,7 @@ const env = require('../config/env'); const { createFilePath } = require('gatsby-source-filesystem'); +const uniq = require('lodash/uniq'); const { dasherize } = require('../utils/slugs'); const { blockNameify } = require('../utils/block-nameify'); @@ -122,6 +123,16 @@ exports.createPages = function createPages({ graphql, actions, reporter }) { createChallengePages(createPage) ); + const blocks = uniq( + result.data.allChallengeNode.edges.map(({ node: { block } }) => block) + ).map(block => blockNameify(block)); + + const superBlocks = uniq( + result.data.allChallengeNode.edges.map( + ({ node: { superBlock } }) => superBlock + ) + ).map(superBlock => blockNameify(superBlock)); + // Create intro pages result.data.allMarkdownRemark.edges.forEach(edge => { const { @@ -136,6 +147,17 @@ exports.createPages = function createPages({ graphql, actions, reporter }) { return null; } try { + if (nodeIdentity === 'blockIntroMarkdown') { + if (!blocks.some(block => block === frontmatter.block)) { + return null; + } + } else if ( + !superBlocks.some( + superBlock => superBlock === frontmatter.superBlock + ) + ) { + return null; + } const pageBuilder = createByIdentityMap[nodeIdentity](createPage); return pageBuilder(edge); } catch (e) {