Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with unmounted NextRouter when consuming Nextjs remote application #6498 #6509

Open
jianingroja opened this issue Nov 6, 2024 · 19 comments
Assignees

Comments

@jianingroja
Copy link

jianingroja commented Nov 6, 2024

中文

问题描述

现有应用框架如下

  • 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
Image

错误源码

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.
Image

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.

@KyrieLii
Copy link
Member

KyrieLii commented Nov 8, 2024

@jianingroja Modern.js 主应用 需要更新下代码,现在只初始化了项目

@jianingroja
Copy link
Author

@KyrieLii
多谢指出。已更新

@MaykonMorais
Copy link

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

@KyrieLii
Copy link
Member

@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? 🤔

@ScriptedAlchemy
Copy link
Contributor

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.

@KyrieLii
Copy link
Member

KyrieLii commented Nov 11, 2024

@ScriptedAlchemy ye, the problem is indeed that an error occurs when using the nextjs router api (useRouter)..

@KyrieLii
Copy link
Member

@MaykonMorais Can you provide a minimal reproducible demo? It may not be the same problem.

@zllkjc
Copy link
Member

zllkjc commented Nov 11, 2024

@MaykonMorais you can give a new issue for us

@2heal1
Copy link
Member

2heal1 commented Nov 11, 2024

@ScriptedAlchemy I'm not sure ,will the problem be solved by setting nextjs-router as shared ?

@ScriptedAlchemy
Copy link
Contributor

问题描述

现有应用框架如下

  • 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
Image

期待行为

可以从主应用正常访问加载子应用的页面。

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/"

@2heal1
Copy link
Member

2heal1 commented Nov 11, 2024

@ScriptedAlchemy I'm not sure ,will the problem be solved by setting nextjs-router as shared ?

Not work ... ignore this

@KyrieLii
Copy link
Member

@2heal1 I have tried shared something, nextjs is a complex system ..

@ScriptedAlchemy
Copy link
Contributor

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.

@jianingroja
Copy link
Author

jianingroja commented Nov 11, 2024

@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.

@ScriptedAlchemy
Copy link
Contributor

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.

@jianingroja
Copy link
Author

@ScriptedAlchemy Thank you for the comments. We will wait for the migration solution.

@MaykonMorais
Copy link

@jianingroja I opened an issue about this

@jianingroja jianingroja changed the title 接入 Nextjs 应用作为子应用时 NextRouter 报错 #6498 Error with unmounted NextRouter when consuming Nextjs remote application #6498 Nov 13, 2024
@jianingroja
Copy link
Author

@MaykonMorais Thank you, I will be following it! And I have added English version for this issue.

@ScriptedAlchemy
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants