Microfrontends local development
To provide a seamless local development experience, @vercel/microfrontends provides a microfrontends aware local development proxy to run alongside your development servers. This proxy allows you to only run a single microfrontend locally while making sure that all microfrontend requests still work.
Microfrontends allow teams to split apart an application and only run an individual microfrontend to improve developer velocity. A downside of this approach is that requests to the other microfrontends won't work unless that microfrontend is also running locally. The microfrontends proxy solves this by intelligently falling back to route microfrontend requests to production for those applications that are not running locally.
For example, if you have two microfrontends web and docs:
{
"$schema": "https://openapi.vercel.sh/microfrontends.json",
"applications": {
"web": {
"development": {
"fallback": "vercel.com"
}
},
"docs": {
"routing": [
{
"paths": ["/docs", "/docs/:path*"]
}
]
}
}
}A developer working on /docs only runs the Docs microfrontend, while a developer working on /blog only runs the Web microfrontend. If a Docs developer wants to test a transition between /docs and /blog , they need to run both microfrontends locally. This is not the case with the microfrontends proxy as it routes requests to /blog to the instance of Web that is running in production.
Therefore, the microfrontends proxy allows developers to run only the microfrontend they are working on locally and be able to test paths in other microfrontends.
When developing locally with Next.js any traffic a child application receives
will be redirected to the local proxy. Setting the environment variable
MFE_DISABLE_LOCAL_PROXY_REWRITE=1 will disable the redirect and allow you to
visit the child application directly.
- Set up your microfrontends on Vercel
- All applications that are part of the microfrontend have
@vercel/microfrontendslisted as a dependency - Optional: Turborepo in your repository
In order for the local proxy to redirect traffic correctly, it needs to know which port each application's development server will be using. To keep the development server and the local proxy in sync, you can use the
microfrontends portcommand provided by@vercel/microfrontendswhich will automatically assign a port.package.json{ "name": "web", "scripts": { "dev": "next --port $(microfrontends port)" }, "dependencies": { "@vercel/microfrontends": "latest" } }If you would like to use a specific port for each application, you may configure that in
microfrontends.json:microfrontends.json{ "$schema": "https://openapi.vercel.sh/microfrontends.json", "applications": { "web": {}, "docs": { "routing": [ { "paths": ["/docs", "/docs/:path*"] } ], "development": { "task": "start", "local": 3001 } } } }The
localfield may also contain a host or protocol (for example,my.special.localhost.com:3001orhttps://my.localhost.com:3030).If the name of the application in
microfrontends.json(such aswebordocs) does not match the name used inpackage.json, you can also set thepackageNamefield for the application so that the local development proxy knows if the application is running locally.microfrontends.json{ "$schema": "https://openapi.vercel.sh/microfrontends.json", "applications": { "web": {}, "docs": { "routing": [ { "paths": ["/docs", "/docs/:path*"] } ], "packageName": "my-docs-package" } } }package.json{ "name": "my-docs-package", "scripts": { "dev": "next --port $(microfrontends port)" }, "dependencies": { "@vercel/microfrontends": "latest" } }The local proxy is started automatically when running a microfrontend development task with
turbo. By default a microfrontend application'sdevscript is selected as the development task, but this can be changed with thetaskfield inmicrofrontends.json.Running
turbo web#devwill start thewebmicrofrontends development server along with a local proxy that routes all requests fordocsto the configured production host.This requires version
2.3.6or2.4.2or newer of theturbopackage.Turborepo is the suggested way to work with microfrontends as it provides a managed way for running multiple applications and a proxy simultaneously.
If you don't already use Turborepo in your monorepo,
turbocan infer a configuration from yourmicrofrontends.json. This allows you to start using Turborepo in your monorepo without any additional configuration.To get started, follow the Installing
turboguide.Once you have installed
turbo, run your development tasks usingturboinstead of your package manager. This will start the local proxy alongside the development server.You can start the development task for the Web microfrontend by running
turbo run dev --filter=web. Review Turborepo's filter documentation for details about filtering tasks.For more information on adding Turborepo to your repository, review adding Turborepo to an existing repository.
If you do not want to use Turborepo, you can invoke the proxy directly.
package.json{ "name": "web", "scripts": { "dev": "next --port $(microfrontends port)", "proxy": "microfrontends proxy microfrontends.json --local-apps web" }, "dependencies": { "@vercel/microfrontends": "latest" } }Review Understanding the proxy command for more details.
When testing locally, you should use the port from the microfrontends proxy to test your application. For example, if
docsruns on port3001and the microfrontends proxy is on port3024, you should visithttp://localhost:3024/docsto test all parts of their application.You can change the port of the local development proxy by setting
options.localProxyPortinmicrofrontends.json:microfrontends.json{ "applications": { // ... }, "options": { "localProxyPort": 4001 } }
If you're working with a polyrepo setup where microfrontends are distributed across separate repositories, you'll need additional configuration since the microfrontends.json file won't be automatically detected.
First, ensure that each microfrontend repository has access to the shared configuration:
-
Option 1: Use the Vercel CLI to fetch the configuration:
vercel microfrontends pullThis command will download the
microfrontends.jsonfile from your default application to your local repository.If you haven't linked your project yet, the command will prompt you to link your project to Vercel first.
This command requires the Vercel CLI 44.2.2 to be installed.
-
Option 2: Set the
VC_MICROFRONTENDS_CONFIGenvironment variable with a path pointing to yourmicrofrontends.jsonfile:export VC_MICROFRONTENDS_CONFIG=/path/to/microfrontends.jsonYou can also add this to your
.envfile:.envVC_MICROFRONTENDS_CONFIG=/path/to/microfrontends.json
In a polyrepo setup, you'll need to start each microfrontend application separately since they're in different repositories. Unlike monorepos where Turborepo can manage multiple applications, polyrepos require manual coordination:
Start your microfrontend application with the proper port configuration. Follow the Application setup instructions to configure your development script with the
microfrontends portcommand.In the same or a separate terminal, start the microfrontends proxy:
microfrontends proxy --local-apps your-app-nameMake sure to specify the correct application name that matches your
microfrontends.jsonconfiguration.Visit the proxy URL shown in the terminal output (typically
http://localhost:3024) to test the full microfrontends experience. This URL will route requests to your local app or production fallbacks as configured.
Since you're working across separate repositories, you'll need to manually start any other microfrontends you want to test locally, each in their respective repository.
When setting up your monorepo without turborepo, the proxy command used inside the package.json scripts has the following specifications:
microfrontendsis an executable provided by the@vercel/microfrontendspackage.- You can also run it with a command like
npm exec microfrontends ...(or the equivalent for your package manager), as long as it's from a context where the@vercel/microfrontendspackage is installed.
- You can also run it with a command like
proxyis a sub-command to run the local proxy.microfrontends.jsonis the path to your microfrontends configuration file. If you have a monorepo, you may also leave this out and the script will attempt to locate the file automatically.--local-appsis followed by a space separated list of the applications running locally. For the applications provided in this list, the local proxy will route requests to those local applications. Requests for other applications will be routed to thefallbackURL specified in your microfrontends configuration for that app.
For example, if you are running the Web and Docs microfrontends locally, this command would set up the local proxy to route requests locally for those applications, and requests for the remaining applications to their fallbacks:
microfrontends proxy microfrontends.json --local-apps web docsWe recommend having a proxy command associated with each application in your microfrontends group. For example:
- If you run
npm run docs-devto start up yourdocsapplication for local development, set upnpm run docs-proxyas well- This should pass
--local-apps docsso it sends requests to the localdocsapplication, and everything else to the fallback.
- This should pass
Therefore, you can run npm run docs-dev and npm run docs-proxy to get the full microfrontends setup running locally.
To fall back to a Vercel deployment protected with Deployment Protection, set an environment variable with the value of the Protection Bypass for Automation.
You must name the environment variable AUTOMATION_BYPASS_<transformed app name>. The name is transformed to be uppercase, and any non letter or number is replaced with an underscore.
For example, the env var name for an app named my-docs-app would be:
AUTOMATION_BYPASS_MY_DOCS_APP.
- Navigate to the Vercel project for the protected fallback deployment
- Click on the Settings tab
- Click on Deployment Protection
- If not enabled, create a new Protection Bypass for Automation
- Copy the value of the secret
- Navigate to the Vercel project for the default application (may or may not be the same project)
- Click on the Settings tab
- Click on Environment Variables
- Add a new variable with the name
AUTOMATION_BYPASS_<transformed app name>(e.g.AUTOMATION_BYPASS_MY_DOCS_APP) and the value of the secret from the previous step - Set the selected environments for the variable to
Development - Click on Save
- Ensure you have vc installed
- Navigate to the root of the default app folder
- Run
vc loginto authenticate with Vercel - Run
vc linkto link the folder to the Vercel project - Run
vc env pullto pull the secret into your local environment
Include the previous step in your repository setup instructions, so that other users will also have the secret available.
Was this helpful?