Description
中文
问题描述
现有应用框架如下
- 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-mfe
plugin
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-mfe
plugin
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.