DEV Community

Cover image for Sharing components Micro Frontends
Ricardo Maia
Ricardo Maia

Posted on

Sharing components Micro Frontends

Sharing components like common buttons and input fields between microservices can be achieved through several strategies. Here are some effective approaches:

๐Ÿญ. ๐—–๐—ผ๐—บ๐—ฝ๐—ผ๐—ป๐—ฒ๐—ป๐˜ ๐—Ÿ๐—ถ๐—ฏ๐—ฟ๐—ฎ๐—ฟ๐—ถ๐—ฒ๐˜€
Create a shared component library that includes all the reusable components (e.g., buttons, input fields). This library can be published as a package to a package registry (e.g., npm) and then imported into each microservice.

. ๐™Ž๐™ฉ๐™š๐™ฅ๐™จ:
    ย 1. Create a separate repository for the component library.
    ย 2. Develop and test the components.
    ย 3. Publish the library to a package registry.
    ย 4. Install the library in each microservice project.
.๐˜ผ๐™™๐™ซ๐™–๐™ฃ๐™ฉ๐™–๐™œ๐™š๐™จ:
    โ€Š-โ€ŠCentralized management of components.
    โ€Š-โ€ŠEasy updates and versioning.
.๐——๐—ถ๐˜€๐—ฎ๐—ฑ๐˜ƒ๐—ฎ๐—ป๐˜๐—ฎ๐—ด๐—ฒ๐˜€:
    โ€Š-โ€ŠRequires proper version management.
    โ€Š-โ€ŠAll microservices need to be updated to use the latest version.
Enter fullscreen mode Exit fullscreen mode

๐Ÿฎ. ๐— ๐—ผ๐—ป๐—ผ๐—ฟ๐—ฒ๐—ฝ๐—ผ ๐˜„๐—ถ๐˜๐—ต ๐—ง๐—ผ๐—ผ๐—น๐˜€ ๐—น๐—ถ๐—ธ๐—ฒ ๐—ก๐˜… ๐—ผ๐—ฟ ๐—Ÿ๐—ฒ๐—ฟ๐—ป๐—ฎ
Use a monorepo setup with tools like Nx or Lerna to manage multiple projects in a single repository. This approach allows you to share code and dependencies easily between microservices.

. ๐™Ž๐™ฉ๐™š๐™ฅ๐™จ:
    ย 1. Set up a monorepo with Nx or Lerna.
    ย 2. Create shared component libraries within the monorepo.
    ย 3. Import and use the shared components in each microservice.
.๐˜ผ๐™™๐™ซ๐™–๐™ฃ๐™ฉ๐™–๐™œ๐™š๐™จ:
    โ€Š-โ€ŠSimplifies code sharing and dependency management.
    โ€Š-โ€ŠEasier refactoring and consistency across microservices.
.๐——๐—ถ๐˜€๐—ฎ๐—ฑ๐˜ƒ๐—ฎ๐—ป๐˜๐—ฎ๐—ด๐—ฒ๐˜€:
    โ€Š-โ€ŠMonorepo can become complex to manage as it grows.
    โ€Š-โ€ŠRequires a robust CI/CD pipeline.
Enter fullscreen mode Exit fullscreen mode

๐Ÿฏ. ๐—ฆ๐—ต๐—ฎ๐—ฟ๐—ฒ๐—ฑ ๐—š๐—ถ๐˜ ๐—ฆ๐˜‚๐—ฏ๐—บ๐—ผ๐—ฑ๐˜‚๐—น๐—ฒ
Use Git submodules to include a shared repository (containing the common components) within each microservice repository.

. ๐™Ž๐™ฉ๐™š๐™ฅ๐™จ:
    ย 1. Create a repository for shared components.
    ย 2. Add the shared repository as a submodule to each microservice repository.
    ย 3. Use the shared components in the microservices.
.๐˜ผ๐™™๐™ซ๐™–๐™ฃ๐™ฉ๐™–๐™œ๐™š๐™จ:
    โ€Š-โ€ŠDirectly integrates with Git.
    โ€Š-โ€ŠUpdates in the shared repository can be pulled into each microservice.
.๐——๐—ถ๐˜€๐—ฎ๐—ฑ๐˜ƒ๐—ฎ๐—ป๐˜๐—ฎ๐—ด๐—ฒ๐˜€:
    โ€Š-โ€ŠManaging submodule updates can be cumbersome.
    โ€Š-โ€ŠNot as seamless as using a package registry.
Enter fullscreen mode Exit fullscreen mode

๐Ÿฐ. ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ ๐—™๐—ฟ๐—ผ๐—ป๐˜๐—ฒ๐—ป๐—ฑ๐˜€
In a micro-frontend architecture, each microservice can expose its components which can be used by other microservices through a shared component service or by importing them dynamically.

. ๐™Ž๐™ฉ๐™š๐™ฅ๐™จ:
    ย 1. Create a micro-frontend architecture where each microservice can publish its UI components.
    ย 2. Use module federation (e.g., Webpack 5 Module Federation) to dynamically import components.
.๐˜ผ๐™™๐™ซ๐™–๐™ฃ๐™ฉ๐™–๐™œ๐™š๐™จ:
    โ€Š-โ€ŠPromotes independent deployment.
    โ€Š-โ€ŠComponents can be updated independently.
.๐——๐—ถ๐˜€๐—ฎ๐—ฑ๐˜ƒ๐—ฎ๐—ป๐˜๐—ฎ๐—ด๐—ฒ๐˜€:
    โ€Š-โ€ŠRequires a more complex setup and configuration.
    โ€Š-โ€ŠPotential for increased latency and load times.
Enter fullscreen mode Exit fullscreen mode

๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ ๐˜„๐—ถ๐˜๐—ต ๐—ฎ ๐—–๐—ผ๐—บ๐—ฝ๐—ผ๐—ป๐—ฒ๐—ป๐˜ ๐—Ÿ๐—ถ๐—ฏ๐—ฟ๐—ฎ๐—ฟ๐˜† (๐—ป๐—ฝ๐—บ)
Here's an example workflow using a component library published to npm:
๐Ÿญ. ๐—–๐—ฟ๐—ฒ๐—ฎ๐˜๐—ฒ ๐˜๐—ต๐—ฒ ๐—–๐—ผ๐—บ๐—ฝ๐—ผ๐—ป๐—ฒ๐—ป๐˜ ๐—Ÿ๐—ถ๐—ฏ๐—ฟ๐—ฎ๐—ฟ๐˜†:

Image description

๐Ÿฎ. ๐——๐—ฒ๐˜ƒ๐—ฒ๐—น๐—ผ๐—ฝ ๐—–๐—ผ๐—บ๐—ฝ๐—ผ๐—ป๐—ฒ๐—ป๐˜๐˜€:
Create reusable components like Button and Input in the library:

Image description

๐Ÿฏ. ๐—ฃ๐˜‚๐—ฏ๐—น๐—ถ๐˜€๐—ต ๐˜๐—ผ ๐—ป๐—ฝ๐—บ:

Image description

๐Ÿฐ. ๐—จ๐˜€๐—ฒ ๐—ถ๐—ป ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฐ๐—ฒ๐˜€:
Install the library in each microservice:

Image description

Import and use the components in a microservice:

Image description

By following this approach, you can maintain a centralized library of reusable components and easily share them across your microservices, ensuring consistency and reducing duplication.

Top comments (1)

Collapse
 
webjose profile image
Josรฉ Pablo Ramรญrez Vargas

The main 2 disadvantages of a shared NPM library are that you need a private NPM repository, and that your MFE's must all be of the same framework. The beauty of MFE's is framework independence, IMHO.