Skip to content

Commit b5a186c

Browse files
jackjocrossjaredpalmer
authored andcommitted
Update examples to recover from HMR error (#763)
1 parent f15ccc1 commit b5a186c

29 files changed

Lines changed: 275 additions & 119 deletions

File tree

examples/basic/src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import express from 'express';
2-
import app from './server';
2+
3+
let app = require('./server').default;
34

45
if (module.hot) {
56
module.hot.accept('./server', function() {
67
console.log('🔁 HMR Reloading `./server`...');
8+
try {
9+
app = require('./server').default;
10+
} catch (error) {
11+
console.error(error);
12+
}
713
});
814
console.info('✅ Server-side HMR Enabled!');
915
}

examples/with-afterjs/src/index.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
import app from './server';
2-
import http from 'http';
1+
import express from 'express';
32

4-
const server = http.createServer(app);
5-
6-
let currentApp = app;
7-
8-
server.listen(process.env.PORT || 3000, error => {
9-
if (error) {
10-
console.log(error);
11-
}
12-
13-
console.log('🚀 started');
14-
});
3+
let app = require('./server').default;
154

165
if (module.hot) {
17-
console.log('✅ Server-side HMR Enabled!');
18-
19-
module.hot.accept('./server', () => {
6+
module.hot.accept('./server', function() {
207
console.log('🔁 HMR Reloading `./server`...');
21-
server.removeListener('request', currentApp);
22-
const newApp = require('./server').default;
23-
server.on('request', newApp);
24-
currentApp = newApp;
8+
try {
9+
app = require('./server').default;
10+
} catch (error) {
11+
console.error(error);
12+
}
2513
});
14+
console.info('✅ Server-side HMR Enabled!');
2615
}
16+
17+
const port = process.env.PORT || 3000;
18+
19+
export default express()
20+
.use((req, res) => app.handle(req, res))
21+
.listen(port, function(err) {
22+
if (err) {
23+
console.error(err);
24+
return;
25+
}
26+
console.log(`> Started on port ${port}`);
27+
});

examples/with-custom-babel-config/src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import express from 'express';
2-
import app from './server';
2+
3+
let app = require('./server').default;
34

45
if (module.hot) {
56
module.hot.accept('./server', function() {
67
console.log('🔁 HMR Reloading `./server`...');
8+
try {
9+
app = require('./server').default;
10+
} catch (error) {
11+
console.error(error);
12+
}
713
});
814
console.info('✅ Server-side HMR Enabled!');
915
}

examples/with-custom-environment-variables/src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import express from 'express';
2-
import app from './server';
2+
3+
let app = require('./server').default;
34

45
if (module.hot) {
56
module.hot.accept('./server', function() {
67
console.log('🔁 HMR Reloading `./server`...');
8+
try {
9+
app = require('./server').default;
10+
} catch (error) {
11+
console.error(error);
12+
}
713
});
814
console.info('✅ Server-side HMR Enabled!');
915
}

examples/with-custom-webpack-config/src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import express from 'express';
2-
import app from './server';
2+
3+
let app = require('./server').default;
34

45
if (module.hot) {
56
module.hot.accept('./server', function() {
67
console.log('🔁 HMR Reloading `./server`...');
8+
try {
9+
app = require('./server').default;
10+
} catch (error) {
11+
console.error(error);
12+
}
713
});
814
console.info('✅ Server-side HMR Enabled!');
915
}

examples/with-elm/src/index.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
import app from './server';
2-
import http from 'http';
1+
import express from 'express';
32

4-
const server = http.createServer(app);
5-
6-
let currentApp = app;
7-
8-
server.listen(process.env.PORT || 3000);
3+
let app = require('./server').default;
94

105
if (module.hot) {
11-
console.log('✅ Server-side HMR Enabled!');
12-
13-
module.hot.accept('./server', () => {
6+
module.hot.accept('./server', function() {
147
console.log('🔁 HMR Reloading `./server`...');
15-
server.removeListener('request', currentApp);
16-
const newApp = require('./server').default;
17-
server.on('request', newApp);
18-
currentApp = newApp;
8+
try {
9+
app = require('./server').default;
10+
} catch (error) {
11+
console.error(error);
12+
}
1913
});
14+
console.info('✅ Server-side HMR Enabled!');
2015
}
16+
17+
const port = process.env.PORT || 3000;
18+
19+
export default express()
20+
.use((req, res) => app.handle(req, res))
21+
.listen(port, function(err) {
22+
if (err) {
23+
console.error(err);
24+
return;
25+
}
26+
console.log(`> Started on port ${port}`);
27+
});

examples/with-firebase-functions/src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import express from 'express';
2-
import app from './server';
2+
3+
let app = require('./server').default;
34

45
if (module.hot) {
56
module.hot.accept('./server', function() {
67
console.log('🔁 HMR Reloading `./server`...');
8+
try {
9+
app = require('./server').default;
10+
} catch (error) {
11+
console.error(error);
12+
}
713
});
814
console.info('✅ Server-side HMR Enabled!');
915
}

examples/with-heroku/src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import express from 'express';
2-
import app from './server';
2+
3+
let app = require('./server').default;
34

45
if (module.hot) {
56
module.hot.accept('./server', function() {
67
console.log('🔁 HMR Reloading `./server`...');
8+
try {
9+
app = require('./server').default;
10+
} catch (error) {
11+
console.error(error);
12+
}
713
});
814
console.info('✅ Server-side HMR Enabled!');
915
}

examples/with-hyperapp/src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import express from 'express';
2-
import app from './server';
2+
3+
let app = require('./server').default;
34

45
if (module.hot) {
56
module.hot.accept('./server', function() {
67
console.log('🔁 HMR Reloading `./server`...');
8+
try {
9+
app = require('./server').default;
10+
} catch (error) {
11+
console.error(error);
12+
}
713
});
814
console.info('✅ Server-side HMR Enabled!');
915
}

examples/with-inferno/src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import express from 'express';
2-
import app from './server';
2+
3+
let app = require('./server').default;
34

45
if (module.hot) {
56
module.hot.accept('./server', function() {
67
console.log('🔁 HMR Reloading `./server`...');
8+
try {
9+
app = require('./server').default;
10+
} catch (error) {
11+
console.error(error);
12+
}
713
});
814
console.info('✅ Server-side HMR Enabled!');
915
}

0 commit comments

Comments
 (0)