MicroApp
The MicroApp system is how @esmx/router manages framework-agnostic micro-frontends. Each micro-app provides three lifecycle methods: mount, unmount, and optionally renderToString. The router handles transitions between micro-apps during navigation.
RouterMicroAppOptions
The interface every micro-app must implement.
- Type Definition:
mount
- Type:
(el: HTMLElement) => void
Mount the application into the given DOM element. Called when the router navigates to a route bound to this micro-app.
- Parameters:
el: HTMLElement- The DOM element to mount into (fromRouterOptions.appId)
hydration
- Type:
(el: HTMLElement) => void
Hydrate server-rendered markup instead of mounting from scratch. When the container produced by router.renderToString() carries the data-ssr marker, the router calls hydration with the existing SSR root element rather than mount. If SSR content is present but no hydration function is provided, the router throws an error.
- Parameters:
el: HTMLElement- The pre-rendered SSR root element to hydrate into
unmount
- Type:
() => void
Clean up and destroy the application. Called when navigating away to a route bound to a different micro-app.
renderToString
- Type:
() => Awaitable<string>
Return the SSR HTML string for the current state of the application. Called by router.renderToString() during SSR.
RouterMicroAppCallback
A factory function that creates a micro-app, receiving the router instance.
- Type Definition:
RouterMicroApp
The apps option in RouterOptions accepts either a map of named factories or a single factory.
- Type Definition:
Usage
Registering Micro-Apps
Micro-apps are registered via the apps option on the Router and referenced by the app property in route configs:
React Example
Vue 3 Example
Lifecycle
App Selection
When a route is matched, the router determines which micro-app to use:
- The first matched route config with an
appproperty is used - If
appis astring, it's looked up inrouter.options.apps - If
appis a function, it's called directly as the factory
App Transition
When navigating between routes with different app values:
When navigating within the same app (e.g., /react → /react/about):
- No mount/unmount occurs
- The app handles internal routing via its own component system
Force Restart
router.restartApp() forces a full unmount → mount cycle even if the app key hasn't changed.
SSR Flow
During SSR:
Root Element
The appId option in RouterOptions determines where micro-apps are mounted:
- If the element exists in the DOM, it's reused
- If it doesn't exist, a
<div>is created and appended todocument.body - Layer routers create their own root elements with overlay styling