React主机和NextJS远程APP示例/兼容的模块联合会

发布于 2025-01-23 14:04:31 字数 1931 浏览 4 评论 0原文

我没有找到react主机的示例,nextjs远程应用程序 我的主机正在使用React远程应用程序,现在我正在尝试添加nextjs远程应用程序,它无法正常工作:

  • 端口:

    • 主机3000
    • React React Remote 3001
    • NextJS远程3002
  • 在我的主机React应用中,我有以下内容,在WebPack中.config.js文件

plugins: [
    new ModuleFederationPlugin({
      name: 'react_container',
      filename: 'remoteEntry.js',
      remotes: {
        remote_react: 'remote_react@http://localhost:3001/remoteEntry.js', // <- This is working
        remote_nextjs: 'remote_nextjs@http://localhost:3002/_next/static/chunks/remoteEntry.js', // <- Not working :-(
      },
      exposes: {
        './react': 'react',
        './react-dom': 'react-dom',
      },
      shared: {
      },
    }),

以及远程nextjs app中的next.config.js 文件

const { withFederatedSidecar } = require("@module-federation/nextjs-mf");

module.exports = withFederatedSidecar({
  name: "remote_nextjs",
  filename: "static/chunks/remoteEntry.js",
  exposes: {
    "./ExposedComponent": "./components/ExposedComponent",
  },
  shared: {
  },
})({
  // your original next.config.js export
});
  • ,最后,在主机的app.jsx中,我正在尝试消耗远程组件,例如
import RemoteNav from 'remote_react/Nav'; // <- Working 
import ExposedComponent from 'remote_nextjs/ExposedComponent'; // <- Not working

我从中获得404的错误 http:// localhost:3000/_next/static/components/components_exposedcomponent_js.js

谢谢

I didn't find an example for a react host with and nextjs remote app
My host is working with a react remote app, now that I'm trying to add a nextjs remote app, it's not working:

  • Ports:

    • Host 3000
    • React remote 3001
    • NextJS Remote 3002
  • In my host react app I have the following, in the webpack.config.js file

plugins: [
    new ModuleFederationPlugin({
      name: 'react_container',
      filename: 'remoteEntry.js',
      remotes: {
        remote_react: 'remote_react@http://localhost:3001/remoteEntry.js', // <- This is working
        remote_nextjs: 'remote_nextjs@http://localhost:3002/_next/static/chunks/remoteEntry.js', // <- Not working :-(
      },
      exposes: {
        './react': 'react',
        './react-dom': 'react-dom',
      },
      shared: {
      },
    }),

And in the remote nextjs app in the next.config.js file

const { withFederatedSidecar } = require("@module-federation/nextjs-mf");

module.exports = withFederatedSidecar({
  name: "remote_nextjs",
  filename: "static/chunks/remoteEntry.js",
  exposes: {
    "./ExposedComponent": "./components/ExposedComponent",
  },
  shared: {
  },
})({
  // your original next.config.js export
});
  • And finally, in the App.jsx of the host , I'm trying to consume the remote components like
import RemoteNav from 'remote_react/Nav'; // <- Working 
import ExposedComponent from 'remote_nextjs/ExposedComponent'; // <- Not working

The error I'm getting its 404 from
http://localhost:3000/_next/static/chunks/components_ExposedComponent_js.js

enter image description here

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海螺姑娘 2025-01-30 14:04:31

从主机的webpack配置中,我可以看到下一个JS应用程序正在端口3002上运行。

当主机试图从下一个JS服务器中获取捆绑包时,下一个JS远程捆绑会获得404错误,它正在击中端口3000而不是3002。这是因为next.config.js没有更新的publicPath

//您的原始next.config.js export中添加以下位置

webpack: (config) => {
  config.output.publicPath = "http://localhost:3002/_next/";
  return config;
}

。 /代码>您的下一个JS应用程序到端口3002并正确获取捆绑包。

From the webpack config of the host, I can see that the next JS app is running on port 3002.

You are getting 404 error for next JS remote bundles when the host is trying to fetch the bundles from next JS server, it is hitting port 3000 instead of 3002. This is because the next.config.js does not have the updated publicPath.

Add the following in the place where it says //your original next.config.js export in the next.config.js file

webpack: (config) => {
  config.output.publicPath = "http://localhost:3002/_next/";
  return config;
}

This will set the publicPath of your next JS app to port 3002 and fetch the bundles correctly.

森林迷了鹿 2025-01-30 14:04:31

,,
在您的远程应用程序的WebPack配置上,SET PublicPath将解决路径问题。

  export default {
    output: {
      publicPath: 'auto',
    },
    plugins: [...],
    ...
  }

Referring to https://webpack.js.org/guides/public-path/ ,
on your remote app's webpack config, set publicPath would fix the path issue.

  export default {
    output: {
      publicPath: 'auto',
    },
    plugins: [...],
    ...
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文