Skip to content

Commit 61da793

Browse files
committed
Resolve local development race condition on simultaneous nodemon and gulp watch process respawn.
1 parent ebfaa7b commit 61da793

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

.core/gulp.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const defaultConfig = {
2020
browsersync: 3000,
2121
proxy: 3030,
2222
},
23+
serverRetries: 4,
24+
serverRetryDelay: 2000,
2325
open: true,
2426
cssPreProcessor: 'sass',
2527
watch: {

.core/gulp.tasks.js

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,22 @@ const workbox = require('workbox-build');
2929
const { File, FileReader } = require('file-api');
3030
const handlebars = require('handlebars');
3131
const { resolve } = require('path');
32+
const axios = require('axios');
33+
const axiosRetry = require('axios-retry');
3234

3335
// For backward compatibility with gulp override tasks using run-sequence module
3436
// make compatible with gulp4
3537
require('module-alias').addAlias('run-sequence', 'gulp4-run-sequence');
3638

3739
const reactium = (gulp, config, webpackConfig) => {
40+
axiosRetry(axios, {
41+
retries: config.serverRetries,
42+
retryDelay: retryCount => {
43+
console.log(`retry attempt: ${retryCount}`);
44+
return retryCount * config.serverRetryDelay; // time interval between retries
45+
},
46+
});
47+
3848
const task = require('./get-task')(gulp);
3949

4050
const env = process.env.NODE_ENV || 'development';
@@ -109,23 +119,21 @@ const reactium = (gulp, config, webpackConfig) => {
109119

110120
const serve = ({ open } = { open: config.open }) => done => {
111121
const proxy = `localhost:${config.port.proxy}`;
112-
require('axios')
113-
.get(`http://${proxy}`)
114-
.then(() => {
115-
browserSync({
116-
notify: false,
117-
timestamps: false,
118-
port: config.port.browsersync,
119-
ui: { port: config.port.browsersync + 1 },
120-
proxy,
121-
open: open,
122-
ghostMode: false,
123-
startPath: config.dest.startPath,
124-
ws: true,
125-
});
126-
127-
done();
122+
axios.get(`http://${proxy}`).then(() => {
123+
browserSync({
124+
notify: false,
125+
timestamps: false,
126+
port: config.port.browsersync,
127+
ui: { port: config.port.browsersync + 1 },
128+
proxy,
129+
open: open,
130+
ghostMode: false,
131+
startPath: config.dest.startPath,
132+
ws: true,
128133
});
134+
135+
done();
136+
});
129137
};
130138

131139
const watch = (done, restart = false) => {
@@ -142,9 +150,20 @@ const reactium = (gulp, config, webpackConfig) => {
142150
return;
143151
}
144152
case 'restart-watches': {
145-
console.log("Restarting 'watch'...");
146-
watchProcess.kill();
147-
watch(_ => _, true);
153+
console.log('Waiting for server...');
154+
new Promise(resolve =>
155+
setTimeout(resolve, config.serverRetryDelay),
156+
)
157+
.then(() => {
158+
const proxy = `localhost:${config.port.proxy}`;
159+
return axios.get(`http://${proxy}`);
160+
})
161+
.then(() => {
162+
console.log("Restarting 'watch'...");
163+
watchProcess.kill();
164+
watch(_ => _, true);
165+
})
166+
.catch(error => console.error(error));
148167
return;
149168
}
150169
}

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@loadable/component": "^5.14.1",
5656
"action-sequence": "^1.1.1",
5757
"axios": "^0.21.1",
58+
"axios-retry": "^3.1.9",
5859
"body-parser": "^1.19.0",
5960
"chalk": "^4.1.0",
6061
"classnames": "^2.3.1",

0 commit comments

Comments
 (0)