Skip to content

Commit

Permalink
[Flight] End-to-End Fixture (#17319)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored Nov 9, 2019
1 parent 6cb6b1d commit 182f64f
Show file tree
Hide file tree
Showing 9 changed files with 10,031 additions and 0 deletions.
1 change: 1 addition & 0 deletions fixtures/flight/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SKIP_PREFLIGHT_CHECK=true
23 changes: 23 additions & 0 deletions fixtures/flight/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
37 changes: 37 additions & 0 deletions fixtures/flight/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "flight",
"version": "0.1.0",
"private": true,
"dependencies": {
"@babel/register": "^7.7.0",
"concurrently": "^5.0.0",
"express": "^4.17.1",
"react-scripts": "3.2.0"
},
"scripts": {
"prestart": "cp -r ../../build/node_modules/* ./node_modules/",
"prebuild": "cp -r ../../build/node_modules/* ./node_modules/",
"start": "concurrently \"npm run start:server\" \"npm run start:client\"",
"start:client": "react-scripts start",
"start:server": "NODE_ENV=development node server",
"start:prod": "react-scripts build && NODE_ENV=production node server",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
11 changes: 11 additions & 0 deletions fixtures/flight/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Flight</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
28 changes: 28 additions & 0 deletions fixtures/flight/server/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const ReactFlightDOMServer = require('react-dom/unstable-flight-server');
const React = require('react');
const Stream = require('stream');

function Text({children}) {
return <span>{children}</span>;
}

function HTML() {
return (
<div>
<Text>Hello</Text>
<Text>world</Text>
</div>
);
}

module.exports = function(req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
let model = {
content: {
__html: <HTML />,
},
};
ReactFlightDOMServer.pipeToNodeWritable(model, res);
};
46 changes: 46 additions & 0 deletions fixtures/flight/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const babelRegister = require('@babel/register');

babelRegister({
ignore: [/\/(build|node_modules)\//],
presets: ['react-app'],
});

const express = require('express');
const app = express();

// Application
app.get('/', function(req, res) {
if (process.env.NODE_ENV === 'development') {
for (var key in require.cache) {
delete require.cache[key];
}
}
require('./handler')(req, res);
});

app.listen(3001, () => {
console.log('Flight Server listening on port 3001...');
});

app.on('error', function(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;

switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
});
15 changes: 15 additions & 0 deletions fixtures/flight/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React, {Suspense} from 'react';

function Content({data}) {
return <p dangerouslySetInnerHTML={data.model.content} />;
}

function App({data}) {
return (
<Suspense fallback={<h1>Loading...</h1>}>
<Content data={data} />
</Suspense>
);
}

export default App;
7 changes: 7 additions & 0 deletions fixtures/flight/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';
import ReactFlightDOMClient from 'react-dom/unstable-flight-client';
import App from './App';

let data = ReactFlightDOMClient.readFromFetch(fetch('http://localhost:3001'));
ReactDOM.render(<App data={data} />, document.getElementById('root'));
Loading

0 comments on commit 182f64f

Please sign in to comment.