Micro frontends are an architectural style where independently deliverable frontend applications are composed into a larger application. This concept extends the principles of microservices to the frontend world, allowing teams to build, deploy, and scale their parts of the application independently. This chapter will explore the integration of micro frontends with Vue.js, covering everything from basic concepts to advanced implementation details.
Micro frontends involve breaking up a frontend monolith into smaller, manageable pieces that can be developed, tested, and deployed independently. Each micro frontend is a self-contained module that represents a distinct feature or part of the application.
In a monolithic frontend, all features and components are part of a single codebase. This can lead to challenges in scaling and maintaining the application. In contrast, a micro frontend architecture splits the application into smaller, self-contained units that can be developed and deployed independently.
To set up a micro frontend architecture, we need tools that facilitate the creation, deployment, and orchestration of micro frontends. Some popular tools include:
The container application is the shell that hosts and orchestrates the micro frontends.
npx create-single-spa --module-type root-config
root-config
setup:
import { registerApplication, start } from "single-spa";
registerApplication({
name: "@my-org/navbar",
app: () => System.import("@my-org/navbar"),
activeWhen: ["/"]
});
registerApplication({
name: "@my-org/home",
app: () => System.import("@my-org/home"),
activeWhen: ["/home"]
});
start();
Micro frontends can be created similarly to regular Vue.js applications, with additional configuration for integration with the container.
npx create-single-spa --module-type app-parcel --framework vue --package-manager npm
main.js
):
import Vue from 'vue';
import singleSpaVue from 'single-spa-vue';
import App from './App.vue';
const vueLifecycles = singleSpaVue({
Vue,
appOptions: {
render(h) {
return h(App);
},
},
});
export const bootstrap = vueLifecycles.bootstrap;
export const mount = vueLifecycles.mount;
export const unmount = vueLifecycles.unmount;
Micro frontends can pass data using props and emit events to communicate changes.
A shared state management solution like Vuex can be used, but it needs to be carefully managed to avoid tight coupling.
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
sharedData: {}
},
mutations: {
updateData(state, payload) {
state.sharedData = payload;
}
}
});
Using a Pub/Sub model can decouple micro frontends further.
import Vue from 'vue';
export const EventBus = new Vue();
EventBus.$emit('event-name', eventData);
EventBus.$on('event-name', (eventData) => {
// Handle event
});
single-spa provides a way to configure routes for each micro frontend.
registerApplication({
name: "@my-org/settings",
app: () => System.import("@my-org/settings"),
activeWhen: ["/settings"]
});
Micro frontends can have their own nested routes using Vue Router.
import Vue from 'vue';
import Router from 'vue-router';
import SettingsHome from './components/SettingsHome.vue';
import SettingsProfile from './components/SettingsProfile.vue';
Vue.use(Router);
const router = new Router({
routes: [
{ path: '/', component: SettingsHome },
{ path: '/profile', component: SettingsProfile }
]
});
export default router;
Handling navigation between micro frontends needs careful consideration to ensure a seamless user experience.
import { navigateToUrl } from 'single-spa';
function handleNavigation(event) {
navigateToUrl(event.target.href);
}
document.querySelectorAll('a').forEach(link => {
link.addEventListener('click', handleNavigation);
});
Each micro frontend should be built and deployed independently, allowing teams to release updates without affecting others.
stages:
- build
- deploy
build:
stage: build
script:
- npm install
- npm run build
deploy:
stage: deploy
script:
- deploy-to-cdn ./dist
Using CI/CD pipelines ensures that micro frontends are automatically tested and deployed.
Maintaining backward compatibility is crucial to ensure that updates to one micro frontend do not break the entire application.
{
"name": "@my-org/navbar",
"version": "1.0.0"
}
Micro frontends offer a scalable and flexible way to build large, complex applications by breaking them down into smaller, manageable pieces. By integrating Vue.js with micro frontend architecture, teams can leverage the benefits of both technologies to create performant and maintainable applications. This chapter has provided a comprehensive guide to setting up, managing, and deploying micro frontends using Vue.js, covering everything from basic setup to advanced communication and routing strategies. Happy coding !❤️