这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/site/content/docs/guides/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ In our community-supported guides, you'll find examples of how to use `turbo` wi
href="/docs/guides/tools"
/>

<Card
title="Microfrontends"
description="Develop microfrontends with Turborepo"
href="/docs/guides/microfrontends"
/>

<Card
title="Single-package workspaces"
description="Integrate with a standalone application"
Expand Down
1 change: 1 addition & 0 deletions docs/site/content/docs/guides/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"tools",
"linting",
"testing",
"microfrontends",
"single-package-workspaces",
"generating-code",
"skipping-tasks",
Expand Down
229 changes: 229 additions & 0 deletions docs/site/content/docs/guides/microfrontends.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
---
title: Microfrontends
description: Learn how to develop microfrontends with Turborepo.
---

import { PackageManagerTabs, Tab } from '#components/tabs';
import { Callout } from '#components/callout';
import { Files, File, Folder } from '#components/files';
import { ExperimentalBadge } from '#components/experimental-badge';

<ExperimentalBadge>Experimental</ExperimentalBadge>

Turborepo simplifies local development for vertical microfrontends by coordinating multiple applications and providing a unified development experience.

Vertical microfrontends are an architectural pattern where a web application is composed of multiple frontend applications. Each microfrontend can be developed, tested, and deployed separately, while appearing as a single cohesive application to your users.

## How Turborepo helps

Turborepo enhances microfrontend development with:

- **Unified development experience**: Use a single host (like `localhost:3000`) to develop many applications
- **Automatic port assignment**: Each application gets a consistent, predictable port
- **Task orchestration**: Dependencies between microfrontends are handled automatically
- **Simplified code sharing**: Easily share UI components, utilities, and configurations across microfrontends
- **Optimized builds**: Cache and parallelize builds across all your applications

## Quick start

1. `npx create-turbo@latest --example with-microfrontends`
2. Run `turbo dev` in your terminal
3. Visit `localhost:3000` in your browser. The logs for`web#dev` task show that the `web` application's `/` route compiled.
4. Visit `localhost:3000/docs` in your browser. The logs for the `docs#dev` task show that the `docs` application's `/` route compiled.

## Setting up microfrontends in your Turborepo

<Callout>
The tutorial below is using `npx create-turbo@latest` if you want to follow
along.
</Callout>

### 1. Create your applications

Structure your repository with multiple applications:

<Files>
<Folder name="apps" defaultOpen>
<Folder name="web" />
<Folder name="docs" />
</Folder>
</Files>

### 2. Add the configuration

Create a `microfrontends.json` or `microfrontends.jsonc` in your parent application:

```json title="./apps/web/microfrontends.json"
{
"$schema": "https://openapi.vercel.sh/microfrontends.json",
"version": "1",
"applications": {
"web": {
"development": {
"local": 3000,
"fallback": "demo.com"
}
},
"docs": {
"routing": [
{
"paths": ["/docs", "/docs/:path*"]
}
],
"development": {
"local": 3001
}
}
}
}
```

### 3. Configure your development tasks

Ensure each microfrontend has a development task in its `package.json` that points to the port that Turborepo supplies:

```json title="./apps/docs/package.json"
{
"name": "docs",
"scripts": {
"dev": "next dev --port $(microfrontends port)", // [!code highlight]
"build": "next build"
}
}
```

Refer to the documentation for your framework, library, or runtime to learn how to set the port for the script.

### 4. Add the `@vercel/microfrontends` package to child apps

Child applications need the `@vercel/microfrontends` package added to their `package.json` to be able to proxy their routing.

<PackageManagerTabs>
<Tab value="pnpm">
```diff title="./apps/docs/package.json"
{
"name": "docs",
"devDependencies": {
+ "@vercel/microfrontends": "workspace:*"
}
}
```
</Tab>
<Tab value="yarn">
```diff title="./apps/docs/package.json"
{
"name": "docs",
"devDependencies": {
+ "@vercel/microfrontends": "workspace:*"
}
}
```
</Tab>
<Tab value="npm">
```diff title="./apps/docs/package.json"
{
"name": "docs",
"devDependencies": {
+ "@vercel/microfrontends": "workspace:*"
}
}
```
</Tab>
<Tab value="bun (Beta)">
```diff title="./apps/docs/package.json"
{
"name": "docs",
"devDependencies": {
+ "@vercel/microfrontends": "workspace:*"
}
}
```
</Tab>
</PackageManagerTabs>

### 5. Set up your turbo.json

Ensure Turborepo is handling your development workflows:

```json title="./turbo.json"
{
"tasks": {
"dev": {
"cache": false,
"persistent": true
}
}
}
```

## Running microfrontends

Start all your microfrontends simultaneously:

