| 45 | } |
| 46 | |
| 47 | export default function render(url, res) { |
| 48 | res.socket.on('error', error => { |
| 49 | // Log fatal errors |
| 50 | console.error('Fatal', error); |
| 51 | }); |
| 52 | let didError = false; |
| 53 | const {pipe, abort} = renderToPipeableStream(<App assets={assets} />, { |
| 54 | bootstrapScripts: [assets['main.js']], |
| 55 | progressiveChunkSize: 1024, |
| 56 | onShellReady() { |
| 57 | // If something errored before we started streaming, we set the error code appropriately. |
| 58 | res.statusCode = didError ? 500 : 200; |
| 59 | res.setHeader('Content-type', 'text/html'); |
| 60 | // To test the actual chunks taking time to load over the network, we throttle |
| 61 | // the stream a bit. |
| 62 | const throttledResponse = new ThrottledWritable(res); |
| 63 | pipe(throttledResponse); |
| 64 | }, |
| 65 | onShellError(x) { |
| 66 | // Something errored before we could complete the shell so we emit an alternative shell. |
| 67 | res.statusCode = 500; |
| 68 | res.send('<!doctype><p>Error</p>'); |
| 69 | }, |
| 70 | onError(x) { |
| 71 | didError = true; |
| 72 | console.error(x); |
| 73 | }, |
| 74 | }); |
| 75 | // Abandon and switch to client rendering after 5 seconds. |
| 76 | // Try lowering this to see the client recover. |
| 77 | setTimeout(abort, 5000); |
| 78 | } |