Next.js
Support for Next.js is ending read more
This plugin enables Module Federation on Next.js
Supports
- next ^15 || ^14 || ^13 || ^12
- Server-Side Rendering
- Pages router
I highly recommend referencing this application which takes advantage of the best capabilities: https://github.com/module-federation/module-federation-examples
Requirement
I set process.env.NEXT_PRIVATE_LOCAL_WEBPACK = 'true' inside this plugin, but its best if its set in env or command line export.
"Local Webpack" means you must have webpack installed as a dependency, and next will not use its bundled copy of webpack which cannot be used as i need access to all of webpack internals
cross-env NEXT_PRIVATE_LOCAL_WEBPACK=true next devornext buildnpm install webpack
Usage
To avoid hydration errors, use React.lazy instead of next/dynamic for lazy loading federated components.
See the implementation here: https://github.com/module-federation/module-federation-examples/tree/master/nextjs-v13/home/pages
With async boundary installed at the page level. You can then do the following
Demo
You can see it in action here: https://github.com/module-federation/module-federation-examples/tree/master/nextjs-ssr
Options
This plugin works exactly like ModuleFederationPlugin, use it as you'd normally. Note that we already share react and next stuff for you automatically.
Also NextFederationPlugin has own optional argument extraOptions where you can unlock additional features of this plugin:
debug– enables debug mode. It will print additional information about what is going on under the hood.exposePages– exposes automatically all nextjs pages for you and theirs./pages-map.enableImageLoaderFix– adds public hostname to all assets bundled bynextjs-image-loader. So if you serve remoteEntry fromhttp://example.comthen all bundled assets will get this hostname in runtime. It's something like Base URL in HTML but for federated modules.enableUrlLoaderFix– adds public hostname to all assets bundled byurl-loader.skipSharingNextInternals– disables sharing of next internals. You can use it if you want to share next internals yourself or want to use this plugin on non next applications
Demo
You can see it in action here: https://github.com/module-federation/module-federation-examples/pull/2147
Implementing the Plugin
- Use
NextFederationPluginin yournext.config.jsof the app that you wish to expose modules from. We'll call this "next2".
- Use react.lazy, low level api, or require/import from to import remotes.
RuntimePlugins
To provide extensibility and "middleware" for federation, you can refer to @module-federation/enhanced/runtime
Utilities
loadRemote has been removed - you can take advantage of the new runtime apis: https://module-federation.io/guide/runtime/runtime-api.html#loadremote
revalidate
Enables hot reloading of node server (not client) in production. This is recommended, without it - servers will not be able to pull remote updates without a full restart.
More info here: https://github.com/module-federation/nextjs-mf/tree/main/packages/node#utilities
For Express.js
Hot reloading Express.js required additional steps: https://github.com/module-federation/core/blob/main/packages/node/README.md
Whats shared by default?
Under the hood we share some next internals automatically You do not need to share these packages, sharing next internals yourself will cause errors.