Fix chunk-merge bug in ReactFlightWebpackPlugin (v19.2.1)#21
Fix chunk-merge bug in ReactFlightWebpackPlugin (v19.2.1)#21AbanoubGhadban wants to merge 1 commit intoupgrade-to-react-v19.2.1from
Conversation
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.2.1 with the fix applied. Resolves #19 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Greptile SummaryFixes a critical bug where React Server Components would fail to hydrate on slow networks with Key changes:
Trade-offs:
Confidence Score: 4/5
Important Files Changed
Last reviewed commit: 14ff4dd |
| void 0 !== module) | ||
| ) | ||
| if (filePathToModuleMetadata[module]) { | ||
| id = filePathToModuleMetadata[module]; |
There was a problem hiding this comment.
Variable shadowing: reusing id parameter name to store metadata object reduces readability
| id = filePathToModuleMetadata[module]; | |
| const existing = filePathToModuleMetadata[module]; |
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!
| ) | ||
| if (filePathToModuleMetadata[module]) { | ||
| id = filePathToModuleMetadata[module]; | ||
| module = new Set(); |
There was a problem hiding this comment.
Variable shadowing: reusing module parameter name to store Set object reduces readability
| module = new Set(); | |
| 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!
| for (var i = 0; i < id.chunks.length; i += 2) | ||
| module.add(id.chunks[i]); |
There was a problem hiding this comment.
Use descriptive variable name in the merge loop
| for (var i = 0; i < id.chunks.length; i += 2) | |
| module.add(id.chunks[i]); | |
| for (var existingIdx = 0; existingIdx < existing.chunks.length; existingIdx += 2) | |
| existingChunkIds.add(existing.chunks[existingIdx]); |
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!
| for (i = 0; i < chunks.length; i += 2) | ||
| module.has(chunks[i]) || | ||
| id.chunks.push(chunks[i], chunks[i + 1]); |
There was a problem hiding this comment.
Use descriptive variable name and clearer condition formatting
| for (i = 0; i < chunks.length; i += 2) | |
| module.has(chunks[i]) || | |
| id.chunks.push(chunks[i], chunks[i + 1]); | |
| for (var newIdx = 0; newIdx < chunks.length; newIdx += 2) { | |
| if (!existingChunkIds.has(chunks[newIdx])) { | |
| existing.chunks.push(chunks[newIdx], chunks[newIdx + 1]); | |
| } | |
| } |
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!
Summary
recordModule()inReactFlightWebpackPluginto merge chunk arrays instead of overwriting when the same module appears in multiple webpack chunk groupsTypeError: r[e] is not a functionerrors during RSC hydration on slow networks (e.g., 3G + CPU throttling)rsc-patches/v19.2.1with the fix appliedRoot 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:
To merge logic that preserves chunks from all chunk groups:
Test plan
TypeErrorerrors after 3+ hard reloadsResolves #19
🤖 Generated with Claude Code