Open jianingroja opened 1 week ago
@jianingroja Modern.js 主应用 需要更新下代码,现在只初始化了项目
@KyrieLii 多谢指出。已更新
Hi guys! I trying to do something similar but working with ssr: true, is it possible? It's throwing this error for me:
error: "Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization
@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? 🤔
What's the issue you are encountering?
I see next router mentioned here, note it's important not to use any of the router imports from next and next.js router is coupled tightly to next itself.
@ScriptedAlchemy ye, the problem is indeed that an error occurs when using the nextjs router api (useRouter)..
@MaykonMorais Can you provide a minimal reproducible demo? It may not be the same problem.
@MaykonMorais you can give a new issue for us
@ScriptedAlchemy I'm not sure ,will the problem be solved by setting nextjs-router as shared ?
问题描述
现有应用框架如下
- 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
。期待行为
可以从主应用正常访问加载子应用的页面。
Don't use next/link or next/router - use modernjs equivalents in next. Only a next host can provide those. They are coupled to the window and next router appears to also patch react as well.
Avoid any import that starts with "next/"
@ScriptedAlchemy I'm not sure ,will the problem be solved by setting nextjs-router as shared ?
Not work ... ignore this
@2heal1 I have tried shared something, nextjs is a complex system ..
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.
@KyrieLii @ScriptedAlchemy Thank you for the replies.
We are currently using Next.js page router, as it it supported with module federation next plugin.
As Zack you are suggesting to use modernjs router system, which is implemented on top of react route, I am thinking, does it mean to completely refactor/abandon next page router, and replace it with react router? As each route API of react router needs to be used in the context of a <Router> component
. In the aspect of file structure, it means to have something like app router structure, but with react router under the hood?
In the end we are having an application that behaves and structured based on react router
, but wrapper inside of next.js framework. I am finding it a bit tricky, would like to hear some insights about this.
Another problem is that our current nextjs application use heavily SSR, and react router
depends heavily on document
, which does not exist during SSR. Our teams are separated responsibility and maintaining independently different remote applications, that's why we are not migrating directly all our apps to Modern.js
, and seeking this solution as a way of transition. What is your opinion about this situation? Should we migrate completely from Nextjs?
Thank you.
Next.js can function as a remote, but if used as a dev server, it would need a ModernJS host to vend it locally. In this setup, Next.js essentially serves as a build step. Alternatively, you could create a shared router module capable of switching between Next.js and React Router imports, allowing it to work locally with Next.js but default to ModernJS components (e.g., ModernJS’s Link) when hosted by ModernJS. A custom runtime plugin could help manage these transitions. ByteDance's broader teams plan to support migration solutions in the coming quarter; until then, I’ll work on proof-of-concept projects this month to identify and address any issues.
A runtime plugin could be used to map next/router
to React Router equivalents when ModernJS is the host, with similar handling for Link
. However, a translation layer would be necessary to bridge API differences.
ModernJS supports federated SSR, so document-based limitations shouldn’t be a concern—Remix can SSR effectively as well. A local development shell using ModernJS would prevent Next.js from being a host in any situation.
While both can coexist, having any Vercel-owned product in your stack can introduce federation challenges. One approach is to make page components agnostic by passing Link and router props to them. This way, pages can provide the necessary elements, making the components adaptable and enabling dependency injection via props as needed.
@ScriptedAlchemy Thank you for the comments. We will wait for the migration solution.
@jianingroja I opened an issue about this
@MaykonMorais Thank you, I will be following it! And I have added English version for this issue.
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.
中文
问题描述
现有应用框架如下
主应用和子应用之间使用 Module Federation 提供的插件进行连接。 由于 Module Federation 宣布不再为 Next.js 16 及之后的版本提供插件支持,我正在尝试将主应用向 Modern.js 迁移。
期待达成的应用框架如下
目前遇到的问题是,Next.js 子应用接入后,一旦涉及到子应用内部使用 useRouter,便会产生报错,
NextRouter was not mounted
。问题复现
Demo:
可看出,当从主应用访问路由
/simple-page
时,子应用正常加载;访问/complex-page
时,由于子应用中使用了useRouter
,会产生报错NextRouter was not mounted
。错误源码
期待行为
可以从主应用正常访问加载子应用的页面。
English
Description
Micro Frontend Applications structure
next-mfe
pluginAs 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-mfe
pluginProblem
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 ofNextRouter 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 accessingcomplex-page
, because of the usage ofuseRouter
in remote application, an errorNextRouter was not mounted
is thrown.Error source
Expectation
To load remote application with router usage from the host application.