shakacode / react_on_rails

Integration of React + Webpack + Rails + rails/webpacker including server-side rendering of React, enabling a better developer experience and faster client performance.
https://www.shakacode.com/react-on-rails/docs/
MIT License
5.12k stars 631 forks source link

Add support for streaming rendered components using `renderToPipeableStream` #1633

Closed AbanoubGhadban closed 4 weeks ago

AbanoubGhadban commented 5 months ago

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

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


This change is Reviewable

coderabbitai[bot] commented 5 months ago

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

Suggested reviewers

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 - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
🪧 Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit , 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](https://docs.coderabbit.ai/guides/configure-coderabbit) 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](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
AbanoubGhadban commented 4 months ago
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

justin808 commented 3 months ago

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