Skip to content

Commit dfc4690

Browse files
committed
Fixture: Add fetch from a Server Component
1 parent b01ff58 commit dfc4690

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

fixtures/flight/server/index.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,28 @@ const app = express();
1717
// Application
1818
app.get('/', function(req, res) {
1919
if (process.env.NODE_ENV === 'development') {
20-
for (var key in require.cache) {
21-
delete require.cache[key];
22-
}
20+
// This doesn't work in ESM mode.
21+
// for (var key in require.cache) {
22+
// delete require.cache[key];
23+
// }
2324
}
2425
require('./handler.server.js')(req, res);
2526
});
2627

28+
app.get('/todos', function(req, res) {
29+
res.setHeader('Access-Control-Allow-Origin', '*');
30+
res.json([
31+
{
32+
id: 1,
33+
text: 'Shave yaks',
34+
},
35+
{
36+
id: 2,
37+
text: 'Eat kale',
38+
},
39+
]);
40+
});
41+
2742
app.listen(3001, () => {
2843
console.log('Flight Server listening on port 3001...');
2944
});

fixtures/flight/src/App.server.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import {fetch} from 'react-fetch';
23

34
import Container from './Container.js';
45

@@ -7,10 +8,16 @@ import Counter from './Counter.client.js';
78
import ShowMore from './ShowMore.client.js';
89

910
export default function App() {
11+
const todos = fetch('http://localhost:3001/todos').json();
1012
return (
1113
<Container>
1214
<h1>Hello, world</h1>
1315
<Counter />
16+
<ul>
17+
{todos.map(todo => (
18+
<li key={todo.id}>{todo.text}</li>
19+
))}
20+
</ul>
1421
<ShowMore>
1522
<p>Lorem ipsum</p>
1623
</ShowMore>

0 commit comments

Comments
 (0)