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/: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.
- 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/: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/: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.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.When testing Next.js Pages Router routes, navigate through the proxy and inspect
/_next/datarequests.See the Pages Router QA checklist for cross-app navigation cases to verify.
You can change the port of the local development proxy by setting
options.localProxyPortinmicrofrontends.json:microfrontends.json{ "applications": { // ... }, "options": { "localProxyPort": 4001 } }
To debug issues with microfrontends locally, enable microfrontends debug mode when running your application. Details about changes to your application, such as environment variables and rewrites, will be printed to the console. If using the local development proxy, the logs will also print the name of the application and URL of the destination where each request was routed to.
- Set an environment variable
MFE_DEBUG=1 - Or, set
debugtotruewhen callingwithMicrofrontends
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.
-
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.To test several microfrontends together, start each one in its own repository, then pass all of them to a single proxy as a space-separated list:
microfrontends proxy --local-apps web docsYou run only one proxy regardless of how many applications you run locally. The proxy routes requests for the listed applications to your local processes and sends everything else to the
fallbackURLs in your configuration.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.
When the local proxy falls back to a Vercel deployment protected with Deployment Protection, it needs a Protection Bypass for Automation secret to reach the protected deployment.
The preferred approach is to set a single shared VERCEL_AUTOMATION_BYPASS_SECRET in the default application's local environment. The local proxy reads this value and sends it as the x-vercel-protection-bypass header when proxying to protected child project deployments. When Protection Bypass for Automation is enabled on the default application's project, Vercel sets VERCEL_AUTOMATION_BYPASS_SECRET automatically as a system environment variable, so running vercel env pull in the default app brings it down to your local environment.
For one shared secret to authorize requests to every protected child project, each child project must recognize the same bypass secret value. Copy the default application's secret and add it to each protected child project.
- Navigate to the Vercel project for the default application
- Click on the Settings tab
- Click on Deployment Protection
- Under Protection Bypass for Automation, create a secret if one does not already exist
- Copy the value of the secret
- Navigate to the Vercel project for a protected child application
- Click on the Settings tab
- Click on Deployment Protection
- Under Protection Bypass for Automation, set the secret to the same value you copied from the default app
- Repeat for every protected child project you want the proxy to reach
- Ensure you have the Vercel CLI installed
- Navigate to the root of the default app folder
- Run
vercel loginto authenticate with Vercel - Run
vercel linkto link the folder to the Vercel project - Run
vercel env pullto pullVERCEL_AUTOMATION_BYPASS_SECRETinto your local environment file (for example,.env.local)
Include the previous step in your repository setup instructions, so that other developers pull the secret and have it available locally.
If a project needs its own distinct bypass secret rather than a shared one, set a per-app environment variable named AUTOMATION_BYPASS_<transformed app name> in the default application's local environment. When both VERCEL_AUTOMATION_BYPASS_SECRET and the per-app variable are set for an application, the per-app variable takes precedence.
The name is transformed to be uppercase, and any character that is not a letter or number is replaced with an underscore. For example, the variable name for an app named my-docs-app would be AUTOMATION_BYPASS_MY_DOCS_APP.
Set this variable to the value of the protected project's Protection Bypass for Automation secret, scope it to the Development environment on the default application's project, and run vercel env pull in the default app to bring it down locally.
Was this helpful?