modern.js: Error with unmounted `NextRouter` when consuming Nextjs remote application #6498
中文
问题描述
现有应用框架如下
- Next.js 主应用
- Next.js 子应用
主应用和子应用之间使用 Module Federation 提供的插件进行连接。 由于 Module Federation 宣布不再为 Next.js 16 及之后的版本提供插件支持,我正在尝试将主应用向 Modern.js 迁移。
期待达成的应用框架如下
- Modern.js 主应用
- Next.js 主应用
目前遇到的问题是,Next.js 子应用接入后,一旦涉及到子应用内部使用 useRouter,便会产生报错,NextRouter was not mounted。
问题复现
Demo:
可看出,当从主应用访问路由/simple-page时,子应用正常加载;访问/complex-page 时,由于子应用中使用了 useRouter,会产生报错 NextRouter was not mounted。
错误源码
const router = _react.default.useContext(_routercontextsharedruntime.RouterContext);
if (!router) {
throw new Error("NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted");
}
期待行为
可以从主应用正常访问加载子应用的页面。
English
Description
Micro Frontend Applications structure
- Next.js host
- Next.js remote
- Module Federation
next-mfeplugin
As Module Federation announces it will stop supporting Next.js since version 16, I am trying to migration the host application to Modern.js, and keep the remote application being Next.js.
Micro Frontend Applications structure after migration
- Next.js host
- Next.js remote
- Module Federation
next-mfeplugin
Problem
When Next.js remote application is being consumed from the Modern.js host Application, if the loaded remote component contains the usage of NextRouter, an error of NextRouter was not mounted will be thrown.
Reproduction
Demo:
It can be seen that when accessing /simple-page from the host application, remote application is loaded as expected. When accessing complex-page, because of the usage of useRouter in remote application, an error NextRouter was not mounted is thrown.
Error source
const router = _react.default.useContext(_routercontextsharedruntime.RouterContext);
if (!router) {
throw new Error("NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted");
}
Expectation
To load remote application with router usage from the host application.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 19 (13 by maintainers)
I will not retire the plugin without solutions for users 😊 Will start experimenting with options after the 20th, then we may investigate some more robust options next quarter. First I need to see what possible, then I can adapt the next plugin to service migration mode or something. we may also be able to do something on modernjs end to smooth out where possible. But first let me start testing strategies.
@MaykonMorais Thank you, I will be following it! And I have added English version for this issue.
Nexts router is an external that’s set to the window by _app as window.NEXT_ROUTER
So the best thing would be to share modernjs router and link. Then use those in next.js instead. Using modernjs route system not next.
Next router only work with next host.
@MaykonMorais you can give a new issue for us
@MaykonMorais Can you provide a minimal reproducible demo? It may not be the same problem.
@jianingroja 因为 nextjs remote + modernjs host 丢失了 nextjs 框架运行时上下文,这个能力是由
@module-federation/nextjs-mf提供或者说共享的,我研究了一下,目前 modernjs 还不能直接使用这个插件来提供 nextjs 的运行时能力。@ScriptedAlchemy Hi zack , any ideas for this case (modernjs host + nextjs remote) ?I think this demand will increase, do we have any solutions or plans? 🤔