Skip to content

Commit 93b1caf

Browse files
committed
Catch require error for hot-reloading gatsby-config.js fixes #3990
1 parent 3c1cae9 commit 93b1caf

File tree

1 file changed

+12
-4
lines changed
  • packages/gatsby/src/internal-plugins/internal-data-bridge

1 file changed

+12
-4
lines changed

packages/gatsby/src/internal-plugins/internal-data-bridge/gatsby-node.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,18 @@ exports.sourceNodes = ({ boundActionCreators, store }) => {
122122
`gatsby-config.js`
123123
)
124124
chokidar.watch(pathToGatsbyConfig).on(`change`, () => {
125-
// Delete require cache so we can reload the module.
126-
delete require.cache[require.resolve(pathToGatsbyConfig)]
127-
const config = require(pathToGatsbyConfig)
128-
createGatsbyConfigNode(config)
125+
const oldCache = require.cache[require.resolve(pathToGatsbyConfig)]
126+
try {
127+
// Delete require cache so we can reload the module.
128+
delete require.cache[require.resolve(pathToGatsbyConfig)]
129+
const config = require(pathToGatsbyConfig)
130+
createGatsbyConfigNode(config)
131+
} catch (e) {
132+
// Restore the old cache since requiring the new gatsby-config.js failed.
133+
if (oldCache !== undefined) {
134+
require.cache[require.resolve(pathToGatsbyConfig)] = oldCache
135+
}
136+
}
129137
})
130138
}
131139

0 commit comments

Comments
 (0)