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

feat(nextjs-mf): add support for Next.js App Router #2002

Conversation

prasannamestha
Copy link

@prasannamestha prasannamestha commented Jan 21, 2024

Description

This PR adds support for module federation in Next.js App Router. To use this simply pass next/navigation as a shared dependency.

Related Issue

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation.

@prasannamestha prasannamestha force-pushed the pm/support-app-router branch 3 times, most recently from 99b2cff to fd98cb2 Compare January 21, 2024 21:33
@@ -151,7 +151,10 @@ class FederationRuntimePlugin {
// TODO: maybe set this variable as constant is better https://github.com/webpack/webpack/blob/main/lib/config/defaults.js#L176
entryItem.import = ['./src'];
}
if (!entryItem.import.includes(entryFilePath)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not modify /enhanced in order to fix next.js

If you need to do this, we should add a hook or some option that we can pass down from next.js instead.

/enhanced needs to remain as pure as it can and not patch framework specific issues but rather bugs in webpack etc (like single runtime chunk) X

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, maybe we can add "exclude" option to allow users control whether add our federation runtime entry file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ScriptedAlchemy @ahabhgk How about adding exclude options to allow users control whether add our federation runtime entry file in rspack/webpack ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah im good with that

filename: '',
remotes: {},
exposes: {},
shared: ['next/navigation'], // This is important!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this to internal.ts instead, where Next.js retrieves its default shares. Users do not need to add extra shared module to make it work

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I decided to use this was to ensure that defaultShared is not pased in app router. That creates issues with react-dom and react runtime.

It might be a better idea to add a new flag enableAppRouter in federation config - what do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay got another idea - I'll try to figure out whether the project uses app router and do this implicitly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when if both pages and app router are used?

Will it break?

If so we can enable "appRouterOnly" mode so its either one or the other

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it works with both app and pages router.. I have a change, will push the commit shortly.

Only thing left right now is to figure out a way to run this without modifying @module-federation/enhanced

@ScriptedAlchemy
Copy link
Member

Okay, take a look at this branch. #2017

Might give you some ideas, but its still broken in the client, server works tho.

@ManuelcsNuant
Copy link

hello all,

any chance this gets merged soon? I'm getting error with "@module-federation/nextjs-mf@^8.1.7" Error: Failed to collect page data for /[locale]/backtest/[id] on NextJs 14 which I assume its because App router is not supported

@Abhishrut-Peepultree
Copy link

Hey all, I'm getting error "Uncaught TypeError: Cannot read properties of null (reading 'useContext')" while using useRouter() in the remote application. Does anyone have any clue how might this be resolved?

@ManuelcsNuant
Copy link

Hello everyone, sorry to bother again, will app router get supported any time soon? I'm using it with current page router but I want to integrate it with another nextjs that uses app router

@yavuzsecer88
Copy link

Hi guys, is there any progress? Thanks in advance!

@saeedghasemi72
Copy link

any news? and please add a example for next 14 (app router)

@ScriptedAlchemy
Copy link
Member

No progress, i wouldnt expect it to be resolved anytime soon.

@Jony-Y
Copy link

Jony-Y commented Mar 14, 2024

@ScriptedAlchemy So, is the current statement to not use next14 with nextjs-mf? is it only for App router?
I want to know if we need to downgrade nextjs, or use native webpack?

Can you please confirm and provide the suggested solution?

@ryok90
Copy link
Contributor

ryok90 commented Mar 14, 2024

@ScriptedAlchemy So, is the current statement to not use next14 with nextjs-mf? is it only for App router? I want to know if we need to downgrade nextjs, or use native webpack?

Can you please confirm and provide the suggested solution?

@Jony-Y
Only App router is not currently supported (independent of version).
Next.js v14 is working with Pages router.

@phyolim
Copy link

phyolim commented Mar 16, 2024

V14 doesn't work if I use any react client features ( like useState ) in remote component even in page router. I haven't tried with clean projects though.

@ScriptedAlchemy
Copy link
Member

You can't have any app router at all. No folder called app. Whole thing must be only pages router.

App router will most likely never be supported.

@Luferov
Copy link

Luferov commented Mar 20, 2024

You can't have any app router at all. No folder called app. Whole thing must be only pages router.

App router will most likely never be supported.

Could you say, is this a statement that there will never be micro frontends in app router?

@sethjeffery
Copy link

sethjeffery commented Mar 20, 2024

That would be a presumptive statement, I am sure vercel/nextjs would want to support micro-frontend architecture in some form, but based on experience they will likely provide their own solution at some point and push to make it a standard.

@ScriptedAlchemy
Copy link
Member

There will never be module federation with app router, not my ecosystem - not for next.js

Vercel will make proprietary system for app router most likely.

@burakakca
Copy link

burakakca commented Aug 19, 2024

There is an application and it's using the app router and pages structure (95%), I want to use mf in that project but "App Directory is not supported by nextjs-mf. Use only pages directory, do not open git issues about this" error accrued. How can I fix that. Should I need to copy app folder codes to pages structure ? or there is a work around? @ScriptedAlchemy

@VladSam42
Copy link

There will never be module federation with app router, not my ecosystem - not for next.js

Vercel will make proprietary system for app router most likely.

@ScriptedAlchemy , what exactly is the issue? Is this because they switched to SWC? They are pushing the app router very hard and probably will deprecate the page router sooner or later

@kacemws
Copy link

kacemws commented Sep 10, 2024

There will never be module federation with app router, not my ecosystem - not for next.js
Vercel will make proprietary system for app router most likely.

@ScriptedAlchemy , what exactly is the issue? Is this because they switched to SWC? They are pushing the app router very hard and probably will deprecate the page router sooner or later

you can try to use Multizone, other than that i think that we are cooked :/

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

Successfully merging this pull request may close these issues.