react_on_rails icon indicating copy to clipboard operation
react_on_rails copied to clipboard

Add support for streaming rendered components using `renderToPipeableStream`

Open AbanoubGhadban opened this issue 1 year ago β€’ 3 comments

Summary

This PR introduces the new features introduced in React 18 which is supporting Async Components and streaming components. It's done by replacing renderToString function with renderToPipeableStream. https://www.loom.com/share/db55036514b746a7b283e6f296134168?sid=2ce8205d-6956-4e9a-97bc-ee55004f3216

The Dummy App

To open the newly introduced page in the dummy app, you need to clone both React on Rails and React on Rails Pro on your local machine and put them in the same directory

  • <parent_directory>
    • react_on_rails
    • react_on_rails_pro

execute the following commands

cd <parent_directory>
cd react_on_rails
bundle && yarn
cd ../react_on_rails_pro
bundle && yarn
cd spec/dummy
bundle && yarn

bin/dev

Then, open the newly introduced stream_async_components in the dummy app.

More resources

https://react.dev/reference/react-dom/server/renderToPipeableStream https://github.com/reactwg/react-18/discussions/37

Sequence Diagrams

sequenceDiagram
    participant Browser
    participant RailsServer
    participant ReactOnRails (Ruby)
    participant ReactOnRailsPro (JS)
    participant ReactDOMServer
    Browser->>RailsServer: Request page with React component
    RailsServer->>ReactOnRails (Ruby): Call `stream_react_component`
    ReactOnRails (Ruby)->>ReactOnRailsPro (JS): Trigger `streamServerRenderedReactComponent`
    ReactOnRails (JS)->>ReactDOMServer: Call `renderToPipeableStream`
    ReactDOMServer-->>ReactOnRailsPro (JS): PipeableStream
    ReactOnRails (JS)-->>ReactOnRails (Ruby): Stream content
    ReactOnRails (Ruby)-->>RailsServer: Streamed React component content
    RailsServer-->>Browser: Serve content
    Note right of Browser: Displays page with streamed React component

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced streaming functionality for React components, enabling server-side rendering with enhanced performance and better error management.
    • Added methods for asynchronous streaming of React components and improved output formatting in the React on Rails helper module.
    • Implemented new functions to manage server-side rendering of React components to a stream.
    • Enhanced Webpack configuration to support Node.js globals and added new aliases for better compatibility.
    • Added a new method for handling JavaScript rendering with streaming capabilities.
    • Introduced a new method for accessing streaming options in the rendering process.
  • Bug Fixes

    • Improved error handling during rendering for increased reliability.
  • Updates

    • Upgraded React and React-DOM versions to 18.2.0 for improved support and additional features.
    • Updated test setup to ensure compatibility with jsdom environment, including a new polyfill for compatibility issues.
    • Improved version control cleanliness by ignoring IDE-related files and directories.

This change is Reviewable

AbanoubGhadban avatar Jun 17 '24 14:06 AbanoubGhadban

Walkthrough

This update enhances the react_on_rails library by introducing robust server-side streaming capabilities for React components. Significant new methods have been added in both Ruby and JavaScript to facilitate asynchronous rendering, improve error handling, and ensure compatibility with the latest package versions. Additionally, the Webpack configuration has been adjusted to support these features, and various test setups have been refined.

Changes

Files/Paths Change Summary
jest.config.js Added setupFiles property for test suite configuration.
lib/react_on_rails/helper.rb Added streaming methods for React components and refactored existing methods for clarity and functionality.
lib/react_on_rails/react_component/render_options.rb, lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb Added new methods for streaming options and JavaScript rendering capabilities.
node_package/src/ReactOnRails.ts, node_package/src/types/index.ts, node_package/src/serverRenderReactComponent.ts Introduced streamServerRenderedReactComponent for server-side React streaming; updated interface for new functionality.
node_package/tests/ReactOnRails.test.js Improved test setup for rendering checks.
node_package/tests/jest.setup.js Introduced a polyfill for TextEncoder and TextDecoder.
package.json Upgraded react and react-dom versions to 18.2.0.
spec/dummy/config/webpack/alias.js, spec/dummy/config/webpack/commonWebpackConfig.js, spec/dummy/config/webpack/webpackConfig.js Configured webpack to support streaming with stream-browserify and added Node.js globals compatibility.
spec/dummy/spec/helpers/react_on_rails_helper_spec.rb Added tests for rails_context_if_not_already_rendered method to ensure correct behavior and output.

Possibly related PRs

  • #1620: The main PR introduces a new property setupFiles in jest.config.js, which is related to the configuration of test setups. This is relevant to the changes in the lib/react_on_rails/helper.rb file, where the loading behavior of React components is enhanced, potentially impacting how tests are set up and executed.
  • #1653: The refactor of the serverRenderReactComponent function in this PR may relate to the changes in the main PR as both involve modifications to the server rendering logic, which could affect how tests are structured and executed in relation to server-rendered components.

Suggested reviewers

  • alexeyr-ci: Suggested for review due to expertise in the react_on_rails library.
  • justin808: Suggested for review due to familiarity with the react_on_rails library.
  • Judahmeek: Suggested for review given experience with server-side rendering implementations.

Poem

In the garden where bytes flow,
A new feature begins to grow.
Streaming React with seamless grace,
Swiftly dancing in data's embrace.
Rails and JavaScript, hand in hand,
Server to client, perfectly planned.
πŸŒ±πŸš€πŸŽ‰


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❀️ Share
πŸͺ§ Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Jun 17 '24 14:06 coderabbitai[bot]

Would it make sense to add async: false parameter to def stream_react_component instead of a separate function (probably not, but asking just in case)?

The stream_react_component_async is renamed now to stream_react_component. It's async by default. The other function is removed

AbanoubGhadban avatar Jul 28 '24 14:07 AbanoubGhadban

@AbanoubGhadban let's see if this is ready for merge and pre-release.

justin808 avatar Aug 21 '24 00:08 justin808