Historically, Module Federation was limited to Client-Side Rendering (CSR), primarily benefiting Single Page Applications (SPAs). However, the evolving web development landscape has increasingly favored Server-Side Rendering (SSR) for its myriad benefits.
With the advent of version 15.4, Nx has introduced Module Federation with SSR support, marking a significant milestone. This fusion of Module Federation and SSR in Nx Workspaces leverages the strengths of both technologies.
For a comprehensive understanding of Nx and its integration with Module Federation, we highly recommend consulting the official Nx documentation. It offers in-depth insights and practical guidance to effectively leverage these technologies. You can access this valuable resource here:
This guide is focused on guiding you through the setup of Module Federation with Server-Side Rendering specifically for Angular applications.
In a traditional SSR application, the server is responsible for rendering the application based on the route requested by the browser. When combining Module Federation with SSR, this concept is expanded. If a route points to a remote module, the host server delegates the route processing to the remote server, which then renders the HTML and sends it back to the browser.
This integration not only harnesses the full potential of SSR but also maintains the flexibility of dividing the build into smaller segments. It facilitates independent deployment of features within the application, allowing updates to the remote server without necessitating a full redeployment of the host server.
We'll start by creating a host application named 'dashboard' and a remote application called 'login'.
Begin by executing the following command to create a new Nx workspace:
During this process, you'll encounter prompts regarding the workspace type and preset.
Respond to the prompts as follows:
Opting for Nx Cloud (Optional)
While setting up your workspace, you'll also be asked about adding Nx Cloud. This guide doesn't cover Nx Cloud, but its integration with Module Federation is advantageous for shared caching across teams and CI, enhancing build times. Learn more about Nx Cloud at Nx Cloud Website.
After creating the workspace, navigate to it:
Use a single command to scaffold your Module Federation architecture with SSR:
This command generates two Angular Universal (SSR) applications and configures Webpack for both browser and server with Module Federation.
This builds the browser and server bundles for the 'login' application and runs it using Node.js. Note that the 'login' application runs without file watchers, so changes in its code won't automatically reflect.
http://localhost:4200
.If you are actively working on the 'login' application and need to see the results of your changes in real time, you can enable server rebuilds upon code modification. This can be achieved by using the devRemotes
flag:
Using this command, the server will rebuild the 'login' application whenever you make changes, allowing for an iterative and efficient development process.