Skip to content

API Reference Common

Taras Demyanets edited this page Mar 31, 2026 · 1 revision

API Reference: Common

Package: @microfrontend/common Version: 1.6.0 Install: npm install @microfrontend/common No peer dependencies

This package is installed in both the shell and every microfrontend. It provides the shared messaging protocol, base classes, and infrastructure.


Level (enum)

Logging level for controlling console output verbosity.

import { Level } from '@microfrontend/common';
Value Numeric Description
OFF 0 No logging
ERROR 1 Errors only
WARNING 2 Errors and warnings
INFO 3 General information (default)
DEBUG 4 Debug-level detail
LOG 5 Maximum verbosity

MessagingApiBroker

Message broker that translates window.postMessage events into typed handler callbacks. Validates message origins against a whitelist before dispatching.

import { MessagingApiBroker } from '@microfrontend/common';

This class is used internally by MetaRouter and RoutedApp. You typically do not instantiate it directly.

Key behaviors

  • Listens for message events on the window
  • Validates each message's origin against the allowedOrigins whitelist
  • Dispatches typed messages to registered handler callbacks
  • Extends Destroyable for lifecycle management

Destroyable

Base class providing lifecycle management with a destruction guard pattern.

import { Destroyable } from '@microfrontend/common';

Methods

Method Return Description
destroy() void Marks the object as destroyed. Throws if already destroyed.
isDestroyed() boolean Returns true if destroy() has been called
preventUsageUponDestruction() void Throws Error if the object has been destroyed

Interface: IDestroyable

import { IDestroyable } from '@microfrontend/common';
Method Return Description
destroy() void Destroy the object
isDestroyed() boolean Check if destroyed

Message Types

All messages extend MessageBase and are transmitted via window.postMessage between the shell and microfrontends.

MessageBase

Base interface for all messages.

Property Type Description
message string Message type identifier
source string Sender's meta route name (or shell name)

MessageRouted

Sent by a microfrontend to report its current route to the shell.

Property Type Description
message 'routed'
source string Microfrontend's meta route
subRoute string Current subroute URL

MessageMetaRouted

Sent by the shell to tell a microfrontend it has been activated.

Property Type Description
message 'meta-routed'
source string Shell name
activated boolean Whether the microfrontend is being activated
subRoute string | undefined Subroute to navigate to

MessageGoto

Sent by a microfrontend to request navigation to another microfrontend.

Property Type Description
message 'goto'
source string Sender's meta route
metaRoute string Target meta route
subRoute string | undefined Target subroute

MessageBroadcast

Broadcast message sent between microfrontends (routed through the shell).

Property Type Description
message 'broadcast'
metadata MessageBroadcastMetadata Broadcast metadata
data object Payload

MessageBroadcastMetadata

Metadata accompanying a broadcast message.

Property Type Description
tag string Message tag/type
source string Sender's meta route
recipients string[] | undefined Target meta routes (all if omitted)
isRecipientVisible boolean | undefined Whether the receiving microfrontend is currently visible

MessageSetFrameStyles

Sent by a microfrontend to request iframe style changes.

Property Type Description
message 'setFrameStyles'
source string Microfrontend's meta route
styles IMap<string> CSS styles to apply to the iframe

MessageGetCustomFrameConfiguration

Sent as both a request (microfrontend to shell) and a response (shell to microfrontend) for custom iframe configuration data.

Property Type Description
message 'getCustomFrameConfiguration'
source string Sender
configuration IMap<string> Configuration key-value pairs

MessageStateChanged

Sent by a microfrontend to report dirty state.

Property Type Description
message 'stateChanged'
source string Microfrontend's meta route
hasState boolean Whether the microfrontend has unsaved state
subRoute string | undefined Subroute with the state

MessageStateDiscard

Sent by the shell to tell a microfrontend to discard its state.

Property Type Description
message 'stateDiscard'
source string Shell name
metaRoute string Target meta route
subRoute string | undefined Target subroute

MessageMicrofrontendLoaded

Notification that a microfrontend has finished loading.

Property Type Description
message 'microfrontendLoaded'
metaRoute string Loaded microfrontend's meta route

IMap<T>

Generic string-keyed dictionary interface used throughout the library.

import { IMap } from '@microfrontend/common';
interface IMap<T> {
  [key: string]: T;
}

IConsoleFacade

Interface for structured logging.

import { IConsoleFacade } from '@microfrontend/common';
Method Description
error(message, ...params) Log an error
warning(message, ...params) Log a warning
info(message, ...params) Log information
debug(message, ...params) Log debug information
log(message, ...params) Log at max verbosity

Callback Types

HandleBroadcastNotification

type HandleBroadcastNotification = (metadata: MessageBroadcastMetadata, data: object) => void;

HandleGetCustomFrameConfiguration

type HandleGetCustomFrameConfiguration = (configuration: IMap<string>) => void;

Clone this wiki locally