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.
pull/37114/head
Oliver Eyton-Williams 2019-10-08 10:19:43 +02:00 committed by mrugesh
parent 4889e0fdc6
commit e72855dad3
4 changed files with 28 additions and 7 deletions

View File

@ -1,4 +1,4 @@
require('dotenv').config(); const env = require('../config/env');
const { createFilePath } = require('gatsby-source-filesystem'); 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; const { createPage } = actions;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -20,7 +20,11 @@ import { createSelector } from 'reselect';
const DEBOUNCE_TIME = 100; 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 = { const propTypes = {
children: PropTypes.any, children: PropTypes.any,

View File

@ -30,6 +30,12 @@ const locations = {
module.exports = Object.assign(locations, { module.exports = Object.assign(locations, {
locale, locale,
stripePublicKey, stripePublicKey,
algoliaAppId, algoliaAppId:
algoliaAPIKey !algoliaAppId || algoliaAppId === 'Algolia app id from dashboard'
? null
: algoliaAppId,
algoliaAPIKey:
!algoliaAPIKey || algoliaAPIKey === 'Algolia api key from dashboard'
? null
: algoliaAPIKey
}); });

View File

@ -5,8 +5,8 @@ ROLLBAR_APP_ID='my-rollbar-app-id'
ROLLBAR_CLIENT_ID='post_client_id from rollbar dashboard' ROLLBAR_CLIENT_ID='post_client_id from rollbar dashboard'
ALGOLIA_ADMIN_KEY=123abc ALGOLIA_ADMIN_KEY=123abc
ALGOLIA_APP_ID=ACDEFG ALGOLIA_APP_ID='Algolia app id from dashboard'
ALGOLIA_API_KEY=123abc ALGOLIA_API_KEY='Algolia api key from dashboard'
AUTH0_CLIENT_ID=stuff AUTH0_CLIENT_ID=stuff
AUTH0_CLIENT_SECRET=stuff AUTH0_CLIENT_SECRET=stuff