Next.js Integração Visão geral
Suporte for Next.js é ending read more
Este plugin enables Module Federation on Next.js.
Suporta
- next ^15 || ^14 || ^13 || ^12
- Servidor-Side Rendering
- Pages router
I highly recommend referencing este aplicação que takes advantage do best capabilities: https://github.com/module-federation/module-federation-examples
Requirement
I definir process.env.NEXT_PRIVATE_LOCAL_WEBPACK = 'true' inside este plugin, but its best se its definir em env ou linha de comando exporte.
"Local Webpack" means você deve have webpack installed as uma dependência, e next vai não use its bundled copy de webpack que cannot ser usado as i need access para all de webpack internals
cross-env NEXT_PRIVATE_LOCAL_WEBPACK=true next devounext buildnpm install webpack
Uso
To avoid hydration erros, use React.lazy instead de next/dynamic for carregamento lazy federated componentes.
See o implementation here: https://github.com/module-federation/module-federation-examples/tree/master/nextjs-v13/home/pages
Com async boundary installed at o page level. Você pode then do seguintes
Demo
Você pode see it em action here: https://github.com/module-federation/module-federation-examples/tree/master/nextjs-ssr
Opções
Este plugin works exactly like ModuleFederationPlugin, use it as você'd normally. Note esse nós já compartilhe react e next stuff for você automatically.
Also NextFederationPlugin has own opcional argument extraOptions onde você pode unlock additional features de este plugin:
debug– enables debug mode. It vai print additional information about o que é going on under o hood.exposePages– exposes automatically all nextjs pages for você e theirs./pages-map.enableImageLoaderFix– adds public hostname para all assets bundled bynextjs-image-loader. So se você serve remoteEntry dehttp://example.comthen all bundled assets vai get este hostname em runtime. It's something like Base URL em HTML but for módulos federados.enableUrlLoaderFix– adds public hostname para all assets bundled byurl-loader.skipSharingNextInternals– disables compartilhamento de next internals. Você pode use it se você quiser compartilhe next internals yourself ou want para usar este plugin on non next aplicações
Demo
Você pode see it em action here: https://github.com/module-federation/module-federation-examples/pull/2147
Implementing o Plugin
- Use
NextFederationPluginem yournext.config.jsdo app esse você wish para exponha módulos de. We'll call este "next2".
- Use react.lazy, low level api, ou require/importe de para importe remotes.
RuntimePlugins
To forneça extensibility e "middleware" for federation, você pode refer para @module-federation/enhanced/runtime
Utilities
loadRemote has been removed - você pode take advantage do novo runtime apis: https://module-federation.io/guide/runtime/runtime-api.html#loadremote
revalidate
Enables hot reloading de node servidor (não cliente) em produção. Este é recomendado, sem it - servers vai não ser able para pull remote updates sem uma full restart.
More info here: https://github.com/module-federation/nextjs-mf/tree/main/packages/node#utilities
For Express.js
Hot reloading Express.js obrigatório additional steps: https://github.com/module-federation/core/blob/main/packages/node/README.md
Whats shared por padrão?
Under o hood nós compartilhe some next internals automatically Você não precisa compartilhe these pacotes, compartilhamento next internals yourself vai cause erros.