Skip to content

Commit 165f57d

Browse files
committed
[Flight Fixture] Refetching
1 parent dfc4690 commit 165f57d

File tree

8 files changed

+98
-21
lines changed

8 files changed

+98
-21
lines changed

fixtures/flight/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"camelcase": "^5.2.0",
1919
"case-sensitive-paths-webpack-plugin": "2.2.0",
2020
"concurrently": "^5.0.0",
21+
"cors": "^2.8.5",
2122
"css-loader": "2.1.1",
2223
"dotenv": "6.2.0",
2324
"dotenv-expand": "5.1.0",

fixtures/flight/server/handler.server.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ module.exports = async function(req, res) {
1818
// TODO: Read from a map on the disk.
1919
[resolve('../src/Counter.client.js')]: {
2020
id: './src/Counter.client.js',
21-
chunks: ['1'],
21+
chunks: ['2'],
2222
name: 'default',
2323
},
2424
[resolve('../src/ShowMore.client.js')]: {
2525
id: './src/ShowMore.client.js',
26-
chunks: ['2'],
26+
chunks: ['3'],
27+
name: 'default',
28+
},
29+
[resolve('../src/AddTodo.client.js')]: {
30+
id: './src/AddTodo.client.js',
31+
chunks: ['1'],
2732
name: 'default',
2833
},
2934
});

fixtures/flight/server/index.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ babelRegister({
1212
});
1313

1414
const express = require('express');
15+
const cors = require('cors');
1516
const app = express();
17+
app.use(cors());
18+
app.use(express.json());
1619

1720
// Application
1821
app.get('/', function(req, res) {
@@ -25,18 +28,27 @@ app.get('/', function(req, res) {
2528
require('./handler.server.js')(req, res);
2629
});
2730

31+
let todos = [
32+
{
33+
id: 1,
34+
text: 'Shave yaks',
35+
},
36+
{
37+
id: 2,
38+
text: 'Eat kale!',
39+
},
40+
];
41+
2842
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-
]);
43+
res.json(todos);
44+
});
45+
46+
app.post('/todos', function(req, res) {
47+
todos.push({
48+
id: todos.length + 1,
49+
text: req.body.text,
50+
});
51+
res.json(todos);
4052
});
4153

4254
app.listen(3001, () => {

fixtures/flight/src/AddTodo.client.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as React from 'react';
2+
3+
import Container from './Container.js';
4+
import {RefreshContext} from './Context.client';
5+
6+
export default function Counter() {
7+
const [text, setText] = React.useState('');
8+
const [startTransition, isPending] = React.unstable_useTransition();
9+
const refresh = React.useContext(RefreshContext);
10+
return (
11+
<Container>
12+
<input value={text} onChange={e => setText(e.target.value)} />
13+
<button
14+
onClick={async () => {
15+
await fetch('http://localhost:3001/todos', {
16+
method: 'POST',
17+
mode: 'cors',
18+
headers: {
19+
Accept: 'application/json',
20+
'Content-Type': 'application/json',
21+
},
22+
body: JSON.stringify({text}),
23+
});
24+
startTransition(() => {
25+
refresh();
26+
});
27+
}}>
28+
add
29+
</button>
30+
{isPending && ' ...'}
31+
</Container>
32+
);
33+
}

fixtures/flight/src/App.server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {fetch} from 'react-fetch';
33

44
import Container from './Container.js';
55

6+
import AddTodo from './AddTodo.client.js';
67
import Counter from './Counter.client.js';
7-
88
import ShowMore from './ShowMore.client.js';
99

1010
export default function App() {
@@ -18,6 +18,9 @@ export default function App() {
1818
<li key={todo.id}>{todo.text}</li>
1919
))}
2020
</ul>
21+
<AddTodo />
22+
<br />
23+
<br />
2124
<ShowMore>
2225
<p>Lorem ipsum</p>
2326
</ShowMore>

fixtures/flight/src/Context.client.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as React from 'react';
2+
3+
export const RefreshContext = React.createContext(null);

fixtures/flight/src/index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,32 @@ import * as React from 'react';
22
import {Suspense} from 'react';
33
import ReactDOM from 'react-dom';
44
import ReactTransportDOMClient from 'react-transport-dom-webpack';
5+
import {RefreshContext} from './Context.client';
56

6-
let data = ReactTransportDOMClient.createFromFetch(
7+
let initialData = ReactTransportDOMClient.createFromFetch(
78
fetch('http://localhost:3001')
89
);
910

1011
function Content() {
11-
return data.readRoot();
12+
let [data, setData] = React.useState(initialData);
13+
14+
function refresh() {
15+
setData(
16+
ReactTransportDOMClient.createFromFetch(fetch('http://localhost:3001'))
17+
);
18+
}
19+
20+
return (
21+
<RefreshContext.Provider value={refresh}>
22+
{data.readRoot()}
23+
</RefreshContext.Provider>
24+
);
1225
}
1326

14-
ReactDOM.render(
27+
ReactDOM.unstable_createRoot(document.getElementById('root')).render(
1528
<Suspense fallback={<h1>Loading...</h1>}>
1629
<Content />
17-
</Suspense>,
18-
document.getElementById('root')
30+
</Suspense>
1931
);
2032

2133
// Create entry points for Client Components.

fixtures/flight/yarn.lock

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,6 +2804,14 @@ [email protected], core-util-is@~1.0.0:
28042804
version "1.0.2"
28052805
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
28062806

2807+
cors@^2.8.5:
2808+
version "2.8.5"
2809+
resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29"
2810+
integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==
2811+
dependencies:
2812+
object-assign "^4"
2813+
vary "^1"
2814+
28072815
cosmiconfig@^4.0.0:
28082816
version "4.0.0"
28092817
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-4.0.0.tgz#760391549580bbd2df1e562bc177b13c290972dc"
@@ -6457,7 +6465,7 @@ oauth-sign@~0.9.0:
64576465
version "0.9.0"
64586466
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455"
64596467

6460-
[email protected], object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
6468+
[email protected], object-assign@^4, object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
64616469
version "4.1.1"
64626470
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
64636471

@@ -9309,7 +9317,7 @@ validate-npm-package-license@^3.0.1:
93099317
spdx-correct "^3.0.0"
93109318
spdx-expression-parse "^3.0.0"
93119319

9312-
vary@~1.1.2:
9320+
vary@^1, vary@~1.1.2:
93139321
version "1.1.2"
93149322
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
93159323

0 commit comments

Comments
 (0)