Skip to content

Use RSC payload to render server components on server #1696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 31 commits into
base: master
Choose a base branch
from

Conversation

AbanoubGhadban
Copy link
Collaborator

@AbanoubGhadban AbanoubGhadban commented Feb 10, 2025

Using RSC Payload to Render Server Components on Server

Overview

This PR implements a significant architectural improvement to the React Server Components (RSC) rendering flow in both react_on_rails and react_on_rails_pro. Previously, server components were rendered twice - once for HTML generation and again for RSC payload. This PR unifies the process by generating the RSC payload first and using it for server-side rendering, eliminating double rendering and reducing HTTP requests.

Previous Rendering Flow

Before this improvement, the React Server Components rendering process worked as follows:

  1. Initial HTML Generation:

    • The stream_react_component helper is called in the view
    • Makes a request to the node renderer
    • Renderer uses the Server Bundle to generate HTML for all components
    • HTML is streamed to the client
  2. RSC Payload Generation:

    • Browser shows the initial html
    • Browser makes a separate fetch request to the RSC payload URL
    • Calls rsc_payload_react_component on the server
    • Node renderer uses the RSC Bundle to generate the RSC payload
    • Server components are rendered and serialized
    • Client components are included as references
  3. Client Hydration:

    • RSC payload is processed by React runtime
    • Client component chunks are fetched based on references
    • Components are hydrated progressively

This approach had two main inefficiencies:

  1. Double Rendering: Server components are rendered twice:

    • Once for HTML generation using the server bundle
    • Again for RSC payload generation using the RSC bundle
  2. Multiple Requests: Requires two separate HTTP requests:

    • Initial request for HTML
    • Secondary request for RSC payload
sequenceDiagram
    participant Browser
    participant View
    participant NodeRenderer
    participant ServerBundle
    participant RSCBundle
    
    Note over Browser,RSCBundle: 1. Initial HTML Generation
    Browser->>View: Request page
    View->>NodeRenderer: stream_react_component
    NodeRenderer->>ServerBundle: Generate HTML
    ServerBundle-->>NodeRenderer: HTML for all components
    NodeRenderer-->>Browser: Stream HTML
    
    Note over Browser,RSCBundle: 2. RSC Payload Generation
    Browser->>NodeRenderer: Fetch RSC payload
    NodeRenderer->>RSCBundle: rsc_payload_react_component
    RSCBundle-->>NodeRenderer: RSC payload with:<br/>- Server components<br/>- Client component refs
    NodeRenderer-->>Browser: Stream RSC payload
    
    Note over Browser: 3. Client Hydration
    Browser->>Browser: Process RSC payload
    loop For each client component
        Browser->>Browser: Fetch component chunk
        Browser->>Browser: Hydrate component
    end
Loading

Note

For simplicity, this diagram shows the RSC payload being fetched after the HTML is fully streamed to the client. In reality, the browser begins fetching the RSC payload and starts hydration immediately as soon as it receives the necessary HTML, without waiting for the complete page to be streamed. This parallel processing enables faster page interactivity and better performance.

Key Improvements

  1. Elimination of Double Rendering: Server components are now rendered only once - in the RSC bundle - and the resulting payload is used by both server and client.
  2. Single HTTP Request: The client no longer needs to make a separate request for the RSC payload, as it's embedded in the initial HTML response.
  3. Improved Performance: Reduced server load and faster page interactivity by eliminating redundant rendering and requests.

Technical Implementation

Inter-Bundle Communication

The core of this implementation is a mechanism for communication between bundles in the node renderer:

  • Introduced runOnOtherBundle function in the VM context that allows executing code in a different bundle
  • Added generateRSCPayload function that uses runOnOtherBundle to run rendering requests in the RSC bundle
// Simplified representation of the flow
globalThis.generateRSCPayload = function generateRSCPayload(componentName, props, railsContext) {
  const { renderingRequest, rscBundleHash } = railsContext.serverSideRSCPayloadParameters;
  const propsString = JSON.stringify(props);
  const newRenderingRequest = renderingRequest.replace(/\(\s*\)\s*$/, `('${componentName}', ${propsString})`);
  return runOnOtherBundle(rscBundleHash, newRenderingRequest);
}

Key Components for RSC Rendering

Two new components are introduced in React on Rails to facilitate server-side RSC rendering:

RSCServerRoot

RSCServerRoot is the core component responsible for:

  • Requesting the RSC payload from the RSC bundle via the generateRSCPayload function
  • Creating server component elements using the RSC payload
  • Rendering both the server component HTML and embedding the RSC payload for client hydration

This component operates as follows:

  1. Receives the component name and props to render
  2. Creates an SSR manifest by merging client and server manifests
  3. Fetches the RSC payload stream for the component
  4. Splits the stream to use for both rendering and embedding
  5. Renders the server component on the server using the RSC payload
  6. Returns a React fragment containing both the rendered component and the RSCPayloadContainer

RSCPayloadContainer

RSCPayloadContainer handles embedding the RSC payload within the HTML response by:

  • Accepting an RSC payload stream as input
  • Processing the stream chunks progressively
  • Creating script elements that inject the payload into self.REACT_ON_RAILS_RSC_PAYLOAD
  • Suspending rendering until new chunks arrive, enabling streaming

This creates a seamless experience where:

  • Server components render immediately on the server
  • RSC payload is embedded directly in the HTML response
  • Client can hydrate components progressively without a separate request

New Rendering Flow

  1. When stream_react_component is called:

    • The server bundle rendering function calls generateRSCPayload with the component name and props
    • This executes the component rendering in the RSC bundle
    • RSC bundle generates the payload containing server component data and client component references
    • The payload is returned to the server bundle
  2. The server bundle then:

    • Uses the RSC payload to generate HTML for server components
    • Renders client components normally
    • Embeds the RSC payload within the HTML response
    • Streams the result to the client
  3. On the client:

    • HTML is displayed immediately
    • React runtime uses the embedded RSC payload for hydration
    • Client components are hydrated progressively

Configuration and Detection

  • Added conditional rendering function selection based on bundle type:
ReactOnRails[ReactOnRails.isRSCBundle ? 'serverRenderRSCReactComponent' : 'streamServerRenderedReactComponent']
  • RSC bundles now expose ReactOnRails.isRSCBundle flag to identify their special purpose
  • Server bundles automatically detect and use RSC payload when available

Visual Representation

sequenceDiagram
    participant Browser
    participant View
    participant NodeRenderer
    participant RSCBundle
    participant ServerBundle
    
    Note over Browser,ServerBundle: 1. Initial Request
    Browser->>View: Request page
    View->>NodeRenderer: stream_react_component
    NodeRenderer->>ServerBundle: Execute rendering request
    ServerBundle->>RSCBundle: generateRSCPayload(component, props)
    RSCBundle-->>ServerBundle: RSC payload with:<br/>- Server components<br/>- Client component refs
    ServerBundle-->>NodeRenderer: Generate HTML using RSC payload
    
    Note over Browser,ServerBundle: 2. Single Response
    NodeRenderer-->>Browser: Stream HTML with embedded RSC payload
    
    Note over Browser: 3. Client Hydration
    Browser->>Browser: Process embedded RSC payload
    loop For each client component
        Browser->>Browser: Fetch component chunk
        Browser->>Browser: Hydrate component
    end
Loading

Testing

  • Added tests for the serverRenderRSCReactComponent function
  • Verified correct payload generation and embedding
  • Ensured proper hydration of client components using the embedded payload
  • Confirmed reduced network requests and rendering time

Performance Improvements

  • Server Load: Reduced overhead of rendering server components twice
  • Network Traffic: Reduced by eliminating separate RSC payload requests.
  • Time to Interactive: Improved by streamlining the rendering and hydration process

Pull Request checklist

Remove this line after checking all the items here. If the item is not applicable to the PR, both check it out and wrap it by ~.

  • Add/update test to cover these changes
  • Update documentation
  • Update CHANGELOG file
    Add the CHANGELOG entry at the top of the file.

Other Information

Remove this paragraph and mention any other important and relevant information such as benchmarks.


This change is Reviewable

Summary by CodeRabbit

  • New Features

    • Added support for streaming React Server Component (RSC) payloads from server to client, improving server-side rendering and hydration.
    • Introduced components for handling RSC payloads and embedding them directly into pages.
    • Added configuration options for managing server and client manifest files for RSC.
    • Introduced registration methods for React Server Components with optimized server bundle handling.
  • Improvements

    • Enhanced RSC rendering flow to eliminate double rendering and reduce HTTP requests.
    • Upgraded the protocol between Node Renderer and Rails to support multiple bundle uploads.
    • Improved error handling and manifest management for server and client components.
    • Refined console replay handling and stream transformation for better client-side logging and stream processing.
    • Enhanced React Server Components hydration to support streaming from preloaded payloads.
  • Bug Fixes

    • Fixed issues with error state handling during server rendering.
  • Documentation

    • Updated changelog and in-code documentation to reflect new RSC features and usage.
  • Chores

    • Updated dependencies and configuration files for better compatibility and development workflow.

Copy link
Contributor

coderabbitai bot commented Feb 10, 2025

Walkthrough

This update introduces significant enhancements to React Server Components (RSC) support in the React on Rails ecosystem. Key changes include new configuration options and constants for managing RSC manifests, new utilities for loading and handling JSON manifest files, and the addition of specialized server/client registration and rendering logic for RSC. The rendering flow is refined to support streaming payloads, reduce redundant HTTP requests, and enable multiple bundles. New components and helper functions are added to facilitate the seamless transfer and hydration of RSC payloads between server and client, with updates to type definitions, package exports, and build configurations.

Changes

File(s) Change Summary
lib/react_on_rails/configuration.rb, lib/react_on_rails/test_helper/webpack_assets_status_checker.rb, lib/react_on_rails/utils.rb Added new configuration constant and attribute for RSC manifest, updated initialization and file existence checks, and introduced a utility for retrieving the RSC manifest file path. Updated asset checker to recognize the new manifest.
node_package/src/ReactOnRails.client.ts, node_package/src/ReactOnRailsRSC.ts, node_package/src/types/index.ts Added isRSCBundle property to indicate RSC bundle usage; updated RailsContext type to a discriminated union with new RSC-related fields.
node_package/src/loadJsonFile.ts, node_package/src/loadReactClientManifest.ts Replaced the manifest loader with a new generic JSON file loader, removing the old specialized loader.
node_package/src/registerServerComponent/server.ts, node_package/src/registerServerComponent/server.rsc.ts Split server component registration logic: server bundle now wraps components with RSCServerRoot, while RSC bundle registers components directly. Added new RSC-specific registration file.
node_package/src/serverRenderReactComponent.ts, node_package/src/buildConsoleReplay.ts Improved error handling and console replay logic to ensure correct state and avoid empty script tags.
node_package/src/streamServerRenderedReactComponent.ts Refactored stream transformation to separate and schedule console replay chunk emission, added a flush method for timely completion.
node_package/src/RSCClientRoot.ts Overhauled client RSC hydration: added streaming support from a global payload array, conditional fetch fallback, and ensured React 19 compatibility.
node_package/src/transformRSCStreamAndReplayConsoleLogs.ts Enhanced to handle streams of both Uint8Array and RSCPayloadChunk, refactored chunk handling for clarity and correctness.
node_package/src/RSCPayloadContainer.tsx Introduced a new component to stream and inject RSC payload chunks into the client, ensuring ordered delivery and robust error handling.
node_package/src/RSCServerRoot.tsx Added a new SSR component for server-side rendering of RSC, handling manifest mapping, stream teeing, and payload delivery.
node_package/src/transformRSCNodeStream.ts Added a utility to transform RSC node streams into streams of HTML for SSR.
node_package/src/reactApis.cts Added a utility to verify the presence of React's use API, ensuring compatibility with React 19+.
package.json Updated exports to support both server and RSC bundles, and switched dependency to a development branch of react-on-rails-rsc.
lib/react_on_rails/helper.rb Extended Rails context to include RSC payload generation URL when RSC support is enabled.
knip.ts Marked the new RSC server registration file as a production dependency.
eslint.config.ts Updated ESLint config to ignore .yalc directories, support .tsx extensions, and enforce JSX file extension rules.
CHANGELOG.md Documented the new and improved RSC rendering flow, protocol upgrades, and new components.

Sequence Diagram(s)

sequenceDiagram
    participant Rails
    participant NodeRenderer
    participant RSCServerRoot
    participant RSCPayloadContainer
    participant Client

    Rails->>NodeRenderer: Request SSR for RSC component
    NodeRenderer->>RSCServerRoot: Render component with props & context
    RSCServerRoot->>NodeRenderer: Load server/client manifests
    RSCServerRoot->>NodeRenderer: generateRSCPayload (returns stream)
    RSCServerRoot->>RSCPayloadContainer: Tee stream for payload delivery
    RSCServerRoot->>NodeRenderer: Render component from stream
    NodeRenderer->>Rails: Return HTML + script tags for RSC payload
    Rails->>Client: Serve HTML page with embedded RSC payload
    Client->>RSCClientRoot: Hydrate using embedded payload or fetch as fallback
Loading

Possibly related PRs

  • shakacode/react_on_rails#1644: Introduces and enhances support for React Server Components (RSC), including configuration, helpers, and registration logic—directly related to this PR's changes.
  • shakacode/react_on_rails#1649: Updates console log replay functionality in server rendering, which aligns with the improvements to console replay logic in this PR.

Suggested reviewers

  • Judahmeek
  • justin808

Poem

🐇
A hop, a leap, the server streams,
RSC payloads flow in beams.
Manifests mapped, the context set,
Streams and chunks in order met.
With roots and containers, logs replayed,
React and Rails, together arrayed.
The garden grows—new features bloom,
As RSCs now truly zoom!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c919b1d and 029c273.

📒 Files selected for processing (1)
  • node_package/src/registerServerComponent/server.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • node_package/src/registerServerComponent/server.ts
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: dummy-app-integration-tests (newest)
  • GitHub Check: dummy-app-integration-tests (oldest)
  • GitHub Check: build
  • GitHub Check: rspec-package-tests (oldest)
  • GitHub Check: rspec-package-tests (newest)
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

@justin808
Copy link
Member

@AbanoubGhadban can you update the PR description?

@AbanoubGhadban AbanoubGhadban force-pushed the abanoubghadban/pro362-add-support-for-RSC branch from 9e49cb6 to b0173f1 Compare March 4, 2025 09:36
Base automatically changed from abanoubghadban/pro362-add-support-for-RSC to master March 7, 2025 23:42
@AbanoubGhadban
Copy link
Collaborator Author

@AbanoubGhadban can you update the PR description?

@justin808 Sure, I updated it

@AbanoubGhadban AbanoubGhadban force-pushed the abanoubghadban/pro465/use-rsc-payload-to-render-server-components-on-server branch from 316958d to 5c99bc4 Compare March 19, 2025 19:35
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (8)
node_package/src/loadJsonFile.ts (1)

1-22: Well-implemented utility for loading JSON files

This utility function is a good addition that provides caching for loaded JSON files, preventing redundant disk reads. The implementation includes proper error handling, logging, and follows async/await patterns correctly.

There is one optimization worth considering:

async function loadJsonFile(fileName: string) {
  // Asset JSON files are uploaded to node renderer.
  // Renderer copies assets to the same place as the server-bundle.js and rsc-bundle.js.
  // Thus, the __dirname of this code is where we can find the manifest file.
  const filePath = path.resolve(__dirname, fileName);
  if (!loadedJsonFiles.has(filePath)) {
    try {
      const file = JSON.parse(await fs.readFile(filePath, 'utf8'));
      loadedJsonFiles.set(filePath, file);
    } catch (error) {
-      console.error(`Failed to load JSON file: ${filePath}`, error);
+      const errorMessage = `Failed to load JSON file: ${filePath}`;
+      console.error(errorMessage, error);
-      throw error;
+      throw new Error(errorMessage, { cause: error });
    }
  }

  return loadedJsonFiles.get(filePath)!;
}

This change would provide a more descriptive error message while preserving the original error as the cause, making debugging easier.

node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts (1)

3-35: Improve error handling and resource management in stream transformation

While the core transformation logic is solid, there are a few concerns with the implementation:

  1. The error handling stringifies the entire stream (JSON.stringify(stream) on line 33), which could be problematic for large streams or potentially expose sensitive data.
  2. There's no cleanup for the decoder or transform stream if an error occurs.
  3. The function assumes a specific format (JSON with an html property) without validation.

Consider these improvements:

export default function transformRSCStream(stream: NodeJS.ReadableStream): NodeJS.ReadableStream {
  const decoder = new TextDecoder();
  let lastIncompleteChunk = '';

  const htmlExtractor = new Transform({
    transform(oneOrMoreChunks, _, callback) {
      try {
        const decodedChunk = lastIncompleteChunk + decoder.decode(oneOrMoreChunks);
        const separateChunks = decodedChunk.split('\n').filter(chunk => chunk.trim() !== '');

        if (!decodedChunk.endsWith('\n')) {
          lastIncompleteChunk = separateChunks.pop() ?? '';
        } else {
          lastIncompleteChunk = '';
        }

        for (const chunk of separateChunks) {
          const parsedData = JSON.parse(chunk) as { html: string };
+         if (!parsedData || typeof parsedData.html !== 'string') {
+           throw new Error(`Invalid RSC chunk format: ${chunk.substring(0, 100)}${chunk.length > 100 ? '...' : ''}`);
+         }
          this.push(parsedData.html);
        }
        callback();
      } catch (error) {
        callback(error as Error);
      }
    }
  });

  try {
    return stream.pipe(htmlExtractor);
  } catch (error) {
-   throw new Error(`Error transforming RSC stream (${stream.constructor.name}), (stream: ${stream}), stringified stream: ${JSON.stringify(stream)}, error: ${error}`);
+   throw new Error(`Error transforming RSC stream (${stream.constructor.name}), error: ${error}`);
  }
}
package.json (1)

57-57: Consider using versioned dependencies instead of branch references

While using a specific branch works for development, it's generally better to use versioned dependencies for stability. Consider publishing a versioned release of the RSC package once the feature is stable.

Also applies to: 65-65

node_package/src/createReactOutput.ts (1)

8-26: Good refactoring of React element creation logic

Extracting the element creation logic into a separate function improves code organization and reusability. The function correctly handles both direct React elements and components that need to be wrapped.

However, the warning message mentions "ReactOnRails v13 will throw error on this", but according to package.json, the current version is 15.0.0-alpha.2. Consider updating this message.

-incorrectly returned a React Element (JSX). Instead, return a React Function Component by
-wrapping your JSX in a function. ReactOnRails v13 will throw error on this, as React Hooks do not
+incorrectly returned a React Element (JSX). Instead, return a React Function Component by
+wrapping your JSX in a function. This may cause errors with React Hooks, as they do not
node_package/src/ReactOnRailsRSC.ts (1)

27-31: Enhance error message clarity
When the Rails context is missing critical fields, it might help to specifically mention which fields are missing rather than displaying a generic "Rails context is not available."

-    throw new Error('Rails context is not available');
+    throw new Error('Rails context is missing either `serverSide` or `reactClientManifestFileName`.');
node_package/src/RSCServerRoot.ts (3)

7-13: Evaluate the global generateRSCPayload declaration
Declaring a global could conflict in certain bundling scenarios or mock contexts. Consider scoping it under a namespace or module export if collisions are a concern.


31-57: Consider making the prefix configurable
Currently, /webpack/development/ is hardcoded. If you plan to run in multiple environments (e.g., production), a configurable prefix would be more flexible.


60-65: Inconsistent naming in error message
The code checks railsContext.reactClientManifestFileName and railsContext.reactServerClientManifestFileName but the message mentions serverClientManifestFileName and reactServerClientManifestFileName. Clarify or rename for accuracy.

-  `${'serverClientManifestFileName and reactServerClientManifestFileName are required. ' +
+  `${'reactClientManifestFileName and reactServerClientManifestFileName are required. ' +
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 79b4941 and 5c99bc4.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (19)
  • lib/react_on_rails/configuration.rb (6 hunks)
  • lib/react_on_rails/test_helper/webpack_assets_status_checker.rb (1 hunks)
  • lib/react_on_rails/utils.rb (1 hunks)
  • node_package/src/RSCServerRoot.ts (1 hunks)
  • node_package/src/ReactOnRails.client.ts (1 hunks)
  • node_package/src/ReactOnRailsRSC.ts (3 hunks)
  • node_package/src/createReactOutput.ts (2 hunks)
  • node_package/src/handleError.ts (1 hunks)
  • node_package/src/loadJsonFile.ts (1 hunks)
  • node_package/src/loadReactClientManifest.ts (0 hunks)
  • node_package/src/registerServerComponent/server.rsc.ts (1 hunks)
  • node_package/src/registerServerComponent/server.ts (2 hunks)
  • node_package/src/serverRenderReactComponent.ts (2 hunks)
  • node_package/src/streamServerRenderedReactComponent.ts (4 hunks)
  • node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts (1 hunks)
  • node_package/src/types/index.ts (4 hunks)
  • node_package/tests/serverRenderReactComponent.test.js (2 hunks)
  • node_package/tests/streamServerRenderedReactComponent.test.jsx (2 hunks)
  • package.json (2 hunks)
💤 Files with no reviewable changes (1)
  • node_package/src/loadReactClientManifest.ts
🧰 Additional context used
🧬 Code Definitions (3)
node_package/src/registerServerComponent/server.rsc.ts (1)
node_package/src/types/index.ts (3) (3)
  • ReactComponent (99-99)
  • RenderFunction (101-101)
  • ReactOnRails (179-214)
node_package/src/registerServerComponent/server.ts (2)
node_package/src/ComponentRegistry.ts (1) (1)
  • components (55-57)
node_package/src/types/index.ts (3) (3)
  • ReactComponent (99-99)
  • RenderFunction (101-101)
  • RailsContext (16-47)
node_package/src/ReactOnRailsRSC.ts (3)
node_package/src/types/index.ts (2) (2)
  • RSCRenderParams (143-145)
  • StreamRenderState (222-225)
node_package/src/streamServerRenderedReactComponent.ts (1) (1)
  • transformRenderStreamChunksToResultObject (86-121)
node_package/src/loadJsonFile.ts (1) (1)
  • loadJsonFile (6-22)
🔇 Additional comments (39)
node_package/src/ReactOnRails.client.ts (1)

334-335: Simple addition to support RSC bundles

This change adds a new isRSCBundle property to the ReactOnRails object, initialized to false. This flag helps identify whether the current bundle is being used as a React Server Components (RSC) bundle, which aligns with addressing the server components rendering twice issue mentioned in the PR objectives.

node_package/src/handleError.ts (1)

65-68: Good defensive programming to handle missing renderToString

This change adds a check to verify that ReactDOMServer.renderToString is a function before invoking it, preventing potential runtime errors in environments where the function might not be available. This is especially important for RSC environments where different rendering methods might be used.

lib/react_on_rails/test_helper/webpack_assets_status_checker.rb (1)

55-56: Properly extending asset checking to include server client manifest

This addition extends the all_compiled_assets method to handle the React server client manifest file, following the same pattern as the existing client manifest file handling. This change is consistent with the PR's goal of properly handling RSC payloads.

lib/react_on_rails/utils.rb (1)

125-132: LGTM: React server client manifest path implementation

The implementation follows the established pattern of other manifest file path methods. The comment clearly explains that the server manifest is generated by the server bundle and won't be served from the dev server, which justifies why it doesn't use the PackerUtils.using_packer? check that's present in the client manifest method.

node_package/src/registerServerComponent/server.rsc.ts (1)

1-26: Well-documented RSC component registration implementation

The function is simple but effective, delegating to ReactOnRails.register without additional wrapping. The documentation clearly explains the difference between this registration approach and server bundle registration, which helps developers understand when to use this function.

lib/react_on_rails/configuration.rb (6)

13-13: LGTM: Good constant naming convention

The constant name follows the same convention as the existing client manifest constant.


25-25: LGTM: Default configuration setup

The default configuration is set correctly using the defined constant.


70-70: LGTM: Attribute accessor addition

The attribute accessor is correctly added to the list, maintaining alphabetical ordering.


86-87: LGTM: Initialize method parameter addition

The parameter is correctly added to the initialize method signature, maintaining the pattern of other parameters.


117-117: LGTM: Configuration property assignment

The configuration property is correctly set in the initialize method.


269-275: LGTM: Webpack generated files setup

The modification to include the new manifest file in the webpack_generated_files array follows the same pattern as the other files.

node_package/tests/serverRenderReactComponent.test.js (2)

71-88: Clear test naming improvement

The test case renaming from "serverRenderReactComponent renders promises" to "serverRenderReactComponent renders promises that return strings" improves clarity by explicitly stating what type of promise result is being tested.


90-110: Good addition of test coverage for React component promises

This new test appropriately verifies that serverRenderReactComponent can handle promises that resolve to React components, which is an important capability for the RSC implementation. The test structure is clean and effectively validates both the rendering result and error handling.

node_package/src/registerServerComponent/server.ts (2)

2-14: Improved documentation and implementation for RSC support

The updated documentation clearly explains the new approach of wrapping components with RSCServerRoot to handle server-side rendering using pre-generated RSC payloads. This aligns well with the PR objective of reducing duplicate rendering of server components.


26-34: Well-structured implementation for RSC component wrapping

The implementation effectively wraps each component with RSCServerRoot, enabling the use of pre-generated RSC payloads. The updated function signature that accepts both ReactComponent and RenderFunction provides good flexibility.

package.json (1)

14-17: Good conditional exports configuration for server components

The updated exports configuration properly separates the server-side RSC implementation from the default implementation, which is essential for the React Server Components architecture.

node_package/src/createReactOutput.ts (2)

76-79: Enhanced promise handling for React components

The updated promise handling now correctly processes promises that resolve to either strings or React components, which is essential for supporting the RSC implementation. This change properly integrates with the new createReactElementFromRenderFunction function.


82-82: Simplified component wrapping with the new helper function

Using the extracted helper function here makes the code more consistent and easier to maintain.

node_package/src/ReactOnRailsRSC.ts (3)

18-18: Switching to loadJsonFile is a clean and scalable approach
This replacement for the previous manifest loader is straightforward, and the caching logic in loadJsonFile helps reduce repeated file reads.


41-41: Good use of Promise.all
Executing the manifest load and the render result in parallel is efficient. No concerns here.


77-78: Be mindful of globally setting ReactOnRails.isRSCBundle
If multiple bundles or configurations run simultaneously, globally toggling this flag could cause unintended side effects. Confirm it aligns with your usage across different environments.

node_package/tests/streamServerRenderedReactComponent.test.jsx (6)

57-91: Comprehensive registration logic for diverse component types
You’ve covered multiple scenarios using the componentType switch, ensuring tests can handle sync, async, and error-throwing variations. This adds valuable flexibility to the test suite.


190-206: Validating streamed components from promises
These tests confirm that both an asyncRenderFunction and a simple renderFunction produce valid stream outputs. The checks look thorough.


208-220: Good coverage of sync errors
Ensuring that synchronous errors within the render functions are handled correctly is crucial. These tests confirm correct error reporting.


222-238: Robust handling of async errors
The coverage here clearly verifies fallback logic when async errors occur, strengthening confidence in error-handling behavior.


240-258: Error simulation in erroneous render functions
This block cleanly demonstrates how errors originating within the render function (sync or async) are captured. Nicely done.


283-306: Excellent test for promise-resolved strings
This ensures the library can also stream raw HTML strings. Great addition to broaden the supported return types.

node_package/src/RSCServerRoot.ts (2)

20-22: Proactive React version check
This error clarifies the requirement for React’s new hooks. If the user accidentally uses an older version, they’ll get a direct message. Looks good overall.


59-88: Structured approach for RSC usage
Gathering manifests and constructing a React element from the generated payload stream is a clean design. No major concerns on concurrency or security.

node_package/src/types/index.ts (3)

16-47: Well-structured discriminated union for RailsContext

The refactoring of RailsContext into a discriminated union based on the serverSide property is well-designed. This pattern ensures type safety by requiring different properties depending on whether rendering happens on the client or server side. The extensive comments provide good context about the purpose of the RSC payload parameters.


60-62: Enhanced return type flexibility for rendering functions

The modified return types now correctly accommodate promises that resolve to either strings or React elements, which is essential for supporting asynchronous server component rendering. This change aligns well with the PR's goal of handling RSC payloads.


213-213: New flag to identify RSC bundles

The addition of isRSCBundle to the ReactOnRails interface provides a clear way to identify whether the current bundle is an RSC bundle. This flag is important for conditional behavior based on the rendering context.

node_package/src/serverRenderReactComponent.ts (2)

47-60: Enhanced promise handling for React elements

This is a great improvement to the processPromise function. It now properly handles promises that resolve to either strings or React elements, calling processReactElement when necessary. This change enables support for rendering functions that return promises resolving to React elements, which is essential for RSC rendering.


84-86: Updated promise result type for consistency

The function signature update correctly aligns with the enhanced promise types, ensuring type safety throughout the rendering pipeline. This change is necessary to support the new functionality while maintaining type correctness.

node_package/src/streamServerRenderedReactComponent.ts (5)

123-123: Enhanced function signature for streaming React components

The function now accepts both React elements and promises that resolve to React elements or strings, which is essential for supporting RSC. This change aligns with the overall goal of the PR to use RSC payload for server component rendering.


153-185: Well-implemented promise handling for streaming

The promise-based approach elegantly handles both synchronous and asynchronous rendering paths:

  1. Promise.resolve normalizes the input to always be a promise
  2. The type check for strings provides helpful error messages
  3. The renderToPipeableStream is only called when appropriate

The error handling through onShellError and the various event handlers ensures a robust streaming implementation.


139-151: Improved error handling with extracted onShellError

Extracting the onShellError logic into a separate variable improves code readability and maintainability. The error handling is comprehensive, covering both synchronous and asynchronous errors.


190-193: Updated StreamRenderer type for consistency

The StreamRenderer type has been correctly updated to match the new function signature, ensuring type safety throughout the streaming pipeline. This change is necessary to support the enhanced rendering capabilities.


213-215:

❓ Verification inconclusive

Verify promise handling without isPromise check

The condition now only checks for isServerRenderHash and omits the previous check for isPromise. Ensure this doesn't cause issues with promise handling, as the function signature now explicitly accepts promises.


🏁 Script executed:

#!/bin/bash
# Check how isPromise is used elsewhere in the codebase
grep -r "isPromise" --include="*.ts" --include="*.js" node_package/

Length of output: 686


Action: Confirm Promise Handling Strategy in Streamed Server Render

It appears that in node_package/src/streamServerRenderedReactComponent.ts (lines 213–215), the check for promises using isPromise has been removed—likely because the function signature now explicitly accepts promise values. Our repository search shows that isPromise is still actively used in other contexts (e.g., in createReactOutput.ts and serverRenderReactComponent.ts), which indicates that handling promises is still a relevant concern in some parts of the codebase.

  • Verify: Confirm that omitting the promise check here doesn’t cause unhandled promise values to slip through or lead to unexpected behavior.
  • Clarify: Ensure that the error message—currently stating “Server rendering of streams is not supported for server render hashes or promises”—accurately reflects the intended support of promises. If promises are allowed, consider updating the error message accordingly.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (5)
node_package/src/transformRSCStreamAndReplayConsoleLogs.ts (2)

15-20: Consider adding error handling for DOM operations.

While the script injection logic is well-structured, there's no error handling for the DOM operations. Consider wrapping the DOM manipulation in a try-catch block to gracefully handle potential errors if this code runs in environments where DOM operations might not be available.

if (replayConsoleCode?.trim() !== '') {
+  try {
    const scriptElement = document.createElement('script');
    scriptElement.textContent = replayConsoleCode;
    document.body.appendChild(scriptElement);
+  } catch (error) {
+    console.error('Error injecting console replay script:', error);
+  }
}

24-38: Consider more descriptive variable naming for clarity.

While the code is well-structured, some variable names could be more descriptive to better convey their purpose. For example, decodedValue might be better named as decodedChunk or decodedText to clarify what it represents.

node_package/src/RSCClientRoot.ts (2)

3-4: ESLint directive usage.
Disabling no-underscore-dangle is likely necessary due to __FLIGHT_DATA. If possible, limit its scope to just the lines where _ properties are used.


46-77: Constructing a readable stream from window.__FLIGHT_DATA.
Overriding push to enqueue data is creative. Verify that later usage does not inadvertently rely on standard array push behavior.

node_package/src/RSCPayloadContainer.ts (1)

53-85: Async chunk handling logic.
Your promise chain effectively handles data, error, and end events. Just ensure error scenarios have adequate test coverage, especially for partially streamed data.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5c99bc4 and 11c97ce.

📒 Files selected for processing (8)
  • lib/react_on_rails/helper.rb (2 hunks)
  • node_package/src/RSCClientRoot.ts (4 hunks)
  • node_package/src/RSCPayloadContainer.ts (1 hunks)
  • node_package/src/RSCServerRoot.ts (1 hunks)
  • node_package/src/buildConsoleReplay.ts (1 hunks)
  • node_package/src/streamServerRenderedReactComponent.ts (5 hunks)
  • node_package/src/transformRSCStreamAndReplayConsoleLogs.ts (1 hunks)
  • node_package/src/types/index.ts (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • node_package/src/RSCServerRoot.ts
🧰 Additional context used
🧬 Code Definitions (1)
lib/react_on_rails/helper.rb (3)
lib/react_on_rails/utils.rb (1)
  • react_on_rails_pro? (194-198)
spec/dummy/spec/packs_generator_spec.rb (1)
  • configuration (26-28)
lib/react_on_rails/configuration.rb (1)
  • configuration (16-56)
🪛 Biome (1.9.4)
node_package/src/RSCPayloadContainer.ts

[error] 30-30: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

🔇 Additional comments (19)
node_package/src/transformRSCStreamAndReplayConsoleLogs.ts (3)

11-21: Good refactoring: Extraction of JSON chunk handling logic.

The introduction of the handleJsonChunk function improves code organization by encapsulating the common logic for processing JSON chunks. This makes the code more maintainable and easier to understand.


23-42: Robust handling of ArrayBuffer data in stream processing.

The conditional processing of ArrayBuffer views is a good approach. The code correctly:

  1. Combines incomplete chunks from previous iterations
  2. Splits by newlines and preserves incomplete chunks for the next iteration
  3. Filters out empty lines before parsing
  4. Includes proper error handling for JSON parsing

This makes the stream processing more robust and efficient.


43-45: Appropriate fallback for non-ArrayBuffer values.

Adding a direct call to handleJsonChunk for non-ArrayBuffer values ensures the function can handle both types of input correctly, improving the function's flexibility and compatibility.

lib/react_on_rails/helper.rb (2)

365-367: Validate that rsc_payload_generation_url_path handles nil or empty values correctly.
Currently, rsc_url is assigned only when Pro mode is detected. Confirm that a nil URL path is handled gracefully in the downstream code.

Would you like a script to search the codebase for references to rscPayloadGenerationUrl and check if it handles nil or empty values safely?


378-379: New data keys added to rails_context.
Storing rorPro and rscPayloadGenerationUrl is straightforward, but ensure all relevant tests and usage cases consider these new keys for completeness.

node_package/src/buildConsoleReplay.ts (1)

49-53: Efficient conditional wrapping of console replay.
Good practice to avoid rendering an empty script when there’s nothing to replay.

node_package/src/RSCClientRoot.ts (3)

18-23: Global __FLIGHT_DATA for streaming.
Defining an array on window is a workable approach for bridging client/server data, but confirm that no security or memory leak concerns arise from widely accessible global data.


79-83: Extracting a transformable RSC stream.
The function nicely composes the newly created stream with a console-logging transform. This keeps your RSC flow organized.


110-114: Conditional RSC hydration vs. fetch flow.
Switching to hydrateRoot when the DOM already contains content (e.g., SSR scenario) is a solid approach. The fallback to createRoot for empty DOM nodes is sensible.

node_package/src/RSCPayloadContainer.ts (1)

87-93: Boundary checks on chunk indices.
Throwing an error for out-of-range indices is correct. Consider logging or capturing these errors for debugging streaming issues.

node_package/src/types/index.ts (4)

16-47: Good restructuring of RailsContext type to improve type safety

The transformation of RailsContext from an interface to a discriminated union type with serverSide as the discriminator is a significant improvement for type safety. This approach enforces proper property requirements based on whether we're in server or client context.

The additional type documentation for serverSideRSCPayloadParameters explains the purpose clearly and follows good practices by using unknown type to avoid direct dependencies.


60-60: Type enhancement for Promise resolution

The update to allow CreateReactOutputResult to resolve to either a string or a ReactElement provides better flexibility for handling asynchronous server component rendering.


62-62: Type enhancement for RenderFunctionResult

Similar to the previous change, expanding RenderFunctionResult to allow promises to resolve to either a string or a ReactComponent improves flexibility in handling render function outputs.


213-213: New isRSCBundle flag added

Adding the isRSCBundle boolean property to the ReactOnRails interface properly indicates whether the current bundle is a React Server Components bundle, supporting the PR's objective of using RSC payload for server component rendering.

node_package/src/streamServerRenderedReactComponent.ts (5)

90-124: Good refactoring of console replay functionality

The extraction of console replay logic into a separate function improves code organization and readability. The addition of timeout handling with proper cleanup prevents potential memory leaks.


148-148: Enhanced function signature to support asynchronous component rendering

Updating both the streamRenderReactComponent function and StreamRenderer type to accept promises allows for more flexible component rendering patterns, which aligns with the PR's goal of improving server component rendering.

Also applies to: 216-216


164-176: Improved error handling with dedicated function

Extracting the error handling logic into a separate onShellError function improves readability and maintainability. The function properly updates render state, handles error throwing based on configuration, and manages stream lifecycle.


178-210: Robust Promise handling for rendering results

The new Promise-based approach properly handles both synchronous and asynchronous rendering results. The code correctly identifies string results and provides helpful error messages when string rendering is encountered instead of component rendering, while ensuring proper stream handling in all cases.

The implementation of shell ready and error callbacks in the rendering stream provides better control over the rendering lifecycle and error propagation.


244-259: Improved error handling in catch block

The refactored error handling logic now properly manages the stream lifecycle by emitting errors directly (rather than throwing them), writing error content to the stream, and ensuring the stream is correctly ended. This prevents potential memory leaks and ensures proper error reporting.

Comment on lines 29 to 33
const scriptElement = React.createElement('script', {
dangerouslySetInnerHTML: {
__html: escapeScript(`(self.__FLIGHT_DATA||=[]).push(${chunk.chunk})`),
},
key: `script-${chunkIndex}`,
});
Copy link
Contributor

@coderabbitai coderabbitai bot Mar 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Potential XSS risk with dangerouslySetInnerHTML.
Though you’re escaping possible script tags, confirm that the data injected into __FLIGHT_DATA is fully sanitized to prevent malicious scripts.

Would you like suggestions on a safer approach, such as storing data in attributes instead of script content?

🧰 Tools
🪛 Biome (1.9.4)

[error] 30-30: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AbanoubGhadban Here are safer approaches to handling the RSC payload data:

1. Use script tag with type="application/json"

const RSCPayloadContainer = (
  { chunkIndex, getChunkPromise }: RSCPayloadContainerInnerProps,
): React.ReactNode => {
  const chunkPromise = getChunkPromise(chunkIndex);
  const chunk = React.use(chunkPromise);
  
  // Script with type="application/json" won't be executed
  const scriptElement = React.createElement('script', {
    type: 'application/json',
    id: `flight-data-${chunkIndex}`,
    key: `script-${chunkIndex}`,
    text: chunk.chunk, // Using text prop instead of dangerouslySetInnerHTML
  });

  // Add initialization code in a separate script or component
  React.useEffect(() => {
    const scriptEl = document.getElementById(`flight-data-${chunkIndex}`);
    if (scriptEl && scriptEl.textContent) {
      (window.__FLIGHT_DATA || (window.__FLIGHT_DATA = [])).push(
        JSON.parse(scriptEl.textContent)
      );
    }
  }, [chunkIndex]);

  return (
    <>
      {scriptElement}
      {!chunk.isLastChunk && (
        <React.Suspense fallback={null}>
          <RSCPayloadContainer 
            chunkIndex={chunkIndex + 1} 
            getChunkPromise={getChunkPromise} 
          />
        </React.Suspense>
      )}
    </>
  );
};

2. Use a hidden div with textContent

const RSCPayloadContainer = (
  { chunkIndex, getChunkPromise }: RSCPayloadContainerInnerProps,
): React.ReactNode => {
  const chunkPromise = getChunkPromise(chunkIndex);
  const chunk = React.use(chunkPromise);

  // Create a hidden div with the data
  const dataElement = React.createElement('div', {
    style: { display: 'none' },
    id: `flight-data-${chunkIndex}`,
    key: `data-${chunkIndex}`,
    'data-flight': true, // Marker attribute for identifying the elements
  }, chunk.chunk); // Text content is safely set

  // Process the data
  React.useEffect(() => {
    const dataEl = document.getElementById(`flight-data-${chunkIndex}`);
    if (dataEl && dataEl.textContent) {
      (window.__FLIGHT_DATA || (window.__FLIGHT_DATA = [])).push(
        JSON.parse(dataEl.textContent)
      );
    }
  }, [chunkIndex]);

  return (
    <>
      {dataElement}
      {!chunk.isLastChunk && (
        <React.Suspense fallback={null}>
          <RSCPayloadContainer 
            chunkIndex={chunkIndex + 1} 
            getChunkPromise={getChunkPromise} 
          />
        </React.Suspense>
      )}
    </>
  );
};

3. Use a template element (modern approach)

const RSCPayloadContainer = (
  { chunkIndex, getChunkPromise }: RSCPayloadContainerInnerProps,
): React.ReactNode => {
  const chunkPromise = getChunkPromise(chunkIndex);
  const chunk = React.use(chunkPromise);

  // Template elements aren't rendered but preserve their content
  const templateElement = React.createElement('template', {
    id: `flight-data-${chunkIndex}`,
    key: `template-${chunkIndex}`,
  }, chunk.chunk);

  // Process the data
  React.useEffect(() => {
    const templateEl = document.getElementById(`flight-data-${chunkIndex}`) as HTMLTemplateElement;
    if (templateEl && templateEl.innerHTML) {
      (window.__FLIGHT_DATA || (window.__FLIGHT_DATA = [])).push(
        JSON.parse(templateEl.innerHTML)
      );
    }
  }, [chunkIndex]);

  return (
    <>
      {templateElement}
      {!chunk.isLastChunk && (
        <React.Suspense fallback={null}>
          <RSCPayloadContainer 
            chunkIndex={chunkIndex + 1} 
            getChunkPromise={getChunkPromise} 
          />
        </React.Suspense>
      )}
    </>
  );
};

These approaches eliminate the XSS risk while still allowing you to deliver the RSC flight data to the client for hydration. The first option is likely the most standard approach for embedding non-executable data in HTML.

@AbanoubGhadban AbanoubGhadban force-pushed the abanoubghadban/pro465/use-rsc-payload-to-render-server-components-on-server branch from 11c97ce to 3b95637 Compare March 27, 2025 10:04
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🔭 Outside diff range comments (1)
node_package/src/ClientSideRenderer.ts (1)

17-23: ⚠️ Potential issue

Fix return type mismatch in delegateToRenderer function

The function signature has been changed from returning Promise<boolean> to boolean, but the implementation still contains asynchronous calls. This is causing multiple pipeline failures across different builds.

-function delegateToRenderer(
+async function delegateToRenderer(
  componentObj: RegisteredComponent,
  props: Record<string, unknown>,
  railsContext: RailsContext,
  domNodeId: string,
  trace: boolean,
-): boolean {
+): Promise<boolean> {
🧰 Tools
🪛 GitHub Check: build

[failure] 23-23:
Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 GitHub Check: examples (oldest)

[failure] 23-23:
Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 GitHub Check: build-dummy-app-webpack-test-bundles (oldest)

[failure] 23-23:
Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 GitHub Check: examples (newest)

[failure] 23-23:
Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 GitHub Actions: Lint JS and Ruby

[error] 23-23: error TS1055: Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 GitHub Actions: Generator tests

[error] 23-23: error TS1055: Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 GitHub Actions: JS unit tests for Renderer package

[error] 23-23: TS1055: Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.


[error] 23-23: TS1055: Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 GitHub Actions: Main test

[error] 23-23: error TS1055: Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

♻️ Duplicate comments (1)
node_package/src/streamServerRenderedReactComponent.ts (1)

234-236: ⚠️ Potential issue

Error message needs updating to reflect promise support.

The error message still mentions that "stream rendering is not supported for server render hashes or promises" even though promises are now supported. This should be updated to only mention server render hashes.

-      throw new Error('Server rendering of streams is not supported for server render hashes or promises.');
+      throw new Error('Server rendering of streams is not supported for server render hashes.');
🧹 Nitpick comments (9)
node_package/src/registerServerComponent/server.rsc.ts (1)

22-26: LGTM with minor formatting issues

The function implementation correctly delegates to ReactOnRails.register. There are some minor formatting issues that ESLint has flagged:

  1. Remove trailing whitespace on lines 6 and 11
  2. Adjust function parameter formatting

Consider fixing these formatting issues to keep the codebase consistent:

-const registerServerComponent = (
-  components: { [id: string]: ReactComponent | RenderFunction },
-) => ReactOnRails.register(components);
+const registerServerComponent = (components: { [id: string]: ReactComponent | RenderFunction }) => 
+  ReactOnRails.register(components);
🧰 Tools
🪛 ESLint

[error] 22-24: Replace ⏎··components:·{·[id:·string]:·ReactComponent·|·RenderFunction·},⏎)·=> with components:·{·[id:·string]:·ReactComponent·|·RenderFunction·})·=>⏎·

(prettier/prettier)

node_package/src/buildConsoleReplay.ts (1)

54-54: Formatting inconsistency in type declaration

The type declaration changed from (typeof console)['history'] to typeof console['history']. This change is causing ESLint warnings.

-  customConsoleHistory: typeof console['history'] | undefined = undefined,
+  customConsoleHistory: (typeof console)['history'] | undefined = undefined,
🧰 Tools
🪛 ESLint

[error] 54-54: Replace typeof·console with (typeof·console)

(prettier/prettier)

node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts (1)

33-33: Consider formatting the error message for better readability

The error message line is quite long. Consider breaking it into multiple lines for improved readability.

-  throw new Error(`Error transforming RSC stream (${stream.constructor.name}), (stream: ${stream}), stringified stream: ${JSON.stringify(stream)}, error: ${error}`);
+  throw new Error(
+    `Error transforming RSC stream (${stream.constructor.name}), ` +
+    `(stream: ${stream}), ` +
+    `stringified stream: ${JSON.stringify(stream)}, ` +
+    `error: ${error}`
+  );
🧰 Tools
🪛 ESLint

[error] 33-33: Replace Error·transforming·RSC·stream·(${stream.constructor.name}),·(stream:·${stream}),·stringified·stream:·${JSON.stringify(stream)},·error:·${error} with ⏎······Error·transforming·RSC·stream·(${stream.constructor.name}),·(stream:·${stream}),·stringified·stream:·${JSON.stringify(stream)},·error:·${error},⏎····

(prettier/prettier)

node_package/src/ReactOnRails.client.ts (2)

121-121: Fix the options interface formatting

The parameter type formatting should use semicolons to separate properties to be consistent with TypeScript interfaces.

-  setOptions(newOptions: {traceTurbolinks?: boolean, turbo?: boolean }): void {
+  setOptions(newOptions: {traceTurbolinks?: boolean; turbo?: boolean }): void {
🧰 Tools
🪛 ESLint

[error] 121-121: Replace traceTurbolinks?:·boolean, with ·traceTurbolinks?:·boolean;

(prettier/prettier)


342-342: Consider using object spread syntax instead of Object.assign

Modern JavaScript typically uses the object spread syntax for better readability.

-    this.options = Object.assign({}, DEFAULT_OPTIONS);
+    this.options = { ...DEFAULT_OPTIONS };
🧰 Tools
🪛 ESLint

[error] 342-342: Use an object spread instead of Object.assign eg: { ...foo }.

(prefer-object-spread)

node_package/src/RSCServerRoot.ts (4)

1-7: Fix import order.

The stream import should occur before the type import according to standard import ordering rules.

 import * as React from 'react';
 import { createFromNodeStream } from 'react-on-rails-rsc/client.node';
+import { PassThrough } from 'stream';
 import type { RenderFunction, RailsContext } from './types';
 import transformRSCStream from './transformRSCNodeStreamAndReplayConsoleLogs';
 import loadJsonFile from './loadJsonFile';
 import RSCPayloadContainer from './RSCPayloadContainer';
-import { PassThrough } from 'stream';
🧰 Tools
🪛 ESLint

[error] 7-7: stream import should occur before type import of ./types

(import/order)


22-24: Good React feature check, but fix error message formatting.

The code correctly checks for React.use support, which is necessary for server components, but the error message formatting should be improved.

 if (!('use' in React && typeof React.use === 'function')) {
-  throw new Error('React.use is not defined. Please ensure you are using React 18 with experimental features enabled or React 19+ to use server components.');
+  throw new Error(
+    'React.use is not defined. Please ensure you are using React 18 with experimental features enabled or React 19+ to use server components.'
+  );
 }
🧰 Tools
🪛 ESLint

[error] 23-23: Replace 'React.use·is·not·defined.·Please·ensure·you·are·using·React·18·with·experimental·features·enabled·or·React·19+·to·use·server·components.' with ⏎····'React.use·is·not·defined.·Please·ensure·you·are·using·React·18·with·experimental·features·enabled·or·React·19+·to·use·server·components.',⏎··

(prettier/prettier)


88-93: Ensure stream piping doesn't leak resources.

The code creates two PassThrough streams and pipes the RSC payload stream to both, but there's no explicit error handling for potential pipe failures.

 // Tee the stream to pass it to the server component and the payload container
 const rscPayloadStream1 = new PassThrough();
-rscPayloadStream.pipe(rscPayloadStream1);
+rscPayloadStream.pipe(rscPayloadStream1).on('error', (err) => {
+  console.error('Error in RSC payload stream piping:', err);
+});
 const rscPayloadStream2 = new PassThrough();
-rscPayloadStream.pipe(rscPayloadStream2);
+rscPayloadStream.pipe(rscPayloadStream2).on('error', (err) => {
+  console.error('Error in RSC payload stream piping:', err);
+});

94-108: Use React.Fragment shorthand for better readability.

The code correctly returns a function that renders both the server component and payload container, but could be simplified using the React.Fragment shorthand syntax.

 return () => React.createElement(
-  React.Fragment,
-  null, [
+  <>,
+  [
     React.createElement(
       React.Fragment,
       { key: 'serverComponentElement' },
       use(serverComponentElement)
     ),
     React.createElement(
       RSCPayloadContainer,
       { RSCPayloadStream: rscPayloadStream2, key: 'rscPayloadContainer' },
     ),
-  ]
+  ]
+  </>
 );

Note: If you prefer to avoid JSX syntax and want to keep using React.createElement, you could still clean up the formatting for readability.

🧰 Tools
🪛 ESLint

[error] 95-97: Replace ·React.createElement(⏎····React.Fragment,⏎··· with ⏎····React.createElement(React.Fragment,

(prettier/prettier)


[error] 98-102: Replace ⏎········React.Fragment,⏎········{·key:·'serverComponentElement'·},⏎········use(serverComponentElement)⏎······ with React.Fragment,·{·key:·'serverComponentElement'·},·use(serverComponentElement)

(prettier/prettier)


[error] 103-104: Replace ⏎········RSCPayloadContainer, with RSCPayloadContainer,·{

(prettier/prettier)


[error] 105-105: Replace ·{·RSCPayloadStream:·rscPayloadStream2,·key:·'rscPayloadContainer'·} with ·RSCPayloadStream:·rscPayloadStream2,⏎········key:·'rscPayloadContainer'

(prettier/prettier)


[error] 106-106: Insert }

(prettier/prettier)


[error] 107-108: Delete ⏎··

(prettier/prettier)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 11c97ce and 3b95637.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (26)
  • lib/react_on_rails/configuration.rb (6 hunks)
  • lib/react_on_rails/helper.rb (2 hunks)
  • lib/react_on_rails/test_helper/webpack_assets_status_checker.rb (1 hunks)
  • lib/react_on_rails/utils.rb (1 hunks)
  • node_package/src/ClientSideRenderer.ts (1 hunks)
  • node_package/src/ComponentRegistry.ts (1 hunks)
  • node_package/src/RSCClientRoot.ts (4 hunks)
  • node_package/src/RSCPayloadContainer.ts (1 hunks)
  • node_package/src/RSCServerRoot.ts (1 hunks)
  • node_package/src/ReactOnRails.client.ts (3 hunks)
  • node_package/src/ReactOnRailsRSC.ts (3 hunks)
  • node_package/src/buildConsoleReplay.ts (1 hunks)
  • node_package/src/createReactOutput.ts (2 hunks)
  • node_package/src/handleError.ts (1 hunks)
  • node_package/src/loadJsonFile.ts (1 hunks)
  • node_package/src/loadReactClientManifest.ts (0 hunks)
  • node_package/src/registerServerComponent/server.rsc.ts (1 hunks)
  • node_package/src/registerServerComponent/server.ts (2 hunks)
  • node_package/src/serverRenderReactComponent.ts (2 hunks)
  • node_package/src/streamServerRenderedReactComponent.ts (5 hunks)
  • node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts (1 hunks)
  • node_package/src/transformRSCStreamAndReplayConsoleLogs.ts (2 hunks)
  • node_package/src/types/index.ts (4 hunks)
  • node_package/tests/serverRenderReactComponent.test.js (2 hunks)
  • node_package/tests/streamServerRenderedReactComponent.test.jsx (2 hunks)
  • package.json (2 hunks)
💤 Files with no reviewable changes (1)
  • node_package/src/loadReactClientManifest.ts
🚧 Files skipped from review as they are similar to previous changes (6)
  • lib/react_on_rails/helper.rb
  • node_package/src/handleError.ts
  • node_package/tests/serverRenderReactComponent.test.js
  • node_package/src/loadJsonFile.ts
  • lib/react_on_rails/utils.rb
  • lib/react_on_rails/test_helper/webpack_assets_status_checker.rb
🧰 Additional context used
🧬 Code Definitions (4)
node_package/tests/streamServerRenderedReactComponent.test.jsx (4)
lib/react_on_rails/react_component/render_options.rb (1)
  • props (26-28)
spec/dummy/client/app/store/composeInitialState.js (1)
  • props (3-3)
spec/dummy/client/app/startup/ReduxSharedStoreApp.client.jsx (1)
  • props (16-50)
node_package/src/ReactOnRails.client.ts (1)
  • streamServerRenderedReactComponent (287-291)
node_package/src/streamServerRenderedReactComponent.ts (3)
node_package/src/buildConsoleReplay.ts (1)
  • buildConsoleReplay (53-62)
node_package/src/serverRenderUtils.ts (2)
  • createResultObject (3-18)
  • convertToError (20-22)
node_package/src/types/index.ts (1)
  • RenderParams (142-146)
node_package/src/ReactOnRailsRSC.ts (3)
node_package/src/types/index.ts (2)
  • RSCRenderParams (148-150)
  • StreamRenderState (226-229)
node_package/src/streamServerRenderedReactComponent.ts (1)
  • transformRenderStreamChunksToResultObject (86-147)
node_package/src/loadJsonFile.ts (1)
  • loadJsonFile (7-25)
node_package/src/registerServerComponent/server.rsc.ts (1)
node_package/src/types/index.ts (3)
  • ReactComponent (104-104)
  • RenderFunction (106-106)
  • ReactOnRails (185-218)
🪛 GitHub Check: build
node_package/src/ComponentRegistry.ts

[failure] 23-23:
Cannot find name 'RenderFunction'.

node_package/src/ClientSideRenderer.ts

[failure] 23-23:
Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 GitHub Check: examples (oldest)
node_package/src/ComponentRegistry.ts

[failure] 23-23:
Cannot find name 'RenderFunction'.

node_package/src/ClientSideRenderer.ts

[failure] 23-23:
Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 GitHub Check: build-dummy-app-webpack-test-bundles (oldest)
node_package/src/ComponentRegistry.ts

[failure] 23-23:
Cannot find name 'RenderFunction'.

node_package/src/ClientSideRenderer.ts

[failure] 23-23:
Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 GitHub Check: examples (newest)
node_package/src/ComponentRegistry.ts

[failure] 23-23:
Cannot find name 'RenderFunction'.

node_package/src/ClientSideRenderer.ts

[failure] 23-23:
Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 GitHub Actions: JS unit tests for Renderer package
node_package/src/ComponentRegistry.ts

[error] 23-23: TS2304: Cannot find name 'RenderFunction'.


[error] 23-23: TS2304: Cannot find name 'RenderFunction'.


[error] 23-23: TS2304: Cannot find name 'RenderFunction'.

node_package/src/ClientSideRenderer.ts

[error] 23-23: TS1055: Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.


[error] 23-23: TS1055: Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 ESLint
node_package/src/createReactOutput.ts

[error] 19-19: Insert ······

(prettier/prettier)


[error] 22-22: Insert ,⏎····

(prettier/prettier)


[error] 87-87: Insert ;

(prettier/prettier)

node_package/tests/streamServerRenderedReactComponent.test.jsx

[error] 210-210: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 228-228: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 242-242: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 260-260: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 282-282: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 316-316: Return values from promise executor functions cannot be read.

(no-promise-executor-return)

node_package/src/registerServerComponent/server.ts

[error] 7-7: Delete ·

(prettier/prettier)


[error] 10-10: Delete ·

(prettier/prettier)

node_package/src/ClientSideRenderer.ts

[error] 24-24: 'name' is assigned a value but never used.

(@typescript-eslint/no-unused-vars)

node_package/src/RSCPayloadContainer.ts

[error] 6-6: Insert ;

(prettier/prettier)


[error] 10-10: Insert ;

(prettier/prettier)


[error] 15-15: Insert ;

(prettier/prettier)


[error] 18-20: Replace ⏎····.replace(/<!--/g,·'<\\!--')⏎···· with .replace(/<!--/g,·'<\\!--')

(prettier/prettier)


[error] 23-23: Insert {

(prettier/prettier)


[error] 24-25: Replace ·{·chunkIndex,·getChunkPromise·}:·RSCPayloadContainerInnerProps,⏎ with ·chunkIndex,⏎··getChunkPromise,⏎}:·RSCPayloadContainerInnerProps

(prettier/prettier)


[error] 45-48: Replace ⏎········RSCPayloadContainer,⏎········{·chunkIndex:·chunkIndex·+·1,·getChunkPromise·},⏎······ with RSCPayloadContainer,·{·chunkIndex:·chunkIndex·+·1,·getChunkPromise·}

(prettier/prettier)


[error] 51-51: Insert ;

(prettier/prettier)


[error] 87-87: Insert ⏎····

(prettier/prettier)


[error] 88-88: Replace ···· with ······

(prettier/prettier)


[error] 89-89: Insert ··

(prettier/prettier)


[error] 90-90: Insert ··

(prettier/prettier)


[error] 92-92: Replace ···· with ······

(prettier/prettier)


[error] 93-93: Replace },·[chunkPromises] with ··},⏎····[chunkPromises],⏎··

(prettier/prettier)

node_package/src/transformRSCStreamAndReplayConsoleLogs.ts

[error] 3-3: Replace stream:·ReadableStream<Uint8Array·|·RenderResult> with ⏎··stream:·ReadableStream<Uint8Array·|·RenderResult>,⏎

(prettier/prettier)


[error] 12-12: Delete ······

(prettier/prettier)


[error] 17-17: Replace .trim().replace(/^<script.*>/,·'') with ⏎··········.trim()⏎··········.replace(/^<script.*>/,·'')⏎··········

(prettier/prettier)


[error] 24-24: Delete ······

(prettier/prettier)


[error] 32-32: Replace line with (line)

(prettier/prettier)

node_package/src/streamServerRenderedReactComponent.ts

[error] 99-99: Insert ;

(prettier/prettier)


[error] 149-149: Replace reactRenderingResult:·ReactElement·|·Promise<ReactElement·|·string>,·options:·RenderParams with ⏎··reactRenderingResult:·ReactElement·|·Promise<ReactElement·|·string>,⏎··options:·RenderParams,⏎

(prettier/prettier)


[error] 172-172: Insert ;

(prettier/prettier)


[error] 174-174: Replace .then(reactRenderedElement with ⏎····.then((reactRenderedElement)

(prettier/prettier)


[error] 175-175: Insert ··

(prettier/prettier)


[error] 176-176: Insert ··

(prettier/prettier)


[error] 177-177: Insert ··

(prettier/prettier)


[error] 178-179: Replace 'To·benefit·from·React·on·Rails·Pro·streaming·feature,·your·render·function·should·return·a·React·component.\n'·+·⏎········'Do·not·call·ReactDOMServer.renderToString()·inside·the·render·function·as·this·defeats·the·purpose·of·streaming.\n' with ····'To·benefit·from·React·on·Rails·Pro·streaming·feature,·your·render·function·should·return·a·React·component.\n'·+⏎············'Do·not·call·ReactDOMServer.renderToString()·inside·the·render·function·as·this·defeats·the·purpose·of·streaming.\n',

(prettier/prettier)


[error] 180-180: Insert ··

(prettier/prettier)


[error] 182-182: Replace ······ with ········

(prettier/prettier)


[error] 183-183: Insert ··

(prettier/prettier)


[error] 184-184: Insert ··

(prettier/prettier)


[error] 185-185: Insert ··

(prettier/prettier)


[error] 187-187: Insert ··

(prettier/prettier)


[error] 188-188: Insert ··

(prettier/prettier)


[error] 189-189: Insert ··

(prettier/prettier)


[error] 190-190: Insert ··

(prettier/prettier)


[error] 191-191: Insert ··

(prettier/prettier)


[error] 192-192: Insert ··

(prettier/prettier)


[error] 193-193: Insert ··

(prettier/prettier)


[error] 194-194: Insert ··

(prettier/prettier)


[error] 195-195: Insert ··

(prettier/prettier)


[error] 196-196: Replace ········ with ··········

(prettier/prettier)


[error] 197-197: Insert ··

(prettier/prettier)


[error] 198-198: Replace ········ with ··········

(prettier/prettier)


[error] 199-199: Insert ··

(prettier/prettier)


[error] 200-200: Replace ········ with ··········

(prettier/prettier)


[error] 201-201: Insert ··

(prettier/prettier)


[error] 202-202: Insert ··

(prettier/prettier)


[error] 203-203: Insert ··

(prettier/prettier)


[error] 204-204: Insert ··

(prettier/prettier)


[error] 205-205: Insert ··

(prettier/prettier)


[error] 206-206: Replace ··}) with ····})⏎····

(prettier/prettier)


[error] 240-245: Replace ⏎······readableStream,⏎······writeChunk,⏎······emitError,⏎······endStream⏎····}·=·transformRenderStreamChunksToResultObject({·hasErrors:·true,·isShellReady:·false,·result:·null with ·readableStream,·writeChunk,·emitError,·endStream·}·=·transformRenderStreamChunksToResultObject({⏎······hasErrors:·true,⏎······isShellReady:·false,⏎······result:·null,⏎···

(prettier/prettier)

node_package/src/ReactOnRailsRSC.ts

[error] 24-24: Replace reactRenderingResult:·ReactElement·|·Promise<ReactElement·|·string>,·options:·RSCRenderParams with ⏎··reactRenderingResult:·ReactElement·|·Promise<ReactElement·|·string>,⏎··options:·RSCRenderParams,⏎

(prettier/prettier)


[error] 37-37: Insert ⏎···

(prettier/prettier)

node_package/src/RSCClientRoot.ts

[error] 65-65: Insert ,

(prettier/prettier)


[error] 77-77: Insert ;

(prettier/prettier)


[error] 83-83: Insert ;

(prettier/prettier)


[error] 114-114: Insert ;

(prettier/prettier)

node_package/src/RSCServerRoot.ts

[error] 7-7: stream import should occur before type import of ./types

(import/order)


[error] 20-20: Insert ;

(prettier/prettier)


[error] 23-23: Replace 'React.use·is·not·defined.·Please·ensure·you·are·using·React·18·with·experimental·features·enabled·or·React·19+·to·use·server·components.' with ⏎····'React.use·is·not·defined.·Please·ensure·you·are·using·React·18·with·experimental·features·enabled·or·React·19+·to·use·server·components.',⏎··

(prettier/prettier)


[error] 28-28: Replace stream:·NodeJS.ReadableStream,·ssrManifest:·Record<string,·unknown> with ⏎··stream:·NodeJS.ReadableStream,⏎··ssrManifest:·Record<string,·unknown>,⏎

(prettier/prettier)


[error] 31-31: Insert ;

(prettier/prettier)


[error] 33-33: Replace reactServerManifestFileName:·string,·reactClientManifestFileName:·string with ⏎··reactServerManifestFileName:·string,⏎··reactClientManifestFileName:·string,⏎

(prettier/prettier)


[error] 41-41: Replace "/webpack/development/" with '/webpack/development/'

(prettier/prettier)


[error] 54-54: Insert ,

(prettier/prettier)


[error] 59-59: Insert ;

(prettier/prettier)


[error] 61-61: Replace {·componentName,·componentProps·}:·RSCServerRootProps,·railsContext?:·RailsContext with ⏎··{·componentName,·componentProps·}:·RSCServerRootProps,⏎··railsContext?:·RailsContext,⏎

(prettier/prettier)


[error] 62-62: Replace !railsContext?.serverSide·||·!railsContext?.reactClientManifestFileName·||·!railsContext?.reactServerClientManifestFileName with ⏎····!railsContext?.serverSide·||⏎····!railsContext?.reactClientManifestFileName·||⏎····!railsContext?.reactServerClientManifestFileName⏎··

(prettier/prettier)


[error] 64-64: Insert ⏎········

(prettier/prettier)


[error] 65-65: Replace ······ with ········

(prettier/prettier)


[error] 66-66: Replace 'as·stated·in·the·React·Server·Component·tutorial.·The·received·rails·context·is:·'}${··JSON.stringify(railsContext)}`` with ··'as·stated·in·the·React·Server·Component·tutorial.·The·received·rails·context·is:·'⏎······}${JSON.stringify(railsContext)},

(prettier/prettier)


[error] 73-73: Replace ······ with ········

(prettier/prettier)


[error] 74-74: Replace 'is·set·to·true.' with ··'is·set·to·true.',

(prettier/prettier)


[error] 80-80: Insert ,

(prettier/prettier)


[error] 85-85: Insert ,

(prettier/prettier)


[error] 95-97: Replace ·React.createElement(⏎····React.Fragment,⏎··· with ⏎····React.createElement(React.Fragment,

(prettier/prettier)


[error] 98-102: Replace ⏎········React.Fragment,⏎········{·key:·'serverComponentElement'·},⏎········use(serverComponentElement)⏎······ with React.Fragment,·{·key:·'serverComponentElement'·},·use(serverComponentElement)

(prettier/prettier)


[error] 103-104: Replace ⏎········RSCPayloadContainer, with RSCPayloadContainer,·{

(prettier/prettier)


[error] 105-105: Replace ·{·RSCPayloadStream:·rscPayloadStream2,·key:·'rscPayloadContainer'·} with ·RSCPayloadStream:·rscPayloadStream2,⏎········key:·'rscPayloadContainer'

(prettier/prettier)


[error] 106-106: Insert }

(prettier/prettier)


[error] 107-108: Delete ⏎··

(prettier/prettier)

node_package/src/ReactOnRails.client.ts

[error] 121-121: Replace traceTurbolinks?:·boolean, with ·traceTurbolinks?:·boolean;

(prettier/prettier)


[error] 137-139: Replace ⏎········Invalid·options·passed·to·ReactOnRails.options:·${JSON.stringify(newOptions)},⏎······ with Invalid·options·passed·to·ReactOnRails.options:·${JSON.stringify(newOptions)}

(prettier/prettier)


[error] 342-342: Use an object spread instead of Object.assign eg: { ...foo }.

(prefer-object-spread)

node_package/src/buildConsoleReplay.ts

[error] 54-54: Replace typeof·console with (typeof·console)

(prettier/prettier)

node_package/src/registerServerComponent/server.rsc.ts

[error] 6-6: Delete ·

(prettier/prettier)


[error] 11-11: Delete ·

(prettier/prettier)


[error] 22-24: Replace ⏎··components:·{·[id:·string]:·ReactComponent·|·RenderFunction·},⏎)·=> with components:·{·[id:·string]:·ReactComponent·|·RenderFunction·})·=>⏎·

(prettier/prettier)

node_package/src/serverRenderReactComponent.ts

[error] 61-61: Replace 'Your·render·function·returned·a·Promise,·which·is·only·supported·by·a·node·renderer,·not·ExecJS.' with ⏎······'Your·render·function·returned·a·Promise,·which·is·only·supported·by·a·node·renderer,·not·ExecJS.',⏎····

(prettier/prettier)

node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts

[error] 33-33: Replace Error·transforming·RSC·stream·(${stream.constructor.name}),·(stream:·${stream}),·stringified·stream:·${JSON.stringify(stream)},·error:·${error} with ⏎······Error·transforming·RSC·stream·(${stream.constructor.name}),·(stream:·${stream}),·stringified·stream:·${JSON.stringify(stream)},·error:·${error},⏎····

(prettier/prettier)

node_package/src/types/index.ts

[error] 35-35: Insert ⏎··|·

(prettier/prettier)


[error] 36-36: Insert ····

(prettier/prettier)


[error] 37-37: Replace } with ····}⏎·

(prettier/prettier)


[error] 38-38: Insert ····

(prettier/prettier)


[error] 39-39: Insert ····

(prettier/prettier)


[error] 40-40: Insert ····

(prettier/prettier)


[error] 41-41: Insert ····

(prettier/prettier)


[error] 42-42: Insert ····

(prettier/prettier)


[error] 43-43: Insert ····

(prettier/prettier)


[error] 44-44: Insert ····

(prettier/prettier)


[error] 45-45: Insert ····

(prettier/prettier)


[error] 46-46: Insert ····

(prettier/prettier)


[error] 47-47: Insert ····

(prettier/prettier)


[error] 48-48: Replace } with ····}⏎

(prettier/prettier)

🪛 GitHub Actions: Lint JS and Ruby
node_package/src/ClientSideRenderer.ts

[error] 23-23: error TS1055: Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 GitHub Actions: Generator tests
node_package/src/ClientSideRenderer.ts

[error] 23-23: error TS1055: Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 GitHub Actions: Main test
node_package/src/ClientSideRenderer.ts

[error] 23-23: error TS1055: Type 'boolean' is not a valid async function return type in ES5 because it does not refer to a Promise-compatible constructor value.

🪛 Biome (1.9.4)
node_package/src/RSCPayloadContainer.ts

[error] 30-30: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

🔇 Additional comments (56)
lib/react_on_rails/configuration.rb (6)

13-13: LGTM: Added constant for the React Server Client Manifest file

This adds a new constant for the default React Server Client Manifest file name, consistent with existing naming conventions.


25-25: LGTM: Added default configuration for React Server Client Manifest

The default configuration value is properly set using the constant defined earlier.


70-70: LGTM: Added attribute accessor for React Server Client Manifest

The new configuration property has been properly added to the attribute accessors list.


86-87: LGTM: Updated method signature to include new parameter

The initialize method has been properly extended to support the new configuration parameter.


117-117: LGTM: Added initialization for React Server Client Manifest

The instance variable is properly set from the constructor parameter, consistent with other configuration properties.


269-275: LGTM: Enhanced webpack generated files handling

The method now correctly includes the React Server Client Manifest file in the webpack generated files list when it's present, following the same pattern as other manifest files.

node_package/src/registerServerComponent/server.rsc.ts (2)

1-3: LGTM: Proper imports for the new module

The imports correctly bring in the ReactOnRails client and necessary type definitions.


4-21: LGTM: Well-documented function with clear JSDoc

The JSDoc comment clearly explains the purpose of this function, how it differs from the server bundle registration, and includes a helpful usage example.

🧰 Tools
🪛 ESLint

[error] 6-6: Delete ·

(prettier/prettier)


[error] 11-11: Delete ·

(prettier/prettier)

node_package/src/buildConsoleReplay.ts (1)

57-62: LGTM: Improved handling of empty console replay

Good optimization that avoids adding empty script tags when there's no console output to replay.

node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts (1)

1-35: Implementation for transforming RSC streams looks solid

This new utility function transforms a Node.js ReadableStream containing JSON chunks into an HTML stream by extracting the html property from each JSON object. The implementation correctly handles:

  • Accumulating incomplete chunks
  • Processing multiple newline-separated JSON objects
  • Error propagation and detailed error messages

The stream transformation approach is efficient for handling React Server Component payloads.

🧰 Tools
🪛 ESLint

[error] 33-33: Replace Error·transforming·RSC·stream·(${stream.constructor.name}),·(stream:·${stream}),·stringified·stream:·${JSON.stringify(stream)},·error:·${error} with ⏎······Error·transforming·RSC·stream·(${stream.constructor.name}),·(stream:·${stream}),·stringified·stream:·${JSON.stringify(stream)},·error:·${error},⏎····

(prettier/prettier)

package.json (1)

15-18: Good addition of conditional exports for RSC support

The change to define a conditional export for ./registerServerComponent/server adds proper support for React Server Components by exposing a different entry point for the "react-server" condition. This is consistent with modern React Server Components architecture.

node_package/src/registerServerComponent/server.ts (3)

1-4: Appropriate import of RSCServerRoot for server component rendering

The addition of the RSCServerRoot import and type imports are necessary for the new implementation.


5-25: Well-documented component wrapper functionality

The updated documentation clearly explains how RSCServerRoot handles server-side rendering of React Server Components using pre-generated RSC payloads, which helps developers understand the changes in behavior.

🧰 Tools
🪛 ESLint

[error] 7-7: Delete ·

(prettier/prettier)


[error] 10-10: Delete ·

(prettier/prettier)


26-35: Good implementation of component wrapping for RSC support

The implementation now wraps each registered component with RSCServerRoot, enabling proper server-side rendering of React Server Components. This approach effectively separates the component definition from its server rendering logic.

However, there are a few minor whitespace issues in the JSDoc comments that the linter has flagged.

node_package/src/ReactOnRails.client.ts (2)

137-140: Improved error message formatting

The error message is now more readable with the multi-line formatting.

🧰 Tools
🪛 ESLint

[error] 137-139: Replace ⏎········Invalid·options·passed·to·ReactOnRails.options:·${JSON.stringify(newOptions)},⏎······ with Invalid·options·passed·to·ReactOnRails.options:·${JSON.stringify(newOptions)}

(prettier/prettier)


344-345: Good addition of isRSCBundle flag

Adding the isRSCBundle flag set to false by default establishes the baseline for the client bundle. This will help differentiate between client and RSC bundles in the codebase.

node_package/src/transformRSCStreamAndReplayConsoleLogs.ts (3)

3-3: Updated function signature to support RSC payload rendering

The function now accepts a ReadableStream of either Uint8Array or RenderResult. This change enhances the component's ability to handle both binary data and structured rendering results, which is crucial for the PR's goal of using RSC payload for server component rendering.

🧰 Tools
🪛 ESLint

[error] 3-3: Replace stream:·ReadableStream<Uint8Array·|·RenderResult> with ⏎··stream:·ReadableStream<Uint8Array·|·RenderResult>,⏎

(prettier/prettier)


13-23: Good extraction of JSON chunk handling into a separate function

Extracting this logic improves code organization and readability. The function handles both HTML content and console replay scripts appropriately.

🧰 Tools
🪛 ESLint

[error] 17-17: Replace .trim().replace(/^<script.*>/,·'') with ⏎··········.trim()⏎··········.replace(/^<script.*>/,·'')⏎··········

(prettier/prettier)


25-47: Enhanced stream handling logic to support both Uint8Array and RenderResult

The updated code now differentiates between ArrayBuffer views (binary data) and direct RenderResult objects, enabling the dual functionality needed for RSC support. This implementation properly handles both cases while maintaining backward compatibility.

🧰 Tools
🪛 ESLint

[error] 32-32: Replace line with (line)

(prettier/prettier)

node_package/src/ClientSideRenderer.ts (1)

29-29: Improved logging consistency

Using componentObj.name instead of name improves logging consistency. This is a good change.

node_package/src/createReactOutput.ts (3)

11-29: Good extraction of React element creation logic

Creating a separate function for handling React elements is a good refactoring that improves code maintainability. The function properly handles both direct React elements and component functions.

Consider fixing these minor formatting issues:

export function createReactElementFromRenderFunction(
  renderFunctionResult: ReactComponent,
  name: string,
  props: Record<string, unknown> | undefined,
): React.ReactElement {
  if (React.isValidElement(renderFunctionResult)) {
    // If already a ReactElement, then just return it.
    console.error(
-`Warning: ReactOnRails: Your registered render-function (ReactOnRails.register) for ${name}
+      `Warning: ReactOnRails: Your registered render-function (ReactOnRails.register) for ${name}
incorrectly returned a React Element (JSX). Instead, return a React Function Component by
wrapping your JSX in a function. ReactOnRails v13 will throw error on this, as React Hooks do not
-work if you return JSX. Update by wrapping the result JSX of ${name} in a fat arrow function.`);
+work if you return JSX. Update by wrapping the result JSX of ${name} in a fat arrow function.`,
+    );
    return renderFunctionResult;
  }

  // If a component, then wrap in an element
  const reactComponent = renderFunctionResult as ReactComponent;
  return React.createElement(reactComponent, props);
}
🧰 Tools
🪛 ESLint

[error] 19-19: Insert ······

(prettier/prettier)


[error] 22-22: Insert ,⏎····

(prettier/prettier)


85-89: Enhanced promise handling for render function results

The code now properly handles promises that resolve to either strings or React components, providing better support for asynchronous rendering. This is a necessary improvement for RSC support.

Fix the missing semicolon:

return (renderFunctionResult as Promise<string | ReactComponent>).then((result) => {
  if (typeof result === 'string') return result;
-  return createReactElementFromRenderFunction(result, name, props)
+  return createReactElementFromRenderFunction(result, name, props);
});
🧰 Tools
🪛 ESLint

[error] 87-87: Insert ;

(prettier/prettier)


91-91: Improved consistency in React element creation

Using the new utility function here ensures consistent handling of React elements across the codebase.

node_package/tests/streamServerRenderedReactComponent.test.jsx (5)

63-97: Comprehensive test setup for different component types

The setupStreamTest function has been enhanced to support different component registration strategies, making it more versatile for testing various component types. This is a good improvement for test coverage.


206-222: New tests for render functions that resolve to React components

These tests validate that both synchronous and asynchronous render functions that return React components work correctly with the streaming functionality. They appropriately check for HTML content, console scripts, and error states.

🧰 Tools
🪛 ESLint

[error] 210-210: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


224-254: Added tests for handling errors in render functions

These tests verify that both synchronous and asynchronous errors in render functions are properly handled. They check that error messages are included in the output and that the error state is correctly set.

🧰 Tools
🪛 ESLint

[error] 228-228: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 242-242: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


256-297: Remove .only to avoid skipping other tests

.only will cause Jest to skip all other tests. Remember to remove .only before merging, or your CI might miss regressions.

-  it.only.each(['erroneousRenderFunction', 'erroneousAsyncRenderFunction'])(
+  it.each(['erroneousRenderFunction', 'erroneousAsyncRenderFunction'])(
🧰 Tools
🪛 ESLint

[error] 260-260: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 282-282: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


299-322: New test for string results from promises

This test ensures that promises resolving to strings are properly handled in the streaming component. This is important for supporting different types of rendering results.

🧰 Tools
🪛 ESLint

[error] 316-316: Return values from promise executor functions cannot be read.

(no-promise-executor-return)

node_package/src/RSCClientRoot.ts (6)

3-4: Good use of disabling underscore-dangle for internal usage.

Since __FLIGHT_DATA is user-defined, disabling the rule is justified.


18-23: Global __FLIGHT_DATA property looks correct.

Declaring this property on the window object ensures it's accessible for client-side streaming logic.


79-83: Stream creation and transformation approach is solid.

The function neatly composes the stream, transforms it, and returns a React-friendly stream consumer for RSC data.

🧰 Tools
🪛 ESLint

[error] 83-83: Insert ;

(prettier/prettier)


111-111: Hydration path for non-empty DOM content appears correct.

Using hydrateRoot ensures React reuses existing server-rendered markup.


114-114: Fallback path for empty DOM content is properly handled.

Falling back to fetchRSC for initial rendering is a sensible approach.

🧰 Tools
🪛 ESLint

[error] 114-114: Insert ;

(prettier/prettier)


46-77:

❓ Verification inconclusive

Confirm streaming closure timing to avoid missed chunks.

When the DOM is ready (or if it’s already loaded), the stream is closed. If new chunks arrive after the stream closes, they could be lost. Please confirm if this aligns with the intended design.


🏁 Script executed:

#!/bin/bash
# Description: Verify usage of `createRSCStreamFromPage` to ensure no leftover chunks arrive post-closure.

rg -A 15 "createRSCStreamFromPage"

Length of output: 2377


Confirm Stream Closure Timing in createRSCStreamFromPage

The implementation currently closes the stream when the DOM is ready—either immediately if the document has already loaded or upon the DOMContentLoaded event if it is still loading. This means that any chunks pushed to window.__FLIGHT_DATA after the stream is closed would be lost. Please confirm that this behavior is intentional and aligns with the design goals, or if there should be additional logic to handle any late-arriving chunks.

🧰 Tools
🪛 ESLint

[error] 65-65: Insert ,

(prettier/prettier)


[error] 77-77: Insert ;

(prettier/prettier)

node_package/src/RSCPayloadContainer.ts (6)

3-6: Chunk type definition is straightforward.

Describing each chunk’s content and last-chunk status clarifies the streaming model.

🧰 Tools
🪛 ESLint

[error] 6-6: Insert ;

(prettier/prettier)


8-10: Props interface defined clearly for RSC payload streams.

No issues noted; it cleanly represents external input.

🧰 Tools
🪛 ESLint

[error] 10-10: Insert ;

(prettier/prettier)


12-15: Internal props interface is well-defined.

Structuring the chunk index and promise retrieval function is clear.

🧰 Tools
🪛 ESLint

[error] 15-15: Insert ;

(prettier/prettier)


17-21: Partial script escaping looks good, but confirm comprehensive sanitization.

escapeScript mitigates simple script tags, but it might not handle all potential injection vectors. Consider a robust sanitizer if untrusted data can flow here.

🧰 Tools
🪛 ESLint

[error] 18-20: Replace ⏎····.replace(/<!--/g,·'<\\!--')⏎···· with .replace(/<!--/g,·'<\\!--')

(prettier/prettier)


29-34: Potential XSS risk with dangerouslySetInnerHTML.

This repeats a previously noted concern to confirm data is sanitized before injection.

🧰 Tools
🪛 Biome (1.9.4)

[error] 30-30: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)


53-100: Recursive Suspense structure effectively handles streaming.

Promises array and event handlers seem correct. Ensure large payloads won’t cause memory issues with an ever-growing array.

🧰 Tools
🪛 ESLint

[error] 87-87: Insert ⏎····

(prettier/prettier)


[error] 88-88: Replace ···· with ······

(prettier/prettier)


[error] 89-89: Insert ··

(prettier/prettier)


[error] 90-90: Insert ··

(prettier/prettier)


[error] 92-92: Replace ···· with ······

(prettier/prettier)


[error] 93-93: Replace },·[chunkPromises] with ··},⏎····[chunkPromises],⏎··

(prettier/prettier)

node_package/src/ReactOnRailsRSC.ts (3)

24-28: Ensure the Rails context is set properly.

Throwing an error if railsContext or reactClientManifestFileName is absent is helpful, but confirm that upstream code always sets these values.

🧰 Tools
🪛 ESLint

[error] 24-24: Replace reactRenderingResult:·ReactElement·|·Promise<ReactElement·|·string>,·options:·RSCRenderParams with ⏎··reactRenderingResult:·ReactElement·|·Promise<ReactElement·|·string>,⏎··options:·RSCRenderParams,⏎

(prettier/prettier)


38-39: Concurrently loading manifest and rendering result is efficient.

Combining promises is a good performance optimization. No issues here.


72-72: isRSCBundle flag is introduced successfully.

Double-check that any subsequent logic depending on isRSCBundle is updated accordingly.

node_package/src/RSCServerRoot.ts (1)

61-76: Good error handling for missing context properties.

The component correctly validates required context properties early and provides detailed error messages to help troubleshoot configuration issues.

🧰 Tools
🪛 ESLint

[error] 61-61: Replace {·componentName,·componentProps·}:·RSCServerRootProps,·railsContext?:·RailsContext with ⏎··{·componentName,·componentProps·}:·RSCServerRootProps,⏎··railsContext?:·RailsContext,⏎

(prettier/prettier)


[error] 62-62: Replace !railsContext?.serverSide·||·!railsContext?.reactClientManifestFileName·||·!railsContext?.reactServerClientManifestFileName with ⏎····!railsContext?.serverSide·||⏎····!railsContext?.reactClientManifestFileName·||⏎····!railsContext?.reactServerClientManifestFileName⏎··

(prettier/prettier)


[error] 64-64: Insert ⏎········

(prettier/prettier)


[error] 65-65: Replace ······ with ········

(prettier/prettier)


[error] 66-66: Replace 'as·stated·in·the·React·Server·Component·tutorial.·The·received·rails·context·is:·'}${··JSON.stringify(railsContext)}`` with ··'as·stated·in·the·React·Server·Component·tutorial.·The·received·rails·context·is:·'⏎······}${JSON.stringify(railsContext)},

(prettier/prettier)


[error] 73-73: Replace ······ with ········

(prettier/prettier)


[error] 74-74: Replace 'is·set·to·true.' with ··'is·set·to·true.',

(prettier/prettier)

node_package/src/serverRenderReactComponent.ts (2)

56-72: Good enhancement to handle React elements in promises.

The processPromise function has been updated to handle promises that resolve to either strings or React elements. This enhancement increases flexibility in component rendering and allows for more advanced use cases.

The implementation correctly differentiates between string and React element results and processes them accordingly. Good error message for ExecJS renderer compatibility.

🧰 Tools
🪛 ESLint

[error] 61-61: Replace 'Your·render·function·returned·a·Promise,·which·is·only·supported·by·a·node·renderer,·not·ExecJS.' with ⏎······'Your·render·function·returned·a·Promise,·which·is·only·supported·by·a·node·renderer,·not·ExecJS.',⏎····

(prettier/prettier)


96-100: Type signature updated correctly for promise handling.

The createPromiseResult function signature has been updated to reflect the new promise type that can resolve to either a string or a React element, maintaining consistency with the changes to processPromise.

node_package/src/types/index.ts (3)

17-48: Good use of discriminated union type for server/client context separation.

The refactoring of RailsContext from an interface to a discriminated union type is well-designed, providing clear separation between server-side and client-side contexts with their specific requirements. The detailed comments for the server-side properties are particularly helpful.

This makes the type safer by ensuring that server-side specific properties are only available when serverSide is true, preventing potential runtime errors.

🧰 Tools
🪛 ESLint

[error] 35-35: Insert ⏎··|·

(prettier/prettier)


[error] 36-36: Insert ····

(prettier/prettier)


[error] 37-37: Replace } with ····}⏎·

(prettier/prettier)


[error] 38-38: Insert ····

(prettier/prettier)


[error] 39-39: Insert ····

(prettier/prettier)


[error] 40-40: Insert ····

(prettier/prettier)


[error] 41-41: Insert ····

(prettier/prettier)


[error] 42-42: Insert ····

(prettier/prettier)


[error] 43-43: Insert ····

(prettier/prettier)


[error] 44-44: Insert ····

(prettier/prettier)


[error] 45-45: Insert ····

(prettier/prettier)


[error] 46-46: Insert ····

(prettier/prettier)


[error] 47-47: Insert ····

(prettier/prettier)


[error] 48-48: Replace } with ····}⏎

(prettier/prettier)


65-67: Type enhancements for better promise support.

The types CreateReactOutputResult and RenderFunctionResult have been updated to support promises that resolve to React elements, which aligns with the implementation changes in serverRenderReactComponent.ts. This ensures type safety when using these enhanced promise capabilities.


217-217: Added isRSCBundle property for RSC context identification.

The new isRSCBundle property in the ReactOnRails interface will help identify when code is executing in an RSC bundle context, which is crucial for conditional logic that needs to behave differently in those environments.

node_package/src/streamServerRenderedReactComponent.ts (7)

91-99: Good implementation of console replay chunk building.

The extracted buildConsoleReplayChunk function improves code organization and reusability. It efficiently handles console output capture and prevents sending empty replay scripts. The function correctly tracks previously replayed messages to avoid duplicates.

🧰 Tools
🪛 ESLint

[error] 99-99: Insert ;

(prettier/prettier)


108-115: Improved console replay with timeout and cleanup.

Using a timeout to batch console replay messages is more efficient than sending them immediately with each chunk. The code correctly clears the previous timeout before setting a new one to prevent memory leaks.


119-126: Added flush method ensures all console logs are captured.

The new flush method ensures that any remaining console replay messages are sent before the stream ends, preventing loss of debugging information. This is an important addition for stream transformation.


149-149: Enhanced function signature for better promise support.

The streamRenderReactComponent function now accepts both React elements and promises that resolve to either React elements or strings, providing more flexibility in component rendering.

🧰 Tools
🪛 ESLint

[error] 149-149: Replace reactRenderingResult:·ReactElement·|·Promise<ReactElement·|·string>,·options:·RenderParams with ⏎··reactRenderingResult:·ReactElement·|·Promise<ReactElement·|·string>,⏎··options:·RenderParams,⏎

(prettier/prettier)


160-172: Improved error handling with extracted function.

Error handling logic has been extracted into a separate onShellError function, which improves code readability and maintainability. The function correctly handles errors, updates the render state, and writes error HTML to the stream.

🧰 Tools
🪛 ESLint

[error] 172-172: Insert ;

(prettier/prettier)


174-206: Good promise handling with informative error messages.

The code now uses Promise.resolve to uniformly handle both React elements and promises. When a string is received instead of a React element, it provides a clear error message explaining why this is suboptimal for streaming and suggests not calling renderToString inside render functions.

🧰 Tools
🪛 ESLint

[error] 174-174: Replace .then(reactRenderedElement with ⏎····.then((reactRenderedElement)

(prettier/prettier)


[error] 175-175: Insert ··

(prettier/prettier)


[error] 176-176: Insert ··

(prettier/prettier)


[error] 177-177: Insert ··

(prettier/prettier)


[error] 178-179: Replace 'To·benefit·from·React·on·Rails·Pro·streaming·feature,·your·render·function·should·return·a·React·component.\n'·+·⏎········'Do·not·call·ReactDOMServer.renderToString()·inside·the·render·function·as·this·defeats·the·purpose·of·streaming.\n' with ····'To·benefit·from·React·on·Rails·Pro·streaming·feature,·your·render·function·should·return·a·React·component.\n'·+⏎············'Do·not·call·ReactDOMServer.renderToString()·inside·the·render·function·as·this·defeats·the·purpose·of·streaming.\n',

(prettier/prettier)


[error] 180-180: Insert ··

(prettier/prettier)


[error] 182-182: Replace ······ with ········

(prettier/prettier)


[error] 183-183: Insert ··

(prettier/prettier)


[error] 184-184: Insert ··

(prettier/prettier)


[error] 185-185: Insert ··

(prettier/prettier)


[error] 187-187: Insert ··

(prettier/prettier)


[error] 188-188: Insert ··

(prettier/prettier)


[error] 189-189: Insert ··

(prettier/prettier)


[error] 190-190: Insert ··

(prettier/prettier)


[error] 191-191: Insert ··

(prettier/prettier)


[error] 192-192: Insert ··

(prettier/prettier)


[error] 193-193: Insert ··

(prettier/prettier)


[error] 194-194: Insert ··

(prettier/prettier)


[error] 195-195: Insert ··

(prettier/prettier)


[error] 196-196: Replace ········ with ··········

(prettier/prettier)


[error] 197-197: Insert ··

(prettier/prettier)


[error] 198-198: Replace ········ with ··········

(prettier/prettier)


[error] 199-199: Insert ··

(prettier/prettier)


[error] 200-200: Replace ········ with ··········

(prettier/prettier)


[error] 201-201: Insert ··

(prettier/prettier)


[error] 202-202: Insert ··

(prettier/prettier)


[error] 203-203: Insert ··

(prettier/prettier)


[error] 204-204: Insert ··

(prettier/prettier)


[error] 205-205: Insert ··

(prettier/prettier)


[error] 206-206: Replace ··}) with ····})⏎····

(prettier/prettier)


240-254: Improved error handling with stream utilities.

The error handling in the catch block has been updated to use the stream transformation utilities, which provides consistent error reporting. The code correctly writes the error HTML to the stream and ends it properly.

🧰 Tools
🪛 ESLint

