Skip to content
Permalink
Browse files

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.
  • Loading branch information...
ojeytonwilliams authored and raisedadead committed Oct 8, 2019
1 parent 4889e0f commit e72855dad336406884c6b02dd1a25ba3cabc7373
Showing with 28 additions and 7 deletions.
  1. +13 −2 client/gatsby-node.js
  2. +5 −1 client/src/components/search/WithInstantSearch.js
  3. +8 −2 config/env.js
  4. +2 −2 sample.env
@@ -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) => {
@@ -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,
@@ -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
});
@@ -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

0 comments on commit e72855d

Please sign in to comment.
You can’t perform that action at this time.