```bash title="Terminal"
turbo dev
```

Or run specific applications:

```bash title="Terminal"
turbo dev --filter=docs
```
Comment on lines +169 to +171
Copy link
Contributor

Choose a reason for hiding this comment

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

A callout that if only docs is run all web routes will be served by the "default" host might be useful.


With the microfrontends configuration, Turborepo will:

1. Start each application on its designated port
2. Handle routing between applications
3. Provide a unified terminal UI for monitoring all applications

## Port management

Turborepo provides two ways to assign ports to your microfrontends:

### Explicit port mapping

Specify exact ports in your configuration:

```json title="microfrontends.json"
{
"applications": {
"app": {
"development": {
"local": {
"port": 3000
}
}
}
}
}
```
Comment on lines +187 to +199
Copy link
Contributor

Choose a reason for hiding this comment

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

The "Explicit port mapping" section shows an incorrect JSON structure for the local property that conflicts with both the main example (lines 63, 74) and the actual implementation in the example code.

View Details
📝 Patch Details
diff --git a/docs/site/content/docs/guides/microfrontends.mdx b/docs/site/content/docs/guides/microfrontends.mdx
index 67d5b1f74..916f38633 100644
--- a/docs/site/content/docs/guides/microfrontends.mdx
+++ b/docs/site/content/docs/guides/microfrontends.mdx
@@ -189,9 +189,7 @@ Specify exact ports in your configuration:
   "applications": {
     "app": {
       "development": {
-        "local": {
-          "port": 3000
-        }
+        "local": 3000
       }
     }
   }
@@ -215,8 +213,7 @@ An object where each key is an application identifier and the value contains:
 - **`packageName`** (optional): The package name if different from the application's key in the configuration
 - **`development`**: Development configuration
   - **`task`** (optional): The task name to run for development (defaults to `"dev"`)
-  - **`local`**: Local development settings
-    - **`port`** (optional): Specific port number. If not specified, a consistent port is generated based on the application name
+  - **`local`** (optional): Port number, host string, or full URL for local development (e.g., `3000`, `"my.localhost.me:8080"`, or `"https://my.localhost.me:8080"`). If not specified, a consistent port is generated based on the application name
 
 ### Child configurations
 

Analysis

Incorrect JSON structure for local property in microfrontends.mdx documentation

What fails: The "Explicit port mapping" section (lines 187-199) and Configuration Options section (lines 218-219) in docs/site/content/docs/guides/microfrontends.mdx showed incorrect JSON structure "local": { "port": 3000 }, conflicting with the actual schema and all working examples which use "local": 3000.

How to reproduce:

  1. Compare line 74 showing "local": 3000 (correct)
  2. Compare lines 192-194 showing "local": { "port": 3000 } (incorrect)
  3. Verify against official schema which defines local as "number or string", not an object
  4. Check examples/with-microfrontends/apps/web/microfrontends.json which uses "local": 3000

Result: Documentation showed two different, conflicting structures for the same property, causing users to write invalid configuration files.

Expected: Documentation should consistently show "local": 3000 (or string variants like "local": "localhost:3000") throughout, matching the official schema definition that local is a "number or string" per the Vercel microfrontends schema.


### Automatic port generation

If no port is specified, Turborepo generates a consistent port between 3000-8000 based on the application name. This ensures the same application always gets the same port across different machines and developers.

### Configuration options

#### `version`

Specifies the configuration schema version. Currently only version `"1"` is supported.

#### `applications`

An object where each key is an application identifier and the value contains:

- **`packageName`** (optional): The package name if different from the application's key in the configuration
- **`development`**: Development configuration
- **`task`** (optional): The task name to run for development (defaults to `"dev"`)
- **`local`**: Local development settings
- **`port`** (optional): Specific port number. If not specified, a consistent port is generated based on the application name

### Child configurations

Individual microfrontend packages can declare they're membership of a parent microfrontend using a `partOf` field. In the configuration below, the `partOf` field explicitly makes the app in `./apps/docs` a child of the `web` application.

```json title="./apps/docs/microfrontends.json"
{
"partOf": "web"
}
```
4 changes: 4 additions & 0 deletions docs/site/dictionary.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
TODO
callout
Microsyntaxes
microfrontend
Microfrontend
microfrontends
Microfrontends
PlatformTabs
uncompiled
JSX
Expand Down
38 changes: 38 additions & 0 deletions examples/with-microfrontends/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# Dependencies
node_modules
.pnp
.pnp.js

# Local env files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# Testing
coverage

# Turbo
.turbo

# Vercel
.vercel

# Build Outputs
.next/
out/
build
dist


# Debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Misc
.DS_Store
*.pem
Empty file.
Loading
Loading