From e72855dad336406884c6b02dd1a25ba3cabc7373 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Tue, 8 Oct 2019 10:19:43 +0200 Subject: [PATCH] fix: handle invalid Algolia keys gracefully in dev (#37088) Invalid keys throw an error in production, but just cause invalid PropTypes and build warnings in development. --- client/gatsby-node.js | 15 +++++++++++++-- client/src/components/search/WithInstantSearch.js | 6 +++++- config/env.js | 10 ++++++++-- sample.env | 4 ++-- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/client/gatsby-node.js b/client/gatsby-node.js index 2c6811ed2f2..7f9debc834e 100644 --- a/client/gatsby-node.js +++ b/client/gatsby-node.js @@ -1,4 +1,4 @@ -require('dotenv').config(); +const env = require('../config/env'); const { createFilePath } = require('gatsby-source-filesystem'); @@ -39,7 +39,18 @@ exports.onCreateNode = function onCreateNode({ node, actions, getNode }) { } }; -exports.createPages = function createPages({ graphql, actions }) { +exports.createPages = function createPages({ graphql, actions, reporter }) { + if (!env.algoliaAPIKey || !env.algoliaAppId) { + if (process.env.FREECODECAMP_NODE_ENV === 'production') { + throw new Error( + 'Algolia App id and API key are required to start the client!' + ); + } else { + reporter.info( + 'Algolia keys missing or invalid. Required for search to yield results.' + ); + } + } const { createPage } = actions; return new Promise((resolve, reject) => { diff --git a/client/src/components/search/WithInstantSearch.js b/client/src/components/search/WithInstantSearch.js index 70c7c79b34f..00f4ed0ebeb 100644 --- a/client/src/components/search/WithInstantSearch.js +++ b/client/src/components/search/WithInstantSearch.js @@ -20,7 +20,11 @@ import { createSelector } from 'reselect'; const DEBOUNCE_TIME = 100; -const searchClient = algoliasearch(algoliaAppId, algoliaAPIKey); +// If a key is missing, searches will fail, but the client will still render. +const searchClient = + algoliaAppId && algoliaAPIKey + ? algoliasearch(algoliaAppId, algoliaAPIKey) + : {}; const propTypes = { children: PropTypes.any, diff --git a/config/env.js b/config/env.js index 4ca9cf8f4cf..5cb8d0891bd 100644 --- a/config/env.js +++ b/config/env.js @@ -30,6 +30,12 @@ const locations = { module.exports = Object.assign(locations, { locale, stripePublicKey, - algoliaAppId, - algoliaAPIKey + algoliaAppId: + !algoliaAppId || algoliaAppId === 'Algolia app id from dashboard' + ? null + : algoliaAppId, + algoliaAPIKey: + !algoliaAPIKey || algoliaAPIKey === 'Algolia api key from dashboard' + ? null + : algoliaAPIKey }); diff --git a/sample.env b/sample.env index b80b45c4fec..f49637e2c3e 100644 --- a/sample.env +++ b/sample.env @@ -5,8 +5,8 @@ ROLLBAR_APP_ID='my-rollbar-app-id' ROLLBAR_CLIENT_ID='post_client_id from rollbar dashboard' ALGOLIA_ADMIN_KEY=123abc -ALGOLIA_APP_ID=ACDEFG -ALGOLIA_API_KEY=123abc +ALGOLIA_APP_ID='Algolia app id from dashboard' +ALGOLIA_API_KEY='Algolia api key from dashboard' AUTH0_CLIENT_ID=stuff AUTH0_CLIENT_SECRET=stuff