mirror of
https://github.com/facebook/react.git
synced 2026-02-23 20:23:02 +00:00
* I forgot to call onFatalError I can't figure out how to write a test for this because it only happens when there is a bug in React itself which would then be fixed if we found it. We're also covered by the protection of ReadableStream which doesn't leak other errors to us. * Abort requests if the reader cancels No need to continue computing at this point. * Abort requests if node streams get destroyed This is if the downstream cancels is for example. * Rename Node APIs for Parity with allReady The "Complete" terminology is a little misleading because not everything has been written yet. It's just "Ready" to be written now. onShellReady onShellError onAllReady * 'close' should be enough
45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
import React from 'react';
|
|
import {renderToPipeableStream} from 'react-dom/server';
|
|
|
|
import App from '../src/components/App';
|
|
|
|
let assets;
|
|
if (process.env.NODE_ENV === 'development') {
|
|
// Use the bundle from create-react-app's server in development mode.
|
|
assets = {
|
|
'main.js': '/static/js/bundle.js',
|
|
'main.css': '',
|
|
};
|
|
} else {
|
|
assets = require('../build/asset-manifest.json');
|
|
}
|
|
|
|
export default function render(url, res) {
|
|
res.socket.on('error', error => {
|
|
// Log fatal errors
|
|
console.error('Fatal', error);
|
|
});
|
|
let didError = false;
|
|
const {pipe, abort} = renderToPipeableStream(<App assets={assets} />, {
|
|
bootstrapScripts: [assets['main.js']],
|
|
onShellReady() {
|
|
// If something errored before we started streaming, we set the error code appropriately.
|
|
res.statusCode = didError ? 500 : 200;
|
|
res.setHeader('Content-type', 'text/html');
|
|
pipe(res);
|
|
},
|
|
onShellError(x) {
|
|
// Something errored before we could complete the shell so we emit an alternative shell.
|
|
res.statusCode = 500;
|
|
res.send('<!doctype><p>Error</p>');
|
|
},
|
|
onError(x) {
|
|
didError = true;
|
|
console.error(x);
|
|
},
|
|
});
|
|
// Abandon and switch to client rendering after 5 seconds.
|
|
// Try lowering this to see the client recover.
|
|
setTimeout(abort, 5000);
|
|
}
|