Based on the BFF architecture, Modern.js provides cross-project invocation capabilities, allowing BFF functions created in one project to be invoked by other projects through integrated calls, enabling function sharing and feature reuse across projects. Cross-project invocation consists of producer and consumer sides. The producer is responsible for creating and providing BFF services while generating integrated invocation SDK, and the consumer initiates requests through these SDK.
Upgrade Modern.js dependencies to version x.64.4 or higher, then enable cross-project invocation via configuration. Projects with BFF capabilities enabled can act as BFF producers, or you can create standalone BFF applications.
When executing dev or build, the following artifacts for consumers will be automatically generated:
dist/client directorydist/runtime directoryexports field of package.jsonfiles field of package.jsonEnsure the current project has BFF enabled with API files defined under api/lambda. Add the following configuration:
To provide type hints for the integrated invocation SDK, enable the declaration option in TypeScript configuration:
@modern-js/create command:new command,enable BFF:Note: When a project serves solely as a BFF producer, its runtime does not depend on the /src source directory. Removing the /src directory can help optimize the project's build efficiency.
You can initiate requests to BFF producers from projects using any framework via the SDK.
When producer and consumer are in the same Monorepo, directly import the SDK. API functions reside under ${package_name}/api:
When producer and consumer are in separate repositories, publish the BFF producer as an npm package. The invocation method remains the same as intra-Monorepo.
For real-world scenarios requiring custom BFF service domains, use the configuration function:
The configure function from ${package_name}/runtime supports domain configuration via setDomain, interceptors, and custom SDK. When extending both current project and cross-project SDK on the same page: