Skip to content

Fix chunk-merge bug in ReactFlightWebpackPlugin (v19.0.3)#20

Open
AbanoubGhadban wants to merge 3 commits intomainfrom
fix/chunk-merge-bug-v19.0.3
Open

Fix chunk-merge bug in ReactFlightWebpackPlugin (v19.0.3)#20
AbanoubGhadban wants to merge 3 commits intomainfrom
fix/chunk-merge-bug-v19.0.3

Conversation

@AbanoubGhadban
Copy link
Collaborator

Summary

  • Fix recordModule() in ReactFlightWebpackPlugin to merge chunk arrays instead of overwriting when the same module appears in multiple webpack chunk groups
  • This fixes TypeError: r[e] is not a function errors during RSC hydration on slow networks (e.g., 3G + CPU throttling)
  • Built from React fork rsc-patches/v19.0.3 with the fix applied

Root Cause

When webpack splits modules across multiple chunks (via chunk groups), the plugin's recordModule() was called multiple times for the same module. Each call overwrote the previous entry with only the current chunk group's chunks, losing earlier chunk references. The RSC flight client then failed to preload all necessary chunks, causing __webpack_require__ to fail when a module factory hadn't been registered yet.

The Fix

Changed from:

filePathToModuleMetadata[href] = { id, chunks, name: '*' };

To merge logic that preserves chunks from all chunk groups:

if (filePathToModuleMetadata[href]) {
  // Merge chunks from additional chunk groups
  const existing = filePathToModuleMetadata[href];
  const existingChunkIds = new Set();
  for (let i = 0; i < existing.chunks.length; i += 2) {
    existingChunkIds.add(existing.chunks[i]);
  }
  for (let j = 0; j < chunks.length; j += 2) {
    if (!existingChunkIds.has(chunks[j])) {
      existing.chunks.push(chunks[j], chunks[j + 1]);
    }
  }
} else {
  filePathToModuleMetadata[href] = { id, chunks: chunks.slice(), name: '*' };
}

Test plan

  • Built from fixed React fork branch
  • Verify with localhub-demo-search under Slow 3G + 6x CPU throttling
  • Confirm zero TypeError errors after 3+ hard reloads

Resolves #19

🤖 Generated with Claude Code

AbanoubGhadban and others added 3 commits December 16, 2025 11:02
When a 'use client' module appears in multiple webpack chunk groups,
recordModule() was overwriting the manifest entry instead of merging
chunk arrays. This caused missing chunk preloads during RSC hydration,
leading to __webpack_require__ failures under slow networks.

Built from React fork rsc-patches/v19.0.3 with the fix applied.

Resolves #19

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link

coderabbitai bot commented Feb 24, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/chunk-merge-bug-v19.0.3

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps
Copy link

greptile-apps bot commented Feb 24, 2026

Greptile Summary

Fixes a critical bug where recordModule() was overwriting chunk arrays instead of merging them when the same module appears in multiple webpack chunk groups. This resolves TypeError: r[e] is not a function errors during RSC hydration on slow networks.

  • Implements Set-based deduplication to merge chunks from multiple chunk groups
  • Adds defensive chunks.slice() to avoid reference issues when creating new entries
  • Uses if-else branching to handle both existing and new module metadata entries

The fix addresses the root cause by preserving all chunk references needed for proper RSC client-side hydration.

Confidence Score: 4/5

  • Safe to merge with minor style improvements suggested
  • The logic correctly implements chunk merging to fix a critical hydration bug. Variable shadowing exists but doesn't affect correctness. The Set-based deduplication is sound, and the defensive chunks.slice() prevents reference issues.
  • No files require special attention - the single file change is straightforward and addresses the stated bug

Important Files Changed

Filename Overview
src/react-server-dom-webpack/cjs/react-server-dom-webpack-plugin.js Fixed chunk array overwriting bug by implementing merge logic with Set-based deduplication; variable shadowing exists but doesn't affect correctness

Last reviewed commit: ef99ec0

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

1 file reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +249 to +250
id = filePathToModuleMetadata[module];
module = new Set();
Copy link

Choose a reason for hiding this comment

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

Variable shadowing: id and module parameters are being reused as local variables. While this works, it reduces readability.

Suggested change
id = filePathToModuleMetadata[module];
module = new Set();
const existing = filePathToModuleMetadata[module];
const existingChunkIds = new Set();

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment on lines +251 to +252
for (var i = 0; i < id.chunks.length; i += 2)
module.add(id.chunks[i]);
Copy link

Choose a reason for hiding this comment

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

Use descriptive variable name in loop instead of reusing i

Suggested change
for (var i = 0; i < id.chunks.length; i += 2)
module.add(id.chunks[i]);
for (let j = 0; j < id.chunks.length; j += 2)
module.add(id.chunks[j]);

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@AbanoubGhadban AbanoubGhadban changed the base branch from upgrade-to-react-v19.0.3 to main February 28, 2026 13:54
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.

1 participant