web-infra-dev / rsbuild

The Rspack-based build tool. It's fast, out-of-the-box and extensible.
https://rsbuild.dev/
MIT License
1.53k stars 120 forks source link

[Bug]: MF breaks the asset path when more than one sub path #2482

Closed XavierLeTohic closed 1 week ago

XavierLeTohic commented 4 months ago

Version

System:
    OS: macOS 14.4.1
    CPU: (8) arm64 Apple M1
    Memory: 41.70 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Browsers:
    Chrome: 125.0.6422.141
    Safari: 17.4.1
  npmPackages:
    @rsbuild/core: ^0.7.1 => 0.7.1 
    @rsbuild/plugin-react: ^0.7.1 => 0.7.1

Details

When setting moduleFederation.options and reloading with more than one sub path (.i.e /sub/path) Rsbuild inject assets without the prefix / even setting output.assetPrefix: "/" resulting in a white page.

Commenting or removing the whole moduleFederation from rsbuild config and everything works well.

See the attached Codesandbox and repository to reproduce.

Reproduce link

https://codesandbox.io/p/github/XavierLeTohic/rsbuild-mf-asset-path/main

Reproduce Steps

The repo of the codesandbox is here:

https://github.com/XavierLeTohic/rsbuild-mf-asset-path

To reproduce:

  1. Clone
  2. npm install or yarn install
  3. npm run dev or yarn dev
  4. On http://localhost:5050/example everything will work as expected if you reload
  5. Go to http://localhost:5050/example/sub/path and reload it will be broken
  6. Comment the whole module federation in rsbuild.config.ts and try to reload it'll work
chenjiahan commented 4 months ago

output.assetPrefix is for production build, can you try the dev.assetPrefix config?

Note that when you use moduleFederation.options, Rsbuild will set the default value of Rspack's output.publicPath configuration to 'auto', this is recommended by MF team.

XavierLeTohic commented 4 months ago

dev.assetPrefix helped me discover that the reason why it's not working it's that it does not find assets using rsbuild dev and with module federation no matter the prefix I use, I updated the Codesandbox to reproduce, any idea?

Note that output.publicPath does not exists according to the types in 0.7.1 even tho it's mentioned in the documentation.

Screenshot 2024-06-03 at 10 14 07 Screenshot 2024-06-03 at 10 26 34

chenjiahan commented 4 months ago

output.publicPath is a config of Rspack, you can use Rsbuild's tools.rspack to configure it.

ScriptedAlchemy commented 3 months ago

Is public path set to auto?

XavierLeTohic commented 3 months ago

Was fixed in dev by setting config.output.publicPath = '/' since config.output.publicPath = 'auto' still break the injection of assets when more than one sub path is used and the page is reloaded.

Updated the codesandbox to demo when it fails with auto -> https://codesandbox.io/p/github/XavierLeTohic/rsbuild-mf-asset-path/main

imzisy commented 3 months ago

I tried to change publicPath to / but it is not working for the host app.

XavierLeTohic commented 3 months ago

I tried to change publicPath to / but it is not working for the host app.

Did you setup MF via appendPlugins like below?

tools: {
        rspack: (config, { appendPlugins }) => {
            config.output.publicPath = "/";

            appendPlugins([
                new ModuleFederationPlugin({ // ...my options })
            ]);
        },
    },
imzisy commented 3 months ago

I tried to change publicPath to / but it is not working for the host app.

Did you setup MF via appendPlugins like below?

tools: {
      rspack: (config, { appendPlugins }) => {
          config.output.publicPath = "/";

          appendPlugins([
              new ModuleFederationPlugin({ // ...my options })
          ]);
      },
  },

I'm using the v1 moduleFederation.options .

XavierLeTohic commented 3 months ago

No certain but I think you can find the plugin of the 1.5 from @rspack/core's container

nate-summercook commented 2 months ago

maybe related to #2747 ?

imzisy commented 2 months ago

To resolve this issue, upgrade to the latest version of Module Federation.

XavierLeTohic commented 2 months ago

@imzisy this issue also happen with the built-in module federation of rsbuild, not only using the external plugin @module-federation/enhanced/rspack

XavierLeTohic commented 2 months ago

After upgrading to ^1.0.0-alpha.6 it fixed the issue using tools.rspack but the issue remain with the built-in moduleFederation.options

qmakani commented 1 week ago

this doesn't seem like an issue anymore. should be safe to close it?

XavierLeTohic commented 1 week ago

Fixed on the latest versions