From 6d77c70eb264dabd9c6863c18aa8cffa8b84391c Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Wed, 12 Aug 2020 22:05:05 -0700 Subject: [PATCH] feat: add config to enable swiftype builds using env variable --- gatsby-config.js | 1 + package.json | 1 + plugins/gatsby-source-swiftype/gatsby-node.js | 9 ++++++--- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/gatsby-config.js b/gatsby-config.js index 4f9c0ec13..10b9b79f7 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -48,6 +48,7 @@ module.exports = { resolve: 'gatsby-source-swiftype', options: { file: `${__dirname}/src/data/related-pages.json`, + enabled: Boolean(process.env.BUILD_RELATED_CONTENT), engineKey: 'Ad9HfGjDw4GRkcmJjUut', pageLimit: 5, getPath: ({ node }) => node.frontmatter.path, diff --git a/package.json b/package.json index 4c26df50b..b395e04f4 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "build": "gatsby build", "build:production": "GATSBY_NEWRELIC_ENV=production gatsby build", "build:staging": "GATSBY_NEWRELIC_ENV=staging gatsby build", + "build:related-content": "BUILD_RELATED_CONTENT=true npm run build:production", "develop": "gatsby develop", "format": "prettier --write \"**/*.{js,jsx,json,md}\"", "start": "npm run develop", diff --git a/plugins/gatsby-source-swiftype/gatsby-node.js b/plugins/gatsby-source-swiftype/gatsby-node.js index 8fa90c626..6eea73564 100644 --- a/plugins/gatsby-source-swiftype/gatsby-node.js +++ b/plugins/gatsby-source-swiftype/gatsby-node.js @@ -6,6 +6,7 @@ const data = {}; exports.onCreateNode = async ({ node, getNodesByType }, pluginOptions) => { const { + enabled, filterNode = () => false, getParams, getPath, @@ -13,7 +14,7 @@ exports.onCreateNode = async ({ node, getNodesByType }, pluginOptions) => { engineKey, } = pluginOptions; - if (!filterNode({ node })) { + if (!enabled || !filterNode({ node })) { return; } @@ -61,7 +62,9 @@ exports.onCreateNode = async ({ node, getNodesByType }, pluginOptions) => { }; exports.onPostBootstrap = (_, pluginOptions) => { - const { file } = pluginOptions; + const { file, enabled } = pluginOptions; - fs.writeFileSync(file, JSON.stringify(data, null, 2), { flag: 'w' }); + if (enabled) { + fs.writeFileSync(file, JSON.stringify(data, null, 2), { flag: 'w' }); + } };