Skip to content

Commit

Permalink
Catch require error for hot-reloading gatsby-config.js fixes #3990
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAMathews committed Feb 14, 2018
1 parent 3c1cae9 commit 93b1caf
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,18 @@ exports.sourceNodes = ({ boundActionCreators, store }) => {
`gatsby-config.js`
)
chokidar.watch(pathToGatsbyConfig).on(`change`, () => {
// Delete require cache so we can reload the module.
delete require.cache[require.resolve(pathToGatsbyConfig)]
const config = require(pathToGatsbyConfig)
createGatsbyConfigNode(config)
const oldCache = require.cache[require.resolve(pathToGatsbyConfig)]
try {
// Delete require cache so we can reload the module.
delete require.cache[require.resolve(pathToGatsbyConfig)]
const config = require(pathToGatsbyConfig)
createGatsbyConfigNode(config)
} catch (e) {
// Restore the old cache since requiring the new gatsby-config.js failed.
if (oldCache !== undefined) {
require.cache[require.resolve(pathToGatsbyConfig)] = oldCache
}
}
})
}

Expand Down

0 comments on commit 93b1caf

Please sign in to comment.