[error] 240-245: Replace ⏎······readableStream,⏎······writeChunk,⏎······emitError,⏎······endStream⏎····}·=·transformRenderStreamChunksToResultObject({·hasErrors:·true,·isShellReady:·false,·result:·null with ·readableStream,·writeChunk,·emitError,·endStream·}·=·transformRenderStreamChunksToResultObject({⏎······hasErrors:·true,⏎······isShellReady:·false,⏎······result:·null,⏎···

(prettier/prettier)

Comment on lines 82 to 103
const rscPayloadStream = await generateRSCPayload(
componentName,
componentProps,
railsContext.serverSideRSCPayloadParameters
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify error handling for generateRSCPayload function.

The code correctly calls generateRSCPayload with the required parameters, but there's no error handling if the function throws an error.

Let's check how errors from this function are handled:


🏁 Script executed:

#!/bin/bash
# Search for similar async function calls in the codebase to see how they're handling errors
rg -A 5 -B 5 "await .*Payload" --type ts

Length of output: 1546


Attention: Missing Error Handling for generateRSCPayload

It appears that the call to generateRSCPayload in node_package/src/RSCServerRoot.ts isn’t wrapped in any error handling. A similar pattern is observed in RSCClientRoot.ts where asynchronous payload-fetching functions are called without explicit error management. To make the code more robust, please consider introducing a try-catch block around the call. For example:

try {
  const rscPayloadStream = await generateRSCPayload(
    componentName,
    componentProps,
    railsContext.serverSideRSCPayloadParameters
  );
} catch (error) {
  // handle the error appropriately—log it, send a server error response, etc.
}

This change will ensure that any errors thrown by generateRSCPayload are properly caught and managed.

🧰 Tools
🪛 ESLint

[error] 85-85: Insert ,

(prettier/prettier)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (3)
node_package/src/RSCPayloadContainer.ts (1)

28-33: ⚠️ Potential issue

Potential XSS vulnerability with dangerouslySetInnerHTML.

Using dangerouslySetInnerHTML to inject JavaScript poses a security risk, even with basic escaping.

Consider alternatives:

  1. Store data in a safer format like a JSON attribute on a non-executable element
  2. Use a dedicated script loading mechanism with strict content validation
- const scriptElement = React.createElement('script', {
-   dangerouslySetInnerHTML: {
-     __html: escapeScript(`(self.__FLIGHT_DATA||=[]).push(${chunk.chunk})`),
-   },
-   key: `script-${chunkIndex}`,
- });
+ // Option 1: Store data as a data attribute and process it safely
+ const dataElement = React.createElement('div', {
+   'data-flight-chunk': JSON.stringify(chunk.chunk),
+   'data-flight-index': chunkIndex,
+   key: `data-${chunkIndex}`,
+   id: `flight-data-${chunkIndex}`,
+ });
+ 
+ // Then use a separate initialization script that safely processes these elements
🧰 Tools
🪛 Biome (1.9.4)

[error] 29-29: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

node_package/src/RSCServerRoot.ts (2)

38-67: ⚠️ Potential issue

SSR manifest creation needs error handling and hardcoded paths should be configurable.

This function has two important issues that were previously flagged but not addressed:

  1. There's no error handling if manifest files can't be loaded
  2. The webpack path "/webpack/development/" is hardcoded, which won't work in production environments
 const createSSRManifest = async (
   reactServerManifestFileName: string,
   reactClientManifestFileName: string,
 ) => {
-  const [reactServerManifest, reactClientManifest] = await Promise.all([
-    loadJsonFile(reactServerManifestFileName),
-    loadJsonFile(reactClientManifestFileName),
-  ]);
+  try {
+    const [reactServerManifest, reactClientManifest] = await Promise.all([
+      loadJsonFile(reactServerManifestFileName),
+      loadJsonFile(reactClientManifestFileName),
+    ]);
 
-  const ssrManifest = {
-    moduleLoading: {
-      prefix: '/webpack/development/',
-      crossOrigin: null,
-    },
-    moduleMap: {} as Record<string, unknown>,
-  };
+    // Get prefix from environment or fallback to default
+    const webpackPrefix = process.env.WEBPACK_PREFIX || '/webpack/development/';
+    
+    const ssrManifest = {
+      moduleLoading: {
+        prefix: webpackPrefix,
+        crossOrigin: null,
+      },
+      moduleMap: {} as Record<string, unknown>,
+    };

    Object.entries(reactClientManifest).forEach(([aboluteFileUrl, clientFileBundlingInfo]) => {
      const serverFileBundlingInfo = reactServerManifest[aboluteFileUrl];
      ssrManifest.moduleMap[(clientFileBundlingInfo as { id: string }).id] = {
        '*': {
          id: (serverFileBundlingInfo as { id: string }).id,
          chunks: (serverFileBundlingInfo as { chunks: string[] }).chunks,
          name: '*',
        },
      };
    });

    return ssrManifest;
+  } catch (error) {
+    throw new Error(`Failed to load or process manifest files: ${error instanceof Error ? error.message : String(error)}`);
+  }
 };

99-103: ⚠️ Potential issue

Missing Error Handling for generateRSCPayload

The call to generateRSCPayload isn't wrapped in any error handling, which was previously flagged. If the function fails, the error would be unhandled and could crash the application.

-  const rscPayloadStream = await generateRSCPayload(
-    componentName,
-    componentProps,
-    railsContext.serverSideRSCPayloadParameters,
-  );
+  try {
+    const rscPayloadStream = await generateRSCPayload(
+      componentName,
+      componentProps,
+      railsContext.serverSideRSCPayloadParameters,
+    );

And modify the rest of the function to be within this try block with appropriate error handling.

🧹 Nitpick comments (5)
node_package/src/ComponentRegistry.ts (1)

23-23: Remove Unnecessary Extra Whitespace
The extra space between component and .length on line 23 is flagged by the static analysis tools (prettier/prettier) and should be removed. Keeping the accessor tight improves readability and avoids linting issues.

-      const isRenderer = renderFunction && component .length === 3;
+      const isRenderer = renderFunction && component.length === 3;
🧰 Tools
🪛 ESLint

[error] 23-23: Delete ·

(prettier/prettier)

node_package/src/ReactOnRails.client.ts (1)

340-340: Consider using object spread operator instead of Object.assign.

The ESLint rule suggests using the modern object spread syntax for better readability.

- this.options = Object.assign({}, DEFAULT_OPTIONS);
+ this.options = { ...DEFAULT_OPTIONS };
🧰 Tools
🪛 ESLint

[error] 340-340: Use an object spread instead of Object.assign eg: { ...foo }.

(prefer-object-spread)

node_package/src/RSCPayloadContainer.ts (1)

17-19: Implement more comprehensive script content sanitization.

The current escapeScript function handles some basic escaping but may not cover all potential security issues.

Consider using a more comprehensive sanitization approach:

- function escapeScript(script: string) {
-   return script.replace(/<!--/g, '<\\!--').replace(/<\/(script)/gi, '</\\$1');
- }
+ function escapeScript(script: string) {
+   // Escape HTML comments, script tags, and other potentially dangerous content
+   return script
+     .replace(/<!--/g, '<\\!--')
+     .replace(/<\/(script)/gi, '</\\$1')
+     .replace(/</g, '\\u003C')
+     .replace(/>/g, '\\u003E')
+     .replace(/\//g, '\\u002F');
+ }
node_package/src/RSCServerRoot.ts (2)

1-8: Fix import order to comply with ESLint rules.

The stream import should occur before the type import according to the ESLint rule import/order. Consider reordering imports:

 import * as React from 'react';
 import { createFromNodeStream } from 'react-on-rails-rsc/client.node';
+import { PassThrough } from 'stream';
 import type { RenderFunction, RailsContext } from './types';
 import transformRSCStream from './transformRSCNodeStreamAndReplayConsoleLogs';
 import loadJsonFile from './loadJsonFile';
 import RSCPayloadContainer from './RSCPayloadContainer';
-import { PassThrough } from 'stream';
🧰 Tools
🪛 ESLint

[error] 7-7: stream import should occur before type import of ./types

(import/order)


112-119: Improve React element creation syntax

The current implementation creates React elements with React.createElement calls, which is verbose and harder to read. Consider using JSX for better readability if your project is configured for it.

If JSX is available, this could be simplified to:

return () => (
  <>
    <React.Fragment key="serverComponentElement">
      {use(serverComponentElement)}
    </React.Fragment>
    <RSCPayloadContainer
      RSCPayloadStream={rscPayloadStream2}
      key="rscPayloadContainer"
    />
  </>
);

If JSX is not an option, at least consider using the array spread operator for cleaner code:

 return () =>
   React.createElement(React.Fragment, null, [
-    React.createElement(React.Fragment, { key: 'serverComponentElement' }, use(serverComponentElement)),
-    React.createElement(RSCPayloadContainer, {
-      RSCPayloadStream: rscPayloadStream2,
-      key: 'rscPayloadContainer',
-    }),
+    React.createElement(
+      React.Fragment, 
+      { key: 'serverComponentElement' }, 
+      use(serverComponentElement)
+    ),
+    React.createElement(
+      RSCPayloadContainer, 
+      {
+        RSCPayloadStream: rscPayloadStream2,
+        key: 'rscPayloadContainer',
+      }
+    ),
   ]);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3b95637 and 5baf370.

📒 Files selected for processing (15)
  • node_package/src/ComponentRegistry.ts (1 hunks)
  • node_package/src/RSCClientRoot.ts (4 hunks)
  • node_package/src/RSCPayloadContainer.ts (1 hunks)
  • node_package/src/RSCServerRoot.ts (1 hunks)
  • node_package/src/ReactOnRails.client.ts (1 hunks)
  • node_package/src/ReactOnRailsRSC.ts (4 hunks)
  • node_package/src/buildConsoleReplay.ts (1 hunks)
  • node_package/src/createReactOutput.ts (2 hunks)
  • node_package/src/registerServerComponent/server.rsc.ts (1 hunks)
  • node_package/src/registerServerComponent/server.ts (2 hunks)
  • node_package/src/serverRenderReactComponent.ts (3 hunks)
  • node_package/src/streamServerRenderedReactComponent.ts (5 hunks)
  • node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts (1 hunks)
  • node_package/src/transformRSCStreamAndReplayConsoleLogs.ts (2 hunks)
  • node_package/src/types/index.ts (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (9)
  • node_package/src/registerServerComponent/server.ts
  • node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts
  • node_package/src/buildConsoleReplay.ts
  • node_package/src/registerServerComponent/server.rsc.ts
  • node_package/src/serverRenderReactComponent.ts
  • node_package/src/transformRSCStreamAndReplayConsoleLogs.ts
  • node_package/src/RSCClientRoot.ts
  • node_package/src/types/index.ts
  • node_package/src/streamServerRenderedReactComponent.ts
🧰 Additional context used
🧬 Code Definitions (2)
node_package/src/ReactOnRailsRSC.ts (2)
node_package/src/types/index.ts (2)
  • RSCRenderParams (151-153)
  • ReactOnRails (188-221)
node_package/src/loadJsonFile.ts (1)
  • loadJsonFile (7-25)
node_package/src/RSCServerRoot.ts (3)
node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts (1)
  • transformRSCStream (3-37)
node_package/src/loadJsonFile.ts (1)
  • loadJsonFile (7-25)
node_package/src/types/index.ts (2)
  • RenderFunction (109-109)
  • RailsContext (17-51)
🪛 GitHub Actions: Lint JS and Ruby
node_package/src/createReactOutput.ts

[warning] 11-11: Unused export 'createReactElementFromRenderFunction' detected.

🪛 Biome (1.9.4)
node_package/src/RSCPayloadContainer.ts

[error] 29-29: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

🪛 ESLint
node_package/src/ComponentRegistry.ts

[error] 23-23: Delete ·

(prettier/prettier)

node_package/src/RSCServerRoot.ts

[error] 7-7: stream import should occur before type import of ./types

(import/order)

node_package/src/ReactOnRails.client.ts

[error] 340-340: Use an object spread instead of Object.assign eg: { ...foo }.

(prefer-object-spread)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: dummy-app-integration-tests (oldest)
  • GitHub Check: dummy-app-integration-tests (newest)
🔇 Additional comments (12)
node_package/src/ComponentRegistry.ts (1)

1-3: Address Missing Type Import for RenderFunction
A previous review comment pointed out that RenderFunction was not imported, leading to build errors (e.g., TS2304). If this type is used or expected (possibly via related type definitions in the registry), please update the import statement accordingly. For example:

-import { type RegisteredComponent, type ReactComponentOrRenderFunction } from './types';
+import { type RegisteredComponent, type ReactComponentOrRenderFunction, type RenderFunction } from './types';
node_package/src/ReactOnRails.client.ts (1)

343-343: Good addition of the isRSCBundle flag to distinguish client-side bundle.

This flag correctly identifies that this is not an RSC bundle, which will help with conditional logic throughout the application related to server components.

node_package/src/createReactOutput.ts (3)

83-90: Improved promise handling for render function results.

The enhancement to properly handle promises that resolve to either strings or React components makes the code more robust. By checking the type of the resolved value and processing it accordingly, this change ensures consistent React element generation.


92-92: Code simplification using the new helper function.

Good refactoring to use the newly created helper function, which improves consistency and maintainability.


11-30:

✅ Verification successful

Good extraction of element creation logic into a separate function.

The new function createReactElementFromRenderFunction improves code reusability and consolidates the logic for handling render function results. The warning message for JSX return values is informative and helps with future React compatibility.

However, the pipeline shows a warning about this function being unused, despite it being used within the file. This could be a linting misconfiguration.


🏁 Script executed:

#!/bin/bash
# Check where createReactElementFromRenderFunction is actually used
grep -n "createReactElementFromRenderFunction" node_package/src/createReactOutput.ts

Length of output: 323


Confirmed: Function Usage is Correct & Lint Warning Likely a Misconfiguration

Good extraction of the element creation logic into a separate function. The grep results confirm that createReactElementFromRenderFunction is indeed defined (line 11) and used at lines 88 and 92 in the file. The pipeline warning about the function being unused appears to be a lint misconfiguration rather than an issue with the code. Please review your lint settings if the warning persists.

🧰 Tools
🪛 GitHub Actions: Lint JS and Ruby

[warning] 11-11: Unused export 'createReactElementFromRenderFunction' detected.

node_package/src/RSCPayloadContainer.ts (1)

49-99: Well-structured stream handling with proper React suspense integration.

The RSCPayloadContainerWrapper effectively manages streaming data with appropriate error handling and React suspense integration. The useState initialization with a function ensures the setup happens only once, and the callback for getChunkPromise is properly memoized.

node_package/src/ReactOnRailsRSC.ts (3)

24-31: Good enhancement to function signature and validation.

The updated function signature now properly handles both direct React elements and promises, making it more flexible. The validation of required context properties helps prevent runtime errors.


42-43: Efficient concurrent loading using Promise.all.

Using Promise.all to load the client manifest and resolve the rendering result concurrently is a good optimization that can improve performance.


76-76: Correct setting of isRSCBundle flag for server components.

Setting isRSCBundle to true here correctly identifies this as an RSC bundle, complementing the client-side setting. This flag will be essential for conditional logic throughout the application.

node_package/src/RSCServerRoot.ts (3)

22-26: Good React version check

The check for React.use availability ensures compatibility with the required React version, which is a good practice.


73-85: Good validation of required context properties

The validation ensures that all necessary context properties are present before proceeding, with helpful error messages that include the received context.


87-93: Good validation of generateRSCPayload availability

The check for the availability of the generateRSCPayload function provides a clear error message that guides the user on how to enable RSC support.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
node_package/src/createReactOutput.ts (2)

11-30: Good refactoring to centralize React element creation logic

This new helper function improves code organization by centralizing the logic for handling render function results, whether they're React elements or components.

However, consider using console.warn instead of console.error since this is a warning rather than a critical error. Also, the error message is quite verbose and could be more concise while maintaining clarity.

- console.error(
-   `Warning: ReactOnRails: Your registered render-function (ReactOnRails.register) for ${name}
- incorrectly returned a React Element (JSX). Instead, return a React Function Component by
- wrapping your JSX in a function. ReactOnRails v13 will throw error on this, as React Hooks do not
- work if you return JSX. Update by wrapping the result JSX of ${name} in a fat arrow function.`,
- );
+ console.warn(
+   `Warning: ReactOnRails: Render-function for '${name}' returned JSX directly instead of a Function Component. ` +
+   `This prevents React Hooks from working properly and will throw an error in ReactOnRails v13. ` +
+   `Wrap your JSX in a function instead.`
+ );

86-89: Improved promise handling for server components

The enhanced promise handling now correctly processes both string results (HTML) and React components, which addresses the issue of server components rendering twice as mentioned in the PR objectives.

For completeness, consider adding more robust error handling for unexpected result types:

return (renderFunctionResult as Promise<string | ReactComponent>).then((result) => {
  if (typeof result === 'string') return result;
+ if (result === null || result === undefined) {
+   console.error(`ReactOnRails: Render function for '${name}' resolved to ${result}`);
+   return null;
+ }
  return createReactElementFromRenderFunction(result, name, props);
});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5baf370 and cb1683d.

📒 Files selected for processing (3)
  • knip.ts (1 hunks)
  • node_package/src/createReactOutput.ts (2 hunks)
  • node_package/tests/streamServerRenderedReactComponent.test.jsx (3 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
node_package/src/createReactOutput.ts (7)
node_package/src/types/index.ts (1)
  • ReactComponent (107-107)
lib/generators/react_on_rails/templates/redux/base/app/javascript/bundles/HelloWorld/reducers/helloWorldReducer.js (1)
  • name (4-11)
spec/dummy/client/app/startup/HelloWorldProps.jsx (1)
  • name (8-8)
spec/dummy/client/app/startup/HelloWorldHooks.jsx (1)
  • name (8-8)
spec/dummy/client/app/store/composeInitialState.js (1)
  • props (3-3)
spec/dummy/client/app/startup/ReduxSharedStoreApp.client.jsx (1)
  • props (16-50)
spec/dummy/client/app/startup/HelloWorldApp.jsx (1)
  • props (14-14)
🪛 ESLint
node_package/tests/streamServerRenderedReactComponent.test.jsx

[error] 210-210: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 228-228: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 242-242: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 260-260: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 282-282: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 316-316: Return values from promise executor functions cannot be read.

(no-promise-executor-return)

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: rspec-package-tests (oldest)
  • GitHub Check: build-dummy-app-webpack-test-bundles (oldest)
  • GitHub Check: rspec-package-tests (newest)
🔇 Additional comments (7)
knip.ts (1)

12-12: Properly registered new RSC server component entry point.

This change correctly adds the new RSC-specific server component implementation file to the Knip configuration, ensuring it's properly tracked as a production dependency. This addition aligns with the PR objective of improving server component rendering.

node_package/tests/streamServerRenderedReactComponent.test.jsx (5)

63-97: Great addition of component type handling!

This refactoring supports testing various component registration strategies, making the tests more comprehensive. The switch statement cleanly handles different component types and provides proper error handling for unknown types.


173-173: Good adjustment for test resilience

Changing from exact length to toBeGreaterThanOrEqual makes the test more robust against implementation changes that might add extra chunks without breaking functionality.

Also applies to: 194-194


206-254: Comprehensive tests for rendering functions

These tests thoroughly validate that both synchronous and asynchronous render functions work correctly, including proper error handling. They reuse test logic efficiently with it.each.

🧰 Tools
🪛 ESLint

[error] 210-210: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 228-228: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 242-242: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


256-297: Thorough error handling tests

These tests ensure errors in render functions (both sync and async) are properly caught and reported, with correct validation of error messages and error emission behavior.

🧰 Tools
🪛 ESLint

[error] 260-260: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


[error] 282-282: Return values from promise executor functions cannot be read.

(no-promise-executor-return)


299-322: Good test for Promise resolving to string

This test validates an important edge case where a component returns a Promise that resolves to a string rather than a React element.

🧰 Tools
🪛 ESLint

[error] 316-316: Return values from promise executor functions cannot be read.

(no-promise-executor-return)

node_package/src/createReactOutput.ts (1)

92-92: Consistent use of the helper function

Good refactoring to consistently use the new helper function for non-promise results as well.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

♻️ Duplicate comments (1)
node_package/src/RSCPayloadContainer.ts (1)

28-33: ⚠️ Potential issue

Security concerns with dangerouslySetInnerHTML remain unaddressed

The current implementation uses dangerouslySetInnerHTML to inject streamed content into a script tag, which poses XSS risks despite the escapeScript function. The escaping only handles comment tags and script closing tags but doesn't protect against all injection vectors.

Consider alternatives like:

  1. Using JSON.stringify for the chunk data before injection
  2. Encoding the data as base64 and storing in a data attribute
  3. Implementing more comprehensive sanitization
const scriptElement = React.createElement('script', {
  dangerouslySetInnerHTML: {
-    __html: escapeScript(`(self.__FLIGHT_DATA||=[]).push(${chunk.chunk})`),
+    __html: `(self.__FLIGHT_DATA||=[]).push(${JSON.stringify(chunk.chunk)})`,
  },
  key: `script-${chunkIndex}`,
});
🧰 Tools
🪛 Biome (1.9.4)

[error] 29-29: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

🧹 Nitpick comments (3)
node_package/src/RSCPayloadContainer.ts (3)

50-81: Potential memory leak with unbounded promise array

The chunkPromises array grows indefinitely as new chunks arrive, which could lead to memory issues for large or continuous streams.

Consider adding a cleanup mechanism for resolved promises, especially if this component might be used with large datasets or long-lived connections. One approach would be to replace resolved promises with null after they're consumed, allowing them to be garbage collected.


65-78: Improve stream error handling

The current implementation creates a new promise after handling an error, which might lead to unexpected behavior if the stream continues to emit data after errors.

Consider adding more robust error handling:

  1. Add error logging
  2. Implement a timeout mechanism for when streams stop without ending
  3. Consider adding a maximum chunk count to prevent DoS attacks
RSCPayloadStream.on('error', (error) => {
+  console.error('React on Rails: Error in RSC payload stream', error);
  rejectCurrentPromise(error);
  createNewPromise();
});

71-74: Consider graceful recovery from stream errors

Currently, after a stream error, the code creates a new promise but doesn't indicate to the user that an error occurred or attempt to reconnect.

Implement a more user-friendly approach to stream errors, such as:

  1. A visible error state in the UI
  2. A retry mechanism for transient errors
  3. A fallback rendering option when streams fail
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cb1683d and 009f69e.

📒 Files selected for processing (9)
  • node_package/src/RSCClientRoot.ts (3 hunks)
  • node_package/src/RSCPayloadContainer.ts (1 hunks)
  • node_package/src/RSCServerRoot.ts (1 hunks)
  • node_package/src/ReactOnRails.client.ts (1 hunks)
  • node_package/src/createReactOutput.ts (2 hunks)
  • node_package/src/loadJsonFile.ts (1 hunks)
  • node_package/src/registerServerComponent/server.ts (2 hunks)
  • node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts (1 hunks)
  • node_package/tests/streamServerRenderedReactComponent.test.jsx (6 hunks)
🚧 Files skipped from review as they are similar to previous changes (7)
  • node_package/src/loadJsonFile.ts
  • node_package/src/ReactOnRails.client.ts
  • node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts
  • node_package/src/createReactOutput.ts
  • node_package/src/RSCClientRoot.ts
  • node_package/src/RSCServerRoot.ts
  • node_package/src/registerServerComponent/server.ts
🧰 Additional context used
🪛 Biome (1.9.4)
node_package/src/RSCPayloadContainer.ts

[error] 29-29: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: dummy-app-integration-tests (newest)
  • GitHub Check: dummy-app-integration-tests (oldest)
🔇 Additional comments (11)
node_package/tests/streamServerRenderedReactComponent.test.jsx (9)

63-97: Excellent implementation of componentType parameter for flexible component testing

This enhancement allows for comprehensive testing of various component registration strategies, which is crucial for RSC support. The switch statement is well-structured with clear cases for each component type and proper error handling for unknown types.


173-173: Improved test flexibility with toBeGreaterThanOrEqual(2)

This change makes the tests more robust by acknowledging that the exact number of chunks may vary, while still ensuring the minimum required chunks are present. This prevents tests from becoming brittle when the implementation details change.

Also applies to: 194-194, 250-250


179-179: Updated error message expectations for accuracy

The modified expectations better match the actual error messages produced by the component, ensuring the tests correctly validate error handling behavior.

Also applies to: 200-200, 255-255


206-224: Good test coverage for render function components

This test ensures that both synchronous and asynchronous render functions correctly produce the expected output in the stream. This is important for ensuring compatibility with various component declaration styles.


226-240: Comprehensive testing of synchronous errors

This test validates that error handling works correctly when components throw synchronous errors, ensuring the error information is properly included in the stream output.


242-260: Thorough handling of asynchronous errors

This test verifies that errors occurring in asynchronous code paths are properly captured and included in the stream. This is crucial for debugging React suspense-related issues.


262-282: Error handling for erroneous render functions

This test confirms that errors thrown directly by render functions (rather than by the components they return) are properly handled. The dynamic error message selection based on component type is a nice touch.


284-307: Verification of error emission when throwJsErrors is true

This test ensures that when configured to throw JS errors, the stream emits the error event correctly. This is important for integration with error handling middleware.


309-334: Support for string Promise components

This test validates an important use case where components return a Promise resolving to a string instead of a React element. This is essential for supporting various RSC payload formats.

node_package/src/RSCPayloadContainer.ts (2)

3-15: Types are well-defined

The type definitions clearly represent the structure and relationships between components. Good job defining specific types for the different props and data structures.


39-46: Efficient recursive rendering approach

The recursive rendering pattern with Suspense is a clean approach to handling the streaming nature of the data. This implementation will progressively render chunks as they arrive.


createNewPromise();
RSCPayloadStream.on('data', (streamChunk) => {
resolveCurrentPromise({ chunk: decoder.decode(streamChunk as Uint8Array), isLastChunk: false });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Validate chunk data before processing

The current implementation directly uses the decoded stream chunk without validation, which could lead to issues if malformed data is received.

Add validation for the chunk data to ensure it's valid JavaScript that can be safely evaluated:

- resolveCurrentPromise({ chunk: decoder.decode(streamChunk as Uint8Array), isLastChunk: false });
+ const decodedChunk = decoder.decode(streamChunk as Uint8Array);
+ try {
+   // Simple validation - attempt to parse as JSON to ensure it's valid
+   JSON.parse(decodedChunk);
+   resolveCurrentPromise({ chunk: decodedChunk, isLastChunk: false });
+ } catch (e) {
+   rejectCurrentPromise(new Error('Invalid chunk data received'));
+ }

Note: This assumes the chunks are valid JSON. If they're in a different format, adjust the validation accordingly.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
resolveCurrentPromise({ chunk: decoder.decode(streamChunk as Uint8Array), isLastChunk: false });
const decodedChunk = decoder.decode(streamChunk as Uint8Array);
try {
// Simple validation - attempt to parse as JSON to ensure it's valid
JSON.parse(decodedChunk);
resolveCurrentPromise({ chunk: decodedChunk, isLastChunk: false });
} catch (e) {
rejectCurrentPromise(new Error('Invalid chunk data received'));
}

@AbanoubGhadban AbanoubGhadban force-pushed the abanoubghadban/pro465/use-rsc-payload-to-render-server-components-on-server branch from 9acaf78 to 20a71db Compare April 4, 2025 10:05
@AbanoubGhadban AbanoubGhadban changed the base branch from master to abanoubghadban/ror1719/add-support-for-async-render-function-returns-component April 4, 2025 10:06
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (4)
node_package/tests/streamServerRenderedReactComponent.test.jsx (1)

118-118: 🛠️ Refactor suggestion

Bug fix: Using once('end', resolve) instead of on('end', resolve)

Using once() instead of on() prevents potential memory leaks by ensuring the callback is removed after it's executed. This is the correct approach for one-time events like 'end'.

Also applies to: 137-137, 153-153, 169-169, 190-190, 211-211, 231-231, 247-247, 267-267, 291-291, 327-327

node_package/src/RSCPayloadContainer.ts (3)

28-33: ⚠️ Potential issue

Potential XSS risk with dangerouslySetInnerHTML.

Though you're escaping script tags, confirm that the data injected into __FLIGHT_DATA is fully sanitized to prevent malicious scripts.

I recommend using a safer approach to inject the data. Consider:

- const scriptElement = React.createElement('script', {
-   dangerouslySetInnerHTML: {
-     __html: escapeScript(`(self.__FLIGHT_DATA||=[]).push(${chunk.chunk})`),
-   },
-   key: `script-${chunkIndex}`,
- });
+ // Create a data element that doesn't execute code
+ const scriptElement = React.createElement('script', {
+   type: 'application/json',
+   id: `flight-data-${chunkIndex}`,
+   key: `script-${chunkIndex}`,
+   dangerouslySetInnerHTML: {
+     __html: escapeScript(chunk.chunk),
+   },
+ });
+ 
+ // Then create a separate script to process it
+ const processScript = React.createElement('script', {
+   key: `process-${chunkIndex}`,
+   dangerouslySetInnerHTML: {
+     __html: escapeScript(`
+       (function() {
+         const data = document.getElementById('flight-data-${chunkIndex}').textContent;
+         (self.__FLIGHT_DATA||=[]).push(JSON.parse(data));
+       })();
+     `),
+   },
+ });

If you need to maintain the current approach, at least add validation before injecting:

+ // Validate that chunk.chunk is valid JSON before injecting
+ try {
+   JSON.parse(chunk.chunk);
+ } catch (e) {
+   console.error('Invalid JSON in chunk:', e);
+   return null;
+ }
🧰 Tools
🪛 Biome (1.9.4)

[error] 29-29: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)


67-67: 🛠️ Refactor suggestion

Validate chunk data before processing.

The current implementation directly uses the decoded stream chunk without validation, which could lead to issues if malformed data is received.

Add validation for the chunk data to ensure it's valid JavaScript that can be safely evaluated:

- resolveCurrentPromise({ chunk: decoder.decode(streamChunk as Uint8Array), isLastChunk: false });
+ const decodedChunk = decoder.decode(streamChunk as Uint8Array);
+ try {
+   // Simple validation - attempt to parse as JSON to ensure it's valid
+   JSON.parse(decodedChunk);
+   resolveCurrentPromise({ chunk: decodedChunk, isLastChunk: false });
+ } catch (e) {
+   rejectCurrentPromise(new Error('Invalid chunk data received'));
+ }

85-87: ⚠️ Potential issue

Fix array bounds check logic.

The current bounds check has an off-by-one error. JavaScript arrays are zero-indexed, so the valid indices are 0 to length-1.

- if (chunkIndex > chunkPromises.length) {
+ if (chunkIndex >= chunkPromises.length) {
    throw new Error('React on Rails Error: RSC Chunk index out of bounds');
  }
🧹 Nitpick comments (8)
docs/javascript/render-functions.md (1)

1-342: Excellent comprehensive documentation

This is a great addition that thoroughly documents render functions, their types, and how to use them with Ruby helper methods. The examples cover all the use cases and return types clearly.

Minor formatting improvements:

  1. Fix trailing punctuation in headings (lines 144, 150, 178, 185, 190)
  2. Consider adding spacing after numbered list markers in lines 9-10
-1. `props`: The props passed from the Ruby helper methods (via the `props:` parameter), which become available in your JavaScript.
-2. `railsContext`: Rails contextual information like current pathname, locale, etc. See the [Render-Functions and the Rails Context](https://www.shakacode.com/react-on-rails/docs/guides/render-functions-and-railscontext/) documentation for more details.
+1. `props`: The props passed from the Ruby helper methods (via the `props:` parameter), which become available in your JavaScript.
+
+2. `railsContext`: Rails contextual information like current pathname, locale, etc. See the [Render-Functions and the Rails Context](https://www.shakacode.com/react-on-rails/docs/guides/render-functions-and-railscontext/) documentation for more details.
🧰 Tools
🪛 LanguageTool

[uncategorized] ~9-~9: Loose punctuation mark.
Context: ...nctions take two parameters: 1. props: The props passed from the Ruby helper m...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~10-~10: Loose punctuation mark.
Context: ...le in your JavaScript. 2. railsContext: Rails contextual information like curre...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~126-~126: Loose punctuation mark.
Context: ...s an error. Always wrap HTML strings in { renderedHtml: '...' }. 2. **Objects Require Specific Proper...

(UNLIKELY_OPENING_PUNCTUATION)


[misspelling] ~192-~192: Did you mean “and”?
Context: ...nts: - The render function MUST return an object - The object MUST include a `com...

(AN_AND)

🪛 markdownlint-cli2 (0.17.2)

144-144: Trailing punctuation in heading
Punctuation: ':'

(MD026, no-trailing-punctuation)


150-150: Trailing punctuation in heading
Punctuation: ':'

(MD026, no-trailing-punctuation)


178-178: Trailing punctuation in heading
Punctuation: ':'

(MD026, no-trailing-punctuation)


185-185: Trailing punctuation in heading
Punctuation: ':'

(MD026, no-trailing-punctuation)


190-190: Trailing punctuation in heading
Punctuation: ':'

(MD026, no-trailing-punctuation)

node_package/src/RSCClientRoot.ts (5)

3-4: Document the reason for disabling the ESLint rule.

Adding a comment explaining why the no-underscore-dangle rule needs to be disabled would improve code maintainability.

-/* eslint-disable no-underscore-dangle */
+/* eslint-disable no-underscore-dangle */
+// Disabled because we need to access and modify the __FLIGHT_DATA property on window

18-22: Document the purpose of the global Window interface extension.

Adding documentation about what __FLIGHT_DATA is used for would help other developers understand its purpose and expected usage.

 declare global {
   interface Window {
+    /**
+     * Global array that stores flight data chunks received from the server
+     * This is used to hydrate React Server Components on the client
+     */
     __FLIGHT_DATA: unknown[];
   }
 }

46-77: Consider adding cleanup logic and improve type safety.

The current implementation might lead to memory leaks if the stream controller isn't properly cleaned up. Additionally, the type of chunk data should be more specific than unknown.

 const createRSCStreamFromPage = () => {
-  let streamController: ReadableStreamController<RSCPayloadChunk> | undefined;
+  let streamController: ReadableStreamDefaultController<RSCPayloadChunk> | undefined;
   const stream = new ReadableStream<RSCPayloadChunk>({
     start(controller) {
       if (typeof window === 'undefined') {
         return;
       }
-      const handleChunk = (chunk: unknown) => {
+      const handleChunk = (chunk: unknown) => {
+        // Validate chunk before enqueueing
+        if (chunk && typeof chunk === 'object') {
           controller.enqueue(chunk as RSCPayloadChunk);
+        } else {
+          console.error('Invalid RSC payload chunk received:', chunk);
+        }
       };
       if (!window.__FLIGHT_DATA) {
         window.__FLIGHT_DATA = [];
       }
       window.__FLIGHT_DATA.forEach(handleChunk);
-      window.__FLIGHT_DATA.push = (...chunks: unknown[]) => {
+      const originalPush = Array.prototype.push;
+      window.__FLIGHT_DATA.push = function(...chunks: unknown[]) {
         chunks.forEach(handleChunk);
-        return chunks.length;
+        return originalPush.apply(this, chunks);
       };
       streamController = controller;
     },
+    cancel() {
+      // Restore original push method on cleanup
+      if (typeof window !== 'undefined' && window.__FLIGHT_DATA) {
+        Object.setPrototypeOf(window.__FLIGHT_DATA, Array.prototype);
+      }
+    }
   });

79-83: Add error handling for stream creation.

The current implementation doesn't handle potential errors that could occur during stream transformation.

 const createFromRSCStream = () => {
-  const stream = createRSCStreamFromPage();
-  const transformedStream = transformRSCStreamAndReplayConsoleLogs(stream);
-  return createFromReadableStream<React.ReactNode>(transformedStream);
+  try {
+    const stream = createRSCStreamFromPage();
+    const transformedStream = transformRSCStreamAndReplayConsoleLogs(stream);
+    return createFromReadableStream<React.ReactNode>(transformedStream);
+  } catch (error) {
+    console.error('Failed to create RSC stream:', error);
+    // Return a promise that resolves to an error component
+    return Promise.resolve(React.createElement('div', { 
+      style: { color: 'red' } 
+    }, 'Error loading server components'));
+  }
 };

111-112: Add error handling for the hydration process.

When switching to server component hydration, the code should handle potential errors that might occur during the process.

-    const root = await createFromRSCStream();
-    ReactDOMClient.hydrateRoot(domNode, root);
+    try {
+      const root = await createFromRSCStream();
+      ReactDOMClient.hydrateRoot(domNode, root);
+    } catch (error) {
+      console.error('Failed to hydrate RSC from server:', error);
+      const root = await fetchRSC({ componentName, rscPayloadGenerationUrlPath, componentProps });
+      ReactDOMClient.createRoot(domNode).render(root);
+    }
node_package/src/isServerRenderResult.ts (1)

19-21: Consider adding JSDoc to document the purpose of the updated function.

Adding documentation would help clarify the purpose of the updated function signature.

+/**
+ * Type guard function that checks if a value is a Promise.
+ * Supports various input types that might be returned from render functions.
+ * 
+ * @param testValue The value to check
+ * @returns A type predicate indicating if the value is a Promise
+ */
 export function isPromise<T>(
   testValue: CreateReactOutputResult | RenderFunctionResult | Promise<T> | RenderStateHtml | string | null,
 ): testValue is Promise<T> {
node_package/tests/serverRenderReactComponent.test.ts (1)

183-207: Consider additional test for error case in async rendering.

While the test coverage is good, consider adding a specific test for error handling in async render functions.

it('serverRenderReactComponent handles errors in async render functions', async () => {
  const errorMessage = 'Async render error';
  const X9: RenderFunction = () => Promise.reject(new Error(errorMessage));
  X9.renderFunction = true;

  ComponentRegistry.register({ X9 });

  const renderResult = serverRenderReactComponent({
    name: 'X9',
    domNodeId: 'myDomId',
    trace: false,
    throwJsErrors: false,
    renderingReturnsPromises: true,
  });
  assertIsPromise(renderResult);
  
  const result = await renderResult;
  expect(result.hasErrors).toBeTruthy();
  expect(result.html).toContain(errorMessage);
  expect(result.html).toContain('Exception in rendering!');
});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9acaf78 and 20a71db.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (30)
  • docs/javascript/render-functions.md (1 hunks)
  • eslint.config.ts (1 hunks)
  • knip.ts (1 hunks)
  • lib/react_on_rails/configuration.rb (6 hunks)
  • lib/react_on_rails/helper.rb (2 hunks)
  • lib/react_on_rails/test_helper/webpack_assets_status_checker.rb (1 hunks)
  • lib/react_on_rails/utils.rb (1 hunks)
  • node_package/src/RSCClientRoot.ts (3 hunks)
  • node_package/src/RSCPayloadContainer.ts (1 hunks)
  • node_package/src/RSCServerRoot.ts (1 hunks)
  • node_package/src/ReactOnRails.client.ts (1 hunks)
  • node_package/src/ReactOnRailsRSC.ts (5 hunks)
  • node_package/src/buildConsoleReplay.ts (1 hunks)
  • node_package/src/createReactOutput.ts (2 hunks)
  • node_package/src/handleError.ts (1 hunks)
  • node_package/src/isServerRenderResult.ts (2 hunks)
  • node_package/src/loadJsonFile.ts (1 hunks)
  • node_package/src/loadReactClientManifest.ts (0 hunks)
  • node_package/src/registerServerComponent/server.rsc.ts (1 hunks)
  • node_package/src/registerServerComponent/server.ts (2 hunks)
  • node_package/src/serverRenderReactComponent.ts (6 hunks)
  • node_package/src/serverRenderUtils.ts (1 hunks)
  • node_package/src/streamServerRenderedReactComponent.ts (5 hunks)
  • node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts (1 hunks)
  • node_package/src/transformRSCStreamAndReplayConsoleLogs.ts (2 hunks)
  • node_package/src/types/index.ts (6 hunks)
  • node_package/tests/serverRenderReactComponent.test.js (0 hunks)
  • node_package/tests/serverRenderReactComponent.test.ts (1 hunks)
  • node_package/tests/streamServerRenderedReactComponent.test.jsx (6 hunks)
  • package.json (2 hunks)
💤 Files with no reviewable changes (2)
  • node_package/tests/serverRenderReactComponent.test.js
  • node_package/src/loadReactClientManifest.ts
🚧 Files skipped from review as they are similar to previous changes (17)
  • knip.ts
  • lib/react_on_rails/test_helper/webpack_assets_status_checker.rb
  • node_package/src/ReactOnRails.client.ts
  • node_package/src/handleError.ts
  • lib/react_on_rails/utils.rb
  • lib/react_on_rails/helper.rb
  • node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts
  • node_package/src/registerServerComponent/server.rsc.ts
  • node_package/src/registerServerComponent/server.ts
  • node_package/src/loadJsonFile.ts
  • node_package/src/transformRSCStreamAndReplayConsoleLogs.ts
  • package.json
  • node_package/src/buildConsoleReplay.ts
  • node_package/src/ReactOnRailsRSC.ts
  • node_package/src/RSCServerRoot.ts
  • node_package/src/createReactOutput.ts
  • node_package/src/types/index.ts
🧰 Additional context used
🧬 Code Definitions (2)
node_package/src/serverRenderUtils.ts (1)
node_package/src/types/index.ts (1)
  • FinalHtmlResult (195-195)
node_package/src/RSCClientRoot.ts (2)
node_package/src/types/index.ts (1)
  • RSCPayloadChunk (205-207)
node_package/src/transformRSCStreamAndReplayConsoleLogs.ts (1)
  • transformRSCStreamAndReplayConsoleLogs (3-60)
🪛 LanguageTool
docs/javascript/render-functions.md

[uncategorized] ~9-~9: Loose punctuation mark.
Context: ...nctions take two parameters: 1. props: The props passed from the Ruby helper m...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~10-~10: Loose punctuation mark.
Context: ...le in your JavaScript. 2. railsContext: Rails contextual information like curre...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~126-~126: Loose punctuation mark.
Context: ...s an error. Always wrap HTML strings in { renderedHtml: '...' }. 2. **Objects Require Specific Proper...

(UNLIKELY_OPENING_PUNCTUATION)


[misspelling] ~192-~192: Did you mean “and”?
Context: ...nts: - The render function MUST return an object - The object MUST include a `com...

(AN_AND)

🪛 markdownlint-cli2 (0.17.2)
docs/javascript/render-functions.md

144-144: Trailing punctuation in heading
Punctuation: ':'

(MD026, no-trailing-punctuation)


150-150: Trailing punctuation in heading
Punctuation: ':'

(MD026, no-trailing-punctuation)


178-178: Trailing punctuation in heading
Punctuation: ':'

(MD026, no-trailing-punctuation)


185-185: Trailing punctuation in heading
Punctuation: ':'

(MD026, no-trailing-punctuation)


190-190: Trailing punctuation in heading
Punctuation: ':'

(MD026, no-trailing-punctuation)

🪛 Biome (1.9.4)
node_package/src/RSCPayloadContainer.ts

[error] 29-29: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: dummy-app-integration-tests (newest)
  • GitHub Check: dummy-app-integration-tests (oldest)
🔇 Additional comments (42)
node_package/src/serverRenderUtils.ts (2)

6-7: Added FinalHtmlResult type to imports

This new type import supports the parameter type update in createResultObject, allowing for more structured rendering results beyond simple strings.


10-10:

✅ Verification successful

Updated parameter type for enhanced flexibility

The change from string | null to FinalHtmlResult | null expands the function's capability to handle both simple HTML strings and the more structured ServerRenderHashRenderedHtml objects, which appears to support the PR's goal of improving server component rendering.

Since this is a type update, please ensure that all callsites have been updated to handle the possibility of receiving a ServerRenderHashRenderedHtml object instead of just a string.


🏁 Script executed:

#!/bin/bash
# Find all callsites of createResultObject to verify they can handle the new parameter type
rg "createResultObject\(" --type ts -A 3 -B 3

Length of output: 4031


Action Required: Confirmed Type Update Compatibility

The update changing the html parameter type in createResultObject from string | null to FinalHtmlResult | null has been verified. Our grep search shows that all call sites in:

  • node_package/src/ReactOnRailsRSC.ts
  • node_package/src/serverRenderReactComponent.ts
  • node_package/src/streamServerRenderedReactComponent.ts

are passing values (mostly strings) that are valid for the new union type. These call sites continue to work correctly (e.g., via JSON.stringify), so they can safely handle either a simple string or a structured ServerRenderHashRenderedHtml object.

Please double-check that none of these call sites perform string-specific operations on the html field that might assume a strict string format. Otherwise, the change is acceptable.

lib/react_on_rails/configuration.rb (6)

13-13: Good addition of the constant for server client manifest

This addition aligns well with the existing pattern used for client manifests, providing a clear default value.


25-25: LGTM: Configuration default properly set

The new server client manifest file is correctly added to the configuration hash with the default value.


71-71: LGTM: Attribute accessor added appropriately

The new manifest file attribute is properly added to the accessor list.


87-88: LGTM: Initialize method parameter added

The parameter is correctly added to the method signature with a default value of nil.


118-118: LGTM: Property initialization added

The new property is properly initialized in the method.


308-315: LGTM: Proper file list construction

The implementation correctly adds the new manifest file to the webpack_generated_files array only if it's present.

eslint.config.ts (1)

148-148: LGTM: Proper configuration for test files

Adding the test pattern to allowDefaultProject is appropriate to ensure proper ESLint support for the TypeScript test files.

node_package/tests/streamServerRenderedReactComponent.test.jsx (5)

63-64: Well-structured component type handling

Great improvement to the test setup, allowing testing of different component registration strategies through a clean switch statement.

Also applies to: 65-97


173-173: Improved test resilience

Replacing exact chunk count expectations with minimum expectations makes the tests more resilient to implementation changes that might add additional chunks.

Also applies to: 194-194, 250-250


206-224: Comprehensive testing for render functions

Good approach using it.each to test both synchronous and asynchronous render functions with the same expectations, reducing code duplication and improving test coverage.


262-307: Good error handling tests

These tests thoroughly verify how errors are handled in both synchronous and asynchronous render functions, including proper error propagation when throwJsErrors is enabled.


309-334: LGTM: Added support for Promise resolving to string

This test case ensures that components can return Promises that resolve to strings, which is an important use case for server rendering.

node_package/src/isServerRenderResult.ts (1)

1-10: LGTM! Good type definition updates.

The expanded type signatures for the functions improve type safety while maintaining backward compatibility.

node_package/tests/serverRenderReactComponent.test.ts (3)

12-24: Well-structured type assertion helpers.

Good implementation of custom type assertions that provide clear error messages. This approach enhances test readability and maintainability.


26-51: Comprehensive test setup for basic component rendering.

The test setup effectively clears the component registry before each test and validates both the rendering process and expected output.


125-158: Excellent documentation of edge case behavior.

The comments explaining the unintended side effects of returning strings from render functions provide valuable context for the tests and future developers.

node_package/src/streamServerRenderedReactComponent.ts (14)

1-1: No issues with the new import statement.
The change to import React as a namespace is a valid approach and aligns well with TypeScript convention.


7-7: Import of isServerRenderHash and isPromise looks good.
This import captures the new helpers needed for distinguishing server render hashes and promises.


11-11: Addition of StreamableComponentResult type.
This change provides stronger type coverage for streaming-related returns.


90-99: Console replay logic looks proper.
Building the console replay chunk dynamically and returning null when empty is a clean solution. Validating length before stringifying is efficient. No issues found.


108-117: Use of timeout to schedule console replay injection.
Clearing the previous timeout before scheduling the new one prevents potential concurrency issues. The zero-delay approach is acceptable for pushing ancillary data asynchronously.


119-124: Flush method usage is consistent.
Invoking clearTimeout and then pushing any remaining console replay chunk upon flush ensures that all console logs are included before finalizing the stream.


149-152: Refined function signature for streamRenderReactComponent.
Changing from ReactElement to StreamableComponentResult correctly accommodates both synchronous elements and async/promise-based elements.


163-170: Centralizing error tracking with reportError.
Marking hasErrors on the render state and optionally emitting the error into the stream is a clean, modular approach.


172-176: Dedicated sendErrorHtml for finalizing an error response.
This function enhances readability by separating error handling from other logic.


178-210: Handling promise-based rendering with React 18 streaming.
Properly checking for string outputs versus valid React elements helps maintain streaming consistency, while renderToPipeableStream usage aligns with React 18.


215-215: Stream renderer type annotation is appropriate.
Introducing this generic signature clarifies which types are supported by the streaming renderer.


235-237: Error thrown for server render hashes in streaming.
The updated message cleanly indicates that streaming is not compatible with server render hashes, reflecting the new code logic.


239-250: Promise check and valid React element enforcement.
Converting or throwing an error if the promise result is not a valid React element helps prevent improper usage. The messaging is clear.


255-268: Robust fallback for uncaught errors in streaming pipeline.
By setting hasErrors and sending error HTML, the code ensures that clients receive appropriate feedback without breaking the stream.

node_package/src/serverRenderReactComponent.ts (10)

1-1: Namespace import for React.
This style aligns well with TypeScript usage, and no issues are apparent.


18-19: Newly introduced types CreateReactOutputAsyncResult and RenderStateHtml.
Adding these clarifies async results and HTML return formats for server rendering.


30-30: Implicit declaration of htmlResult.
Refactoring to a single variable is straightforward and helps maintain readability.


46-46: Using nullish coalescing with htmlResult.
This ensures the returned result is never undefined, matching the expected contract.


48-57: processReactElement gracefully handles render-to-string failures.
Encapsulating renderToString in a try/catch block centralizes error handling and clarifies potential misconfigurations.


60-62: Updated processPromise signature.
Switching to CreateReactOutputAsyncResult aligns with the new async flow, making the code more type-safe.


71-76: Conditional React element check in promise resolution.
Appropriately rendering React elements or returning the final value ensures correct behavior for mixed promise results.


85-86: Refined server render result logic.
Distinguishing between server render hash, promises, and React elements in a single flow clarifies the code path for each outcome.


122-122: Split out final result creation.
createFinalResult consolidates promise handling and synchronous results into a single function, improving testability.


129-129: Delegation to createPromiseResult.
Spreading current render state and returning a structured promise ensures that console logs and rendered HTML are properly collated.

@AbanoubGhadban AbanoubGhadban force-pushed the abanoubghadban/ror1719/add-support-for-async-render-function-returns-component branch from 0306315 to e467df4 Compare April 6, 2025 07:22
@AbanoubGhadban AbanoubGhadban force-pushed the abanoubghadban/pro465/use-rsc-payload-to-render-server-components-on-server branch from 0faea62 to d92f586 Compare April 6, 2025 08:19
@AbanoubGhadban AbanoubGhadban force-pushed the abanoubghadban/ror1719/add-support-for-async-render-function-returns-component branch from fe75977 to 86f8bfb Compare April 7, 2025 10:37
Base automatically changed from abanoubghadban/ror1719/add-support-for-async-render-function-returns-component to master April 7, 2025 10:45
@AbanoubGhadban AbanoubGhadban force-pushed the abanoubghadban/pro465/use-rsc-payload-to-render-server-components-on-server branch from f53eb02 to 8698a9f Compare April 7, 2025 11:01
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (5)
node_package/src/RSCPayloadContainer.ts (3)

28-33: Security risk with dangerouslySetInnerHTML.

While you're escaping some script tags, using dangerouslySetInnerHTML still presents XSS risks. The __FLIGHT_DATA is being directly populated with potentially untrusted data.

🧰 Tools
🪛 Biome (1.9.4)

[error] 29-29: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)


17-19: 🛠️ Refactor suggestion

Enhance script escaping function for better security.

The current escaping function only handles a limited set of special HTML sequences. For better XSS protection, consider a more comprehensive implementation.

 function escapeScript(script: string) {
-  return script.replace(/<!--/g, '<\\!--').replace(/<\/(script)/gi, '</\\$1');
+  return script
+    .replace(/<!--/g, '<\\!--')
+    .replace(/<\/(script)/gi, '</\\$1')
+    .replace(/</g, '\\u003C')
+    .replace(/>/g, '\\u003E')
+    .replace(/\//g, '\\u002F')
+    .replace(/\u2028/g, '\\u2028')
+    .replace(/\u2029/g, '\\u2029');
 }

84-87: ⚠️ Potential issue

Fix array bounds check logic.

There's an off-by-one error in the bounds check. JavaScript arrays are zero-indexed, so valid indices are 0 to length-1.

- if (chunkIndex > chunkPromises.length) {
+ if (chunkIndex >= chunkPromises.length) {
    throw new Error('React on Rails Error: RSC Chunk index out of bounds');
  }
node_package/src/RSCServerRoot.ts (2)

38-67: Prefix path is still hardcoded.

This repeats an earlier suggestion regarding making the "/webpack/development/" path configurable. If a user is not in a development environment, they may need a custom prefix. Consider falling back to an environment variable or a provided option to allow greater flexibility.


100-105: Stream error handling is missing.

As previously suggested, consider attaching .on('error', ...) handlers to these PassThrough streams. This ensures errors are caught and handled rather than causing unhandled exceptions in production environments.

🧹 Nitpick comments (3)
node_package/src/RSCPayloadContainer.ts (1)

66-69: Add validation for stream chunks before processing.

The current implementation directly uses decoded stream chunks without validation, which could lead to issues if malformed data is received.

- resolveCurrentPromise({ chunk: decoder.decode(streamChunk as Uint8Array), isLastChunk: false });
+ const decodedChunk = decoder.decode(streamChunk as Uint8Array);
+ try {
+   // Simple validation to ensure it's valid - the exact validation depends on the expected format
+   if (decodedChunk.trim()) {
+     resolveCurrentPromise({ chunk: decodedChunk, isLastChunk: false });
+   } else {
+     console.warn('Empty chunk received, skipping');
+     return; // Don't create a new promise for empty chunks
+   }
+ } catch (e) {
+   rejectCurrentPromise(new Error(`Invalid chunk data received: ${e.message}`));
+ }
  createNewPromise();
node_package/src/types/index.ts (1)

37-37: Consider clarifying the usage of rscPayloadGenerationUrl.

The new optional property rscPayloadGenerationUrl may need additional documentation or usage examples to help maintainers understand its purpose and how it integrates with the RSC payload rendering workflow.

node_package/src/RSCServerRoot.ts (1)

1-16: Avoid relying on a global function definition.

Declaring function generateRSCPayload in the global scope can lead to hard-to-track dependencies. Consider importing or injecting a function reference instead, to promote clearer module boundaries and testability.

- declare global {
-   function generateRSCPayload(
-     componentName: string,
-     props: Record<string, unknown>,
-     railsContext: RailsContext,
-   ): Promise<NodeJS.ReadableStream>;
- }

+ import { generateRSCPayload } from './rscPayloadGenerator'; // Example import
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 20a71db and 8698a9f.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (23)
  • CHANGELOG.md (1 hunks)
  • eslint.config.ts (1 hunks)
  • knip.ts (1 hunks)
  • lib/react_on_rails/configuration.rb (6 hunks)
  • lib/react_on_rails/helper.rb (3 hunks)
  • lib/react_on_rails/test_helper/webpack_assets_status_checker.rb (1 hunks)
  • lib/react_on_rails/utils.rb (1 hunks)
  • node_package/src/RSCClientRoot.ts (3 hunks)
  • node_package/src/RSCPayloadContainer.ts (1 hunks)
  • node_package/src/RSCServerRoot.ts (1 hunks)
  • node_package/src/ReactOnRails.client.ts (1 hunks)
  • node_package/src/ReactOnRailsRSC.ts (4 hunks)
  • node_package/src/buildConsoleReplay.ts (1 hunks)
  • node_package/src/loadJsonFile.ts (1 hunks)
  • node_package/src/loadReactClientManifest.ts (0 hunks)
  • node_package/src/registerServerComponent/server.rsc.ts (1 hunks)
  • node_package/src/registerServerComponent/server.ts (2 hunks)
  • node_package/src/serverRenderReactComponent.ts (1 hunks)
  • node_package/src/streamServerRenderedReactComponent.ts (2 hunks)
  • node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts (1 hunks)
  • node_package/src/transformRSCStreamAndReplayConsoleLogs.ts (2 hunks)
  • node_package/src/types/index.ts (3 hunks)
  • package.json (2 hunks)
💤 Files with no reviewable changes (1)
  • node_package/src/loadReactClientManifest.ts
🚧 Files skipped from review as they are similar to previous changes (16)
  • eslint.config.ts
  • knip.ts
  • lib/react_on_rails/test_helper/webpack_assets_status_checker.rb
  • node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts
  • lib/react_on_rails/utils.rb
  • lib/react_on_rails/helper.rb
  • node_package/src/registerServerComponent/server.ts
  • lib/react_on_rails/configuration.rb
  • node_package/src/registerServerComponent/server.rsc.ts
  • node_package/src/buildConsoleReplay.ts
  • node_package/src/streamServerRenderedReactComponent.ts
  • node_package/src/loadJsonFile.ts
  • package.json
  • node_package/src/ReactOnRails.client.ts
  • node_package/src/transformRSCStreamAndReplayConsoleLogs.ts
  • node_package/src/ReactOnRailsRSC.ts
🧰 Additional context used
🧬 Code Definitions (2)
node_package/src/serverRenderReactComponent.ts (1)
node_package/src/serverRenderUtils.ts (1)
  • createResultObject (9-24)
node_package/src/RSCServerRoot.ts (4)
node_package/src/types/index.ts (1)
  • RailsContext (20-54)
node_package/tests/testUtils.js (1)
  • stream (14-21)
node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts (1)
  • transformRSCStream (3-31)
node_package/src/loadJsonFile.ts (1)
  • loadJsonFile (7-25)
🪛 Biome (1.9.4)
node_package/src/RSCPayloadContainer.ts

[error] 29-29: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: dummy-app-integration-tests (oldest)
  • GitHub Check: dummy-app-integration-tests (newest)
  • GitHub Check: build
  • GitHub Check: rspec-package-tests (oldest)
  • GitHub Check: rspec-package-tests (newest)
🔇 Additional comments (11)
node_package/src/serverRenderReactComponent.ts (1)

118-118: Bug fix for error handling in result object creation.

Fixed a bug where the result object creation was using the original render state instead of the error state when an error occurred during promise resolution. This ensures that error information is properly propagated.

CHANGELOG.md (1)

26-32: Well-documented architectural improvements in the CHANGELOG.

The new changelog entries clearly document the key improvements made to the RSC rendering flow, including eliminating double rendering and reducing HTTP requests. The mention of new components provides good context for users wanting to understand what changed.

node_package/src/RSCClientRoot.ts (4)

3-22: Added global type definition for Flight data.

Good addition of the global Window interface extension for the __FLIGHT_DATA array that will store React Server Components data. This provides proper TypeScript support for the global variable.


46-77: Stream creation from embedded RSC payload data.

This function creates a ReadableStream from the server-embedded Flight data by:

  1. Setting up a stream controller
  2. Processing existing chunks in __FLIGHT_DATA
  3. Overriding the array's push method to process new chunks immediately
  4. Properly closing the stream when DOM is fully loaded

This is an elegant approach to avoid unnecessary HTTP requests by reusing server-provided data.


79-83: Helper function to create RSC stream from page data.

Creates a complete pipeline to transform the raw RSC stream and prepare it for React's consumption. This encapsulates the multi-step process into a single reusable function.


110-115: Improved SSR hydration logic based on DOM content.

The component now intelligently chooses between:

  1. Hydrating from existing server-rendered content using embedded Flight data
  2. Fetching fresh data when the DOM node is empty

This optimization eliminates redundant network requests when server-rendered content is available.

node_package/src/types/index.ts (2)

20-20: Use of type alias is appropriate.

Converting RailsContext from an interface to a type alias is suitable here, especially since you’re leveraging a union type. This change cleanly accommodates conditional properties based on serverSide.


38-54: Good approach for conditionally requiring server-side properties.

Using a union to differentiate between serverSide: false and serverSide: true helps ensure type safety for RSC. This structure nicely enforces that serverSideRSCPayloadParameters and manifest file names are only present when serverSide is true.

node_package/src/RSCServerRoot.ts (3)

22-26: React version check is a reasonable safeguard.

This explicit check provides clear feedback if the user’s React version does not support server components. It’s a practical guard to avoid silent failures.


69-92: Validation checks look good.

You’re verifying the presence of required fields: serverSide, reactClientManifestFileName, and reactServerClientManifestFileName, plus confirming generateRSCPayload is defined. These checks will help fail fast if the setup is incomplete.


107-115: Creative use of React fragments and the use hook.

Wrapping the server component’s result and the payload container side by side in fragments cleanly separates the streamed RSC content from the payload stream. This approach is succinct and leverages React 18 features well.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 24 out of 24 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (3)

node_package/src/RSCServerRoot.ts:73

  • [nitpick] The error message thrown when required railsContext properties are missing could be clearer. Consider specifying exactly which properties are required (e.g., 'reactClientManifestFileName' and 'reactServerClientManifestFileName') to aid debugging.
  if (!railsContext?.serverSide || !railsContext?.reactClientManifestFileName || !railsContext?.reactServerClientManifestFileName) {

node_package/src/transformRSCNodeStreamAndReplayConsoleLogs.ts:13

  • The current check for line breaks assumes '\n' as the newline delimiter. Consider handling other newline formats (such as '\r\n') to ensure complete chunk detection across environments.
if (!decodedChunk.endsWith('\n')) {

node_package/src/RSCClientRoot.ts:111

  • [nitpick] The logic diverges on whether DOM content exists, choosing between createFromRSCStream and fetch-based hydration. Consider adding a comment to explain why the presence of innerHTML triggers a different hydration strategy to improve code clarity.
if (domNode.innerHTML) {

@AbanoubGhadban AbanoubGhadban changed the title [WIP] Use RSC payload to render server components on server Use RSC payload to render server components on server Apr 7, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
node_package/src/RSCServerRoot.tsx (1)

38-67: Minor spelling error: "aboluteFileUrl".

Consider correcting to "absoluteFileUrl" for clarity. For example:

- Object.entries(reactClientManifest).forEach(([aboluteFileUrl, clientFileBundlingInfo]) => {
+ Object.entries(reactClientManifest).forEach(([absoluteFileUrl, clientFileBundlingInfo]) => {
node_package/src/RSCPayloadContainer.tsx (1)

17-19: escapeScript method.
Implementation is minimal. Consider more robust sanitization if user content is allowed.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8698a9f and 1693393.

📒 Files selected for processing (4)
  • eslint.config.ts (4 hunks)
  • node_package/src/RSCClientRoot.ts (3 hunks)
  • node_package/src/RSCPayloadContainer.tsx (1 hunks)
  • node_package/src/RSCServerRoot.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • eslint.config.ts
🧰 Additional context used
🧬 Code Graph Analysis (1)
node_package/src/RSCClientRoot.ts (2)
node_package/src/types/index.ts (1)
  • RSCPayloadChunk (205-207)
node_package/src/transformRSCStreamAndReplayConsoleLogs.ts (1)
  • transformRSCStreamAndReplayConsoleLogs (3-60)
🪛 Biome (1.9.4)
node_package/src/RSCPayloadContainer.tsx

[error] 50-50: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: dummy-app-integration-tests (oldest)
  • GitHub Check: dummy-app-integration-tests (newest)
  • GitHub Check: build
  • GitHub Check: rspec-package-tests (oldest)
  • GitHub Check: rspec-package-tests (newest)
🔇 Additional comments (18)
node_package/src/RSCClientRoot.ts (5)

8-8: Imports look good.


16-20: No issues with global interface extension.


44-90: Consider verifying late loading scenario.
Current approach closes the stream on DOMContentLoaded, which might cause data loss if the RSC payload script loads afterward.

Please manually test or confirm that all data is guaranteed to be enqueued before DOMContentLoaded triggers.


92-96: Helper function logic is clear.


124-124: Efficient fallback logic.
This approach to detect existing content vs. fetching anew is neat.

node_package/src/RSCServerRoot.tsx (7)

1-8: Import statements are correct.


9-16: Global function declaration.
No issues found, provided that it's properly defined at runtime.


17-21: Type definitions.
Looks good for basic usage.


22-26: React.use version check.
Ensures correct React version and experimental features.


28-36: createFromReactOnRailsNodeStream.
Straightforward usage of transformRSCStream.


69-113: RSCServerRoot.
Implementation is well-structured.


115-115: Export statement.
No concerns.

node_package/src/RSCPayloadContainer.tsx (6)

1-2: Initial imports.
No issues found.


3-7: StreamChunk type.
Accurately defines the chunk structure.


8-10: RSCPayloadContainerProps.
Matches the expected NodeJS.ReadableStream usage.


12-15: RSCPayloadContainerInnerProps.
Property definitions are clear and typed.


21-39: Component documentation.
Clear explanation of this payload container’s purpose and flow.


71-121: Chunk-based approach.
Implementation logic for streaming chunks is sound and well-structured.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🔭 Outside diff range comments (1)
node_package/src/RSCServerRoot.tsx (1)

1-116: 🛠️ Refactor suggestion

Add error handling for stream processing failures

The code doesn't handle errors that might occur during stream processing, such as network errors or timeouts.

Consider adding error handling for the stream processing:

+ import { promisify } from 'util';

// In the RSCServerRoot component:
  const rscPayloadStream = await generateRSCPayload(componentName, componentProps, railsContext);

  // Tee the stream to pass it to the server component and the payload container
  const rscPayloadStream1 = new PassThrough();
+ rscPayloadStream1.on('error', (error) => {
+   console.error('Error in RSC payload stream 1:', error);
+ });
  rscPayloadStream.pipe(rscPayloadStream1);
  const rscPayloadStream2 = new PassThrough();
+ rscPayloadStream2.on('error', (error) => {
+   console.error('Error in RSC payload stream 2:', error);
+ });
  rscPayloadStream.pipe(rscPayloadStream2);
+ rscPayloadStream.on('error', (error) => {
+   console.error('Error in original RSC payload stream:', error);
+   // Propagate the error to the derived streams
+   rscPayloadStream1.emit('error', error);
+   rscPayloadStream2.emit('error', error);
+ });

  const serverComponentElement = createFromReactOnRailsNodeStream(rscPayloadStream1, ssrManifest);
+ 
+ // Add a timeout for stream processing
+ const streamTimeout = setTimeout(() => {
+   const timeoutError = new Error('RSC payload stream timed out');
+   rscPayloadStream.emit('error', timeoutError);
+ }, 30000); // 30 seconds timeout
+ 
+ // Clean up the timeout when the component unmounts
+ React.useEffect(() => {
+   return () => clearTimeout(streamTimeout);
+ }, []);
🧹 Nitpick comments (5)
node_package/src/transformRSCNodeStream.ts (1)

1-31: Add JSDoc comments to improve code documentation

This function serves an important purpose in the RSC rendering flow, but lacks documentation explaining what it does and how it should be used.

Consider adding JSDoc comments:

import { Transform } from 'stream';

+ /**
+  * Transforms an RSC stream containing JSON objects with 'html' property
+  * into a stream of HTML strings.
+  * 
+  * Each chunk in the input stream is expected to be a valid JSON object 
+  * containing an 'html' property. The function extracts and returns
+  * the HTML content from each chunk.
+  * 
+  * @param stream - A readable stream containing JSON objects with 'html' property
+  * @returns A transformed stream containing only the HTML content
+  */
export default function transformRSCStream(stream: NodeJS.ReadableStream): NodeJS.ReadableStream {
  // implementation
}
node_package/src/RSCServerRoot.tsx (4)

100-105: Improve stream handling by using built-in duplex features

The current approach of creating two PassThrough streams and manually piping them might be inefficient. Node.js has built-in methods for duplicating streams.

Consider using Node.js's stream.Readable.prototype.tee() if available (Node.js 16.7.0+) or a more efficient stream duplication:

- // Tee the stream to pass it to the server component and the payload container
- const rscPayloadStream1 = new PassThrough();
- rscPayloadStream.pipe(rscPayloadStream1);
- const rscPayloadStream2 = new PassThrough();
- rscPayloadStream.pipe(rscPayloadStream2);
- const serverComponentElement = createFromReactOnRailsNodeStream(rscPayloadStream1, ssrManifest);
+ // Use more efficient stream duplication if available
+ let rscPayloadStream1, rscPayloadStream2;
+ if (typeof rscPayloadStream.tee === 'function') {
+   // For Node.js 16.7.0+
+   [rscPayloadStream1, rscPayloadStream2] = rscPayloadStream.tee();
+ } else {
+   // Fallback for older Node.js versions
+   rscPayloadStream1 = new PassThrough();
+   rscPayloadStream2 = new PassThrough();
+   rscPayloadStream.pipe(rscPayloadStream1);
+   rscPayloadStream.pipe(rscPayloadStream2);
+ }
+ const serverComponentElement = createFromReactOnRailsNodeStream(rscPayloadStream1, ssrManifest);

Note: Verify Node.js version compatibility in your environment before implementing this change.


38-67: Extract the manifest creation into a separate utility file

The createSSRManifest function handles complex logic that might be reused elsewhere and makes the component file larger than necessary.

Consider moving this function to a separate utility file (e.g., createSSRManifest.ts):

// New file: createSSRManifest.ts
+ import loadJsonFile from './loadJsonFile';
+ 
+ export default async function createSSRManifest(
+   reactServerManifestFileName: string,
+   reactClientManifestFileName: string,
+   webpackPrefix: string = '/webpack/development/'
+ ) {
+   const [reactServerManifest, reactClientManifest] = await Promise.all([
+     loadJsonFile(reactServerManifestFileName),
+     loadJsonFile(reactClientManifestFileName),
+   ]);
+ 
+   const ssrManifest = {
+     moduleLoading: {
+       prefix: webpackPrefix,
+       crossOrigin: null,
+     },
+     moduleMap: {} as Record<string, unknown>,
+   };
+ 
+   Object.entries(reactClientManifest).forEach(([aboluteFileUrl, clientFileBundlingInfo]) => {
+     const serverFileBundlingInfo = reactServerManifest[aboluteFileUrl];
+     if (!serverFileBundlingInfo) return;
+     
+     ssrManifest.moduleMap[(clientFileBundlingInfo as { id: string }).id] = {
+       '*': {
+         id: (serverFileBundlingInfo as { id: string }).id,
+         chunks: (serverFileBundlingInfo as { chunks: string[] }).chunks,
+         name: '*',
+       },
+     };
+   });
+ 
+   return ssrManifest;
+ }

// In RSCServerRoot.tsx
+ import createSSRManifest from './createSSRManifest';

// Remove the createSSRManifest function from RSCServerRoot.tsx and update the call:
- const ssrManifest = await createSSRManifest(
+ const webpackPrefix = railsContext.webpackDevServerBaseUrl || 
+                        `/webpack/${process.env.NODE_ENV || 'development'}/`;
+ const ssrManifest = await createSSRManifest(
    railsContext.reactServerClientManifestFileName,
    railsContext.reactClientManifestFileName,
+   webpackPrefix
  );

73-84: Improve error message clarity with specific checks

The current error message combines multiple checks into one conditional, making it hard to determine which specific property is missing.

Consider separate checks with specific error messages:

- if (
-   !railsContext?.serverSide ||
-   !railsContext?.reactClientManifestFileName ||
-   !railsContext?.reactServerClientManifestFileName
- ) {
-   throw new Error(
-     'serverClientManifestFileName and reactServerClientManifestFileName are required. ' +
-       'Please ensure that React Server Component webpack configurations are properly set ' +
-       'as stated in the React Server Component tutorial. ' +
-       'Make sure to use "stream_react_component" instead of "react_component" to SSR a server component.',
-   );
- }
+ if (!railsContext?.serverSide) {
+   throw new Error(
+     'Server components can only be rendered server-side. ' +
+     'Make sure to use "stream_react_component" instead of "react_component" to SSR a server component.'
+   );
+ }
+ 
+ if (!railsContext?.reactClientManifestFileName) {
+   throw new Error(
+     'reactClientManifestFileName is required. ' +
+     'Please ensure that React Server Component webpack configurations are properly set ' +
+     'as stated in the React Server Component tutorial.'
+   );
+ }
+ 
+ if (!railsContext?.reactServerClientManifestFileName) {
+   throw new Error(
+     'reactServerClientManifestFileName is required. ' +
+     'Please ensure that React Server Component webpack configurations are properly set ' +
+     'as stated in the React Server Component tutorial.'
+   );
+ }

85-92: Add version check for generateRSCPayload

You're checking for the existence of the function but not its compatibility with your expected signature.

Consider adding a simple version check if the function supports a version property:

if (typeof generateRSCPayload !== 'function') {
  throw new Error(
    'generateRSCPayload is not defined. Please ensure that you are using at least version 4.0.0 of ' +
      'React on Rails Pro and the Node renderer, and that ReactOnRailsPro.configuration.enable_rsc_support ' +
      'is set to true.',
  );
}

+ // Optional version check if the function has a version property
+ if (
+   'version' in generateRSCPayload && 
+   typeof generateRSCPayload.version === 'string' && 
+   generateRSCPayload.version < '4.0.0'
+ ) {
+   throw new Error(
+     `Incompatible generateRSCPayload version: ${generateRSCPayload.version}. ` +
+     'Please ensure that you are using at least version 4.0.0 of React on Rails Pro.'
+   );
+ }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1693393 and 098b790.

📒 Files selected for processing (2)
  • node_package/src/RSCServerRoot.tsx (1 hunks)
  • node_package/src/transformRSCNodeStream.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: dummy-app-integration-tests (newest)
  • GitHub Check: dummy-app-integration-tests (oldest)
  • GitHub Check: rspec-package-tests (newest)
  • GitHub Check: rspec-package-tests (oldest)
  • GitHub Check: build

…consistency. Adjust related methods and references to use the new naming convention for the server client manifest file.
…r improved type safety in RSC streaming and handling.
…improve error messaging for missing manifest files
…ty and consistency in RSC streaming handling.
… RSCPayloadChunk type, and improve documentation for chunk handling and transfer resilience.
…ify chunk index validation in RSCServerRoot for improved error handling.
@AbanoubGhadban AbanoubGhadban force-pushed the abanoubghadban/pro465/use-rsc-payload-to-render-server-components-on-server branch from 6047019 to c919b1d Compare April 26, 2025 13:41
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (4)
node_package/src/RSCServerRoot.tsx (2)

45-45: Fix typo in variable name.

There's a typo in the variable name 'aboluteFileUrl' - it should be 'absoluteFileUrl'.

-  Object.entries(reactClientManifest).forEach(([aboluteFileUrl, clientFileBundlingInfo]) => {
+  Object.entries(reactClientManifest).forEach(([absoluteFileUrl, clientFileBundlingInfo]) => {

This typo was previously pointed out in prior reviews.


46-46: 🛠️ Refactor suggestion

Add error handling for missing server manifest entries.

The code directly destructures properties from reactServerManifest[aboluteFileUrl] without checking if the entry exists, which could cause runtime errors if the entry is missing.

-    const { id, chunks } = reactServerManifest[aboluteFileUrl];
+    const serverFileBundlingInfo = reactServerManifest[absoluteFileUrl];
+    if (!serverFileBundlingInfo) {
+      console.warn(`Missing server manifest entry for ${absoluteFileUrl}`);
+      return;
+    }
+    const { id, chunks } = serverFileBundlingInfo;
node_package/src/RSCPayloadContainer.tsx (2)

58-60: Address potential XSS vulnerability with dangerouslySetInnerHTML.

While the escapeScript function provides some protection, using dangerouslySetInnerHTML with content from a stream could still pose XSS risks if the stream contains malicious content.

Ensure that the RSC payload is strictly trusted and cannot contain user-provided content without proper sanitization. Consider adding additional validation if the payload might contain user input.

🧰 Tools
🪛 Biome (1.9.4)

[error] 58-58: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)


78-110: 🛠️ Refactor suggestion

Stream handling should be more resilient.

The current stream handling doesn't properly handle all edge cases, such as when a stream ends immediately after an error or when events occur after the stream has ended.

export default function RSCPayloadContainerWrapper({ RSCPayloadStream }: RSCPayloadContainerProps) {
  const [chunkPromises] = React.useState<Promise<StreamChunk>[]>(() => {
    const promises: Promise<StreamChunk>[] = [];
    let resolveCurrentPromise: (streamChunk: StreamChunk) => void = () => {};
    let rejectCurrentPromise: (error: unknown) => void = () => {};
    const decoder = new TextDecoder();
+    let isStreamEnded = false;

    const createNewPromise = () => {
+      if (isStreamEnded) return;
      const promise = new Promise<StreamChunk>((resolve, reject) => {
        resolveCurrentPromise = resolve;
        rejectCurrentPromise = reject;
      });

      promises.push(promise);
    };

    createNewPromise();
    RSCPayloadStream.on('data', (streamChunk) => {
+      if (isStreamEnded) return;
      resolveCurrentPromise({ chunk: decoder.decode(streamChunk as Uint8Array), isLastChunk: false });
      createNewPromise();
    });

    RSCPayloadStream.on('error', (error) => {
+      if (isStreamEnded) return;
      rejectCurrentPromise(error);
+      // Mark stream as ended to prevent further processing
+      isStreamEnded = true;
      createNewPromise();
    });

    RSCPayloadStream.on('end', () => {
+      if (isStreamEnded) return;
      resolveCurrentPromise({ chunk: '', isLastChunk: true });
+      isStreamEnded = true;
    });

    return promises;
  });
🧹 Nitpick comments (1)
node_package/src/RSCServerRoot.tsx (1)

35-66: Consider enhancing type safety in manifest handling.

The function casts manifest data to specific types but then accesses properties directly, which could be improved with stronger type checking.

Consider adopting a more type-safe approach by extracting the manifest mapping logic to a separate function with proper TypeScript interfaces:

interface ManifestEntry {
  id: string;
  chunks?: string[];
}

function createModuleMap(
  serverManifest: Record<string, ManifestEntry>,
  clientManifest: Record<string, ManifestEntry>
): Record<string, unknown> {
  const moduleMap: Record<string, unknown> = {};
  
  Object.entries(clientManifest).forEach(([absoluteFileUrl, clientEntry]) => {
    const serverEntry = serverManifest[absoluteFileUrl];
    if (!serverEntry || !serverEntry.chunks) {
      console.warn(`Missing or invalid server manifest entry for ${absoluteFileUrl}`);
      return;
    }
    
    moduleMap[clientEntry.id] = {
      '*': {
        id: serverEntry.id,
        chunks: serverEntry.chunks,
        name: '*',
      },
    };
  });
  
  return moduleMap;
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6047019 and c919b1d.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (24)
  • CHANGELOG.md (1 hunks)
  • eslint.config.ts (4 hunks)
  • knip.ts (1 hunks)
  • lib/react_on_rails/configuration.rb (6 hunks)
  • lib/react_on_rails/helper.rb (3 hunks)
  • lib/react_on_rails/test_helper/webpack_assets_status_checker.rb (1 hunks)
  • lib/react_on_rails/utils.rb (1 hunks)
  • node_package/src/RSCClientRoot.ts (3 hunks)
  • node_package/src/RSCPayloadContainer.tsx (1 hunks)
  • node_package/src/RSCServerRoot.tsx (1 hunks)
  • node_package/src/ReactOnRails.client.ts (1 hunks)
  • node_package/src/ReactOnRailsRSC.ts (4 hunks)
  • node_package/src/buildConsoleReplay.ts (1 hunks)
  • node_package/src/loadJsonFile.ts (1 hunks)
  • node_package/src/loadReactClientManifest.ts (0 hunks)
  • node_package/src/reactApis.cts (2 hunks)
  • node_package/src/registerServerComponent/server.rsc.ts (1 hunks)
  • node_package/src/registerServerComponent/server.ts (2 hunks)
  • node_package/src/serverRenderReactComponent.ts (1 hunks)
  • node_package/src/streamServerRenderedReactComponent.ts (1 hunks)
  • node_package/src/transformRSCNodeStream.ts (1 hunks)
  • node_package/src/transformRSCStreamAndReplayConsoleLogs.ts (2 hunks)
  • node_package/src/types/index.ts (3 hunks)
  • package.json (2 hunks)
💤 Files with no reviewable changes (1)
  • node_package/src/loadReactClientManifest.ts
✅ Files skipped from review due to trivial changes (1)
  • package.json
🚧 Files skipped from review as they are similar to previous changes (18)
  • node_package/src/serverRenderReactComponent.ts
  • knip.ts
  • node_package/src/buildConsoleReplay.ts
  • node_package/src/ReactOnRails.client.ts
  • lib/react_on_rails/utils.rb
  • node_package/src/loadJsonFile.ts
  • eslint.config.ts
  • lib/react_on_rails/test_helper/webpack_assets_status_checker.rb
  • lib/react_on_rails/helper.rb
  • node_package/src/registerServerComponent/server.rsc.ts
  • node_package/src/streamServerRenderedReactComponent.ts
  • CHANGELOG.md
  • lib/react_on_rails/configuration.rb
  • node_package/src/transformRSCNodeStream.ts
  • node_package/src/transformRSCStreamAndReplayConsoleLogs.ts
  • node_package/src/types/index.ts
  • node_package/src/ReactOnRailsRSC.ts
  • node_package/src/RSCClientRoot.ts
🧰 Additional context used
🪛 GitHub Check: build
node_package/src/registerServerComponent/server.ts

[failure] 2-2:
Unable to resolve path to module '../RSCServerRoot.ts'

🪛 GitHub Actions: Lint JS and Ruby
node_package/src/registerServerComponent/server.ts

[error] 2-2: ESLint: Unable to resolve path to module '../RSCServerRoot.ts' (import/no-unresolved)

🪛 Biome (1.9.4)
node_package/src/RSCPayloadContainer.tsx

[error] 58-58: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: dummy-app-integration-tests (oldest)
  • GitHub Check: dummy-app-integration-tests (newest)
  • GitHub Check: rspec-package-tests (newest)
  • GitHub Check: rspec-package-tests (oldest)
🔇 Additional comments (8)
node_package/src/reactApis.cts (1)

55-61: LGTM! The new React.use compatibility check is well-implemented.

The ensureReactUseAvailable function is a good safeguard that verifies the presence of React's use API before attempting to render server components, with a clear error message explaining that React 19 is required.

node_package/src/registerServerComponent/server.ts (1)

28-33: Good implementation of server component wrapping.

The approach of wrapping each component with RSCServerRoot is clean and effectively separates concerns, allowing server-side rendering with pre-generated RSC payloads.

node_package/src/RSCServerRoot.tsx (4)

85-91: Good validation for prerequisite functionality.

The check for generateRSCPayload availability with a clear error message is an excellent practice that will help developers quickly identify and resolve configuration issues.


72-83: Good validation of required context properties.

The validation of required Rails context properties with a helpful error message will assist developers in correctly configuring their environment.


99-103: Tee stream implementation is correct.

The approach of duplicating the stream with PassThrough is appropriate for the use case, allowing the RSC payload to be used both for server component rendering and for client payload delivery.


106-114: Clean implementation of server component resolution.

Extracting the resolved component before rendering and using a cleaner JSX structure is a good improvement from previous versions.

node_package/src/RSCPayloadContainer.tsx (2)

17-27: Good script escaping implementation with clear documentation.

The escapeScript function and its detailed documentation clearly explain the approach to preventing HTML parsing issues when injecting scripts.


112-121: Good error handling for out-of-bounds chunk indices.

The getChunkPromise callback correctly validates chunk indices and throws a descriptive error if the index is out of bounds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants