Skip to content

[react-server-dom-webpack] Flight client manifest lists empty chunks when a non-JavaScript asset appears first in a chunk #38453

Description

@Tortleya

Summary

When a Webpack chunk's files set starts with a non-JavaScript asset (such as extracted CSS from mini-css-extract-plugin) or a .hot-update.* file, ReactFlightWebpackPlugin aborts processing that entire chunk and leaves the component's Flight client manifest entry with chunks: [], even though Webpack still emits the chunk's JavaScript file (e.g., client0.js).

Affected version

Impact

The trigger is any non-JavaScript asset (or .hot-update.* file) appearing before the runtime .js/.mjs file in chunk.files — mini-css-extract-plugin is just a common way to produce that ordering, not a bug in mini-css-extract-plugin. Because the Flight client manifest entry ends up with chunks: [], a Flight client relying on the manifest has no chunk ID or JS filename to preload/load for that client reference.

Reproduction

In an empty directory:

npm install [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]
cat <<'JS' > build.cjs
const fs = require('node:fs');
const path = require('node:path');
const webpack = require('webpack');
const MiniCss = require('mini-css-extract-plugin');
const Flight = require('react-server-dom-webpack/plugin');

fs.writeFileSync('entry.js', "import 'react-server-dom-webpack/client.browser';");
fs.writeFileSync('Client.js', "'use client'; import './client.css'; export default () => null;");
fs.writeFileSync('client.css', 'body { color: navy; }');

const compiler = webpack({
  mode: 'development',
  entry: './entry.js',
  output: {path: path.resolve('dist'), publicPath: '/'},
  module: {rules: [{test: /\.css$/, use: [MiniCss.loader, 'css-loader']}]},
  plugins: [
    new MiniCss(),
    new Flight({isServer: false, clientReferences: path.resolve('Client.js')}),
  ],
});

compiler.run((err, stats) => {
  if (err || stats.hasErrors()) throw err || new Error(stats.toString({all: false, errors: true}));
  for (const c of stats.compilation.chunks) {
    if (c.name !== 'main') console.log([...c.files]);
  }
  console.log(JSON.parse(fs.readFileSync('dist/react-client-manifest.json', 'utf8')));
  compiler.close(() => {});
});
JS
node build.cjs

Actual output

[ 'client0.css', 'client0.js' ]
{
  'file:///.../Client.js': { id: './Client.js', chunks: [], name: '*' }
}

The same empty chunks: [] result reproduces with mode: 'production' (with a numeric module id).

Expected behavior

The manifest entry should still record the chunk's JavaScript file regardless of asset order in c.files:

{
  'file:///.../Client.js': { id: './Client.js', chunks: [ 'client0', 'client0.js' ], name: '*' }
}

(Chunks containing only non-JavaScript assets with no .js/.mjs file should of course still produce chunks: [], since the manifest records JS chunks only.)

Related

  • Related: #33155 / PR #33028 updated this same loop to accept .mjs files. This issue is the asset-ordering bug in the same loop: any non-JS or .hot-update.* file appearing first in c.files exits the chunk callback early.

Root cause and suggested fix

In ReactFlightWebpackPlugin.js#L277-L291, both guards inside the for (const file of c.files) loop use return; (#L281 and #L287), returning out of the entire chunkGroup.chunks.forEach callback instead of skipping to the next file:

chunkGroup.chunks.forEach(function (c) {
  for (const file of c.files) {
    if (!(file.endsWith('.js') || file.endsWith('.mjs'))) {
      continue; // was: return;
    }
    if (
      file.endsWith('.hot-update.js') ||
      file.endsWith('.hot-update.mjs')
    )
      continue; // was: return;
    chunks.push(c.id, file);
    break;
  }
});

Changing both return; statements to continue; while keeping break; after chunks.push(c.id, file) (#L289) fixes both dev and production builds (verified locally against [email protected], yielding chunks: [ 'client0', 'client0.js' ]).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions