@mondora/microfrontends
Library for embedding frontends into one another.
Install
yarn add @mondora/microfrontends
Use
In parent apps
import { ChildApp } from "@mondora/microfrontends";
const iframe = document.createElement("iframe");
iframe.src = "https://child.app";
document.body.appendChild(iframe);
const childApp = new ChildApp({
iframe: iframe,
suppliedMethods: {
echo: input => input
},
launchData: {
authToken: "authToken"
},
onLaunched() {
console.log("child app launched");
}
});
childApp.launch();
Or, if you're using React:
import { ChildAppReactComponent } from "@mondora/microfrontends";
<ChildAppReactComponent
url="https://child.app"
suppliedMethods={{
echo: input => input
}}
launchData={{
authToken: "authToken"
}}
launchingPlaceholder={<span>{"Launching child app..."}</span>}
onLaunched={() => {
console.log("child app launched");
}}
/>;
For child apps
import { ParentApp } from "@mondora/microfrontends";
const parentApp = new ParentApp({
onLaunch(launchData) {
ReactDOM.render(<App data={launchData} />);
}
});
parentApp.call("echo", "hello").then(result => {
console.log(result);
});
API
ChildApp
Used from the parent app, creates and launches a child app into an iframe.
Constructor options:
iframe
(required): the iframe pointing to the child apponLaunched
(required): function called when the child app is launchedchildOrigin
: the origin of the child app. In most cases it's detected
automaticallysuppliedMethods
: methods that the child app will be able to calllaunchData
: data to pass to the child app on launch
launch()
Launch the child app.
Returns
Nothing.
<ChildAppReactComponent />
Used from the parent app, React component that creates, launches, and renders a
child app.
Accepted props:
url
: see ChildApp
constructor optionsonLaunched
: see ChildApp
constructor optionschildOrigin
: see ChildApp
constructor optionssuppliedMethods
: see ChildApp
constructor optionslaunchData
: see ChildApp
constructor optionslaunchingPlaceholder
: placeholder to show while the child app is launching
(e.g. a spinner)className
: class assigned to the child app iframe
ParentApp
Used from the child app. It attaches to the parent app, receives launch data and
tells the parent when the child has finished launching. It can be used to call
methods supplied by the parent.
Constructor options:
onLaunch
(required): function called when the child app is launchedparentOrigin
: the expected origin of the parent app. If the parent's origin
differs from it, the child won't attach to it. When not supplied, the child
attaches to the parent regardless of origin
call(name, ...params)
Calls a method supplied by the parent app. Throws an error if the child has not
launched yet.
Params
name
(required): the name of the method to call...params
: the parameters to pass to the method. They must be
JSON-serializable, as they are sent through a cross-window channel
Returns
A promise to the return value of the parent method.