Home / File/ render-to-buffer.js — react Source File

render-to-buffer.js — react Source File

Architecture documentation for render-to-buffer.js, a javascript file in the react codebase. 6 imports, 0 dependents.

File javascript BabelCompiler Entrypoint 6 imports 5 functions

Entity Profile

Dependency Diagram

graph LR
  fbe0797f_1878_6a2d_3c20_5ad62120856d["render-to-buffer.js"]
  80a4d9b0_ea35_ec8f_4f21_8a2085be082e["App.js"]
  fbe0797f_1878_6a2d_3c20_5ad62120856d --> 80a4d9b0_ea35_ec8f_4f21_8a2085be082e
  0ed5d49a_8ea3_0b80_de56_5d5e1cbc4e72["App"]
  fbe0797f_1878_6a2d_3c20_5ad62120856d --> 0ed5d49a_8ea3_0b80_de56_5d5e1cbc4e72
  ea6c61ad_b555_8796_c3e4_a78a8f5c0ffc["delays.js"]
  fbe0797f_1878_6a2d_3c20_5ad62120856d --> ea6c61ad_b555_8796_c3e4_a78a8f5c0ffc
  84cea5a1_779b_9551_2f54_df77f0973a0f["stream"]
  fbe0797f_1878_6a2d_3c20_5ad62120856d --> 84cea5a1_779b_9551_2f54_df77f0973a0f
  ac587885_e294_a1e9_b13f_5e7b920fdb42["react"]
  fbe0797f_1878_6a2d_3c20_5ad62120856d --> ac587885_e294_a1e9_b13f_5e7b920fdb42
  860fcd5f_4dd1_15af_0c6d_0894e4c5dffb["server"]
  fbe0797f_1878_6a2d_3c20_5ad62120856d --> 860fcd5f_4dd1_15af_0c6d_0894e4c5dffb
  style fbe0797f_1878_6a2d_3c20_5ad62120856d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */

import {Writable} from 'stream';
import * as React from 'react';
import {renderToPipeableStream} from 'react-dom/server';
import App from '../src/App';
import {ABORT_DELAY} from './delays';

// In a real setup, you'd read it from webpack build stats.
let assets = {
  'main.js': '/main.js',
  'main.css': '/main.css',
};

function HtmlWritable(options) {
  Writable.call(this, options);
  this.chunks = [];
  this.html = '';
}

HtmlWritable.prototype = Object.create(Writable.prototype);
HtmlWritable.prototype.getHtml = function getHtml() {
  return this.html;
};
HtmlWritable.prototype._write = function _write(chunk, encoding, callback) {
  this.chunks.push(chunk);
  callback();
};
HtmlWritable.prototype._final = function _final(callback) {
  this.html = Buffer.concat(this.chunks).toString();
  callback();
};

module.exports = function render(url, res) {
  let writable = new HtmlWritable();
  res.socket.on('error', error => {
    console.error('Fatal', error);
  });
  let didError = false;
  let didFinish = false;

  writable.on('finish', () => {
    // If something errored before we started streaming, we set the error code appropriately.
    res.statusCode = didError ? 500 : 200;
    res.setHeader('Content-type', 'text/html');
    res.send(writable.getHtml());
  });

  const {pipe, abort} = renderToPipeableStream(<App assets={assets} />, {
    bootstrapScripts: [assets['main.js']],
    onAllReady() {
      // Full completion.
      // You can use this for SSG or crawlers.
      didFinish = true;
    },
    onShellReady() {
      // If something errored before we started streaming, we set the error code appropriately.
      pipe(writable);
    },
    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 if enough time passes.
  // Try lowering this to see the client recover.
  setTimeout(() => {
    if (!didFinish) {
      abort();
    }
  }, ABORT_DELAY);
};

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does render-to-buffer.js do?
render-to-buffer.js is a source file in the react codebase, written in javascript. It belongs to the BabelCompiler domain, Entrypoint subdomain.
What functions are defined in render-to-buffer.js?
render-to-buffer.js defines 5 function(s): HtmlWritable, _final, _write, getHtml, module.
What does render-to-buffer.js depend on?
render-to-buffer.js imports 6 module(s): App, App.js, delays.js, react, server, stream.
Where is render-to-buffer.js in the architecture?
render-to-buffer.js is located at fixtures/fizz/server/render-to-buffer.js (domain: BabelCompiler, subdomain: Entrypoint, directory: fixtures/fizz/server).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free