Skip to content

Commit

Permalink
docs(angular): setup module federation with ssr (nrwl#13815)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 authored Dec 14, 2022
1 parent 8df1834 commit 2883f17
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 1 deletion.
19 changes: 18 additions & 1 deletion docs/map.json
Original file line number Diff line number Diff line change
Expand Up @@ -925,12 +925,29 @@
{
"name": "Faster Builds with Module Federation",
"id": "faster-builds",
"tags": ["use-task-executors"],
"tags": [
"use-task-executors",
"module-federation",
"angular",
"react"
],
"file": "shared/guides/module-federation/faster-builds"
},
{
"name": "Setup Module Federation with SSR for Angular and React",
"id": "module-federation-with-ssr",
"tags": [
"use-task-executors",
"module-federation",
"angular",
"react"
],
"file": "shared/recipes/module-federation-with-ssr"
},
{
"name": "Advanced Micro Frontends with Angular using Dynamic Federation",
"id": "dynamic-module-federation-with-angular",
"tags": ["use-task-executors", "module-federation", "angular"],
"file": "shared/guides/module-federation/dynamic-mfe-angular"
},
{
Expand Down
135 changes: 135 additions & 0 deletions docs/shared/recipes/module-federation-with-ssr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Setup Module Federation with SSR for Angular and React

This guide will walk you through creating a Module Federated setup with Server Side Rendering (SSR) for Angular and React using Nx and its generators.

## Steps

### Create an empty workspace

Run the following command with the options listed to create an empty workspace.

```{% command="npx create-nx-workspace@latest" path="~" %}
✔ Choose your style · integrated
✔ What to create in the new workspace · apps
✔ Repository name · myorg
✔ Enable distributed caching to make your CI faster · No
```

{% card title="Opting into Nx Cloud" description="You will also be prompted whether to add Nx Cloud to your workspace. We won't address this in this recipe, but you can see the introduction to Nx Cloud for more details." url="/nx-cloud/intro/what-is-nx-cloud" /%}

### Install your framework plugin

{% tabs %}

{% tab label="Angular" %}

```{% command="npm install --save-dev @nrwl/angular" path="~/myorg" %}
```

{% /tab %}

{% tab label="React" %}

```{% command="npm install --save-dev @nrwl/react" path="~/myorg" %}
```

{% /tab %}

{% /tabs %}

### Generating a host and multiple remotes with SSR

We will generate the apps required for a storefront application.
We will need the following applications:

- Store - _host application_
- Product - _remote application_
- Checkout - _remote application_

Nx allows you to do this with a single command:

{% tabs %}

{% tab label="Angular" %}

```{% command="npx nx g @nrwl/angular:host store --ssr --remotes=product,checkout" path="~/myorg" %}
```

{% /tab %}

{% tab label="React" %}

```{% command="npx nx g @nrwl/react:host store --ssr --remotes=product,checkout" path="~/myorg" %}
```

{% /tab %}

{% /tabs %}

This will generate three applications, set up with SSR and Module Federation.

### Serving the store application

When using Module Federation, we want to serve the host application along with the remote applications so that everything works as expected.

To do this, run:

{% tabs %}

{% tab label="Angular" %}

```{% command="npx nx serve-ssr store" path="~/myorg" %}
```

{% /tab %}

{% tab label="React" %}

```{% command="npx nx serve store" path="~/myorg" %}
```

{% /tab %}

{% /tabs %}

This will run all three application servers but only the `store` will be watching for file changes. If you make a change to one of the remote applications (`checkout` or `product`) the changes will not be hot reloaded.

### Serving the store application with file watching for checkout

If working on a remote application, we can still serve it via the host application and have it watch for changes.

To serve the `store` application and watch for changes on the `checkout` application run:

{% tabs %}

{% tab label="Angular" %}

```{% command="npx nx serve-ssr store --devRemotes=checkout" path="~/myorg" %}
```

{% /tab %}

{% tab label="React" %}

```{% command="npx nx serve store --devRemotes=checkout" path="~/myorg" %}
```

{% /tab %}

{% /tabs %}

### Additional Resources

To learn more about Module Federation, we have some resources you might find useful:

- [Guide: Faster Builds with Module Federation](/recipes/module-federation/faster-builds)
- [Video: Speed up your Angular serve and build times with Module Federation and Nx](https://www.youtube.com/watch?v=JkcaGzhRjkc)

0 comments on commit 2883f17

Please sign in to comment.