React主机和NextJS远程APP示例/兼容的模块联合会
我没有找到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
- 我有repo 在这里
我从中获得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
- Host
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
- I have repo here
The error I'm getting its 404 from
http://localhost:3000/_next/static/chunks/components_ExposedComponent_js.js
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从主机的webpack配置中,我可以看到下一个JS应用程序正在端口3002上运行。
当主机试图从下一个JS服务器中获取捆绑包时,下一个JS远程捆绑会获得404错误,它正在击中端口3000而不是3002。这是因为
next.config.js
没有更新的publicPath
。在
//您的原始next.config.js export
中添加以下位置。 /代码>您的下一个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 updatedpublicPath
.Add the following in the place where it says
//your original next.config.js export
in thenext.config.js
fileThis will set the
publicPath
of your next JS app toport 3002
and fetch the bundles correctly.指,,
在您的远程应用程序的WebPack配置上,SET PublicPath将解决路径问题。
Referring to https://webpack.js.org/guides/public-path/ ,
on your remote app's webpack config, set publicPath would fix the path issue.