Giter Site home page Giter Site logo

microfronts's Introduction

Microfronts

"One shell to rule them all, One shell to bind them, One shell to wrap them all and in the light to run them"

Polyglot Front-End Solution

Micro-Frontends approach enables us to split our products into separate modules as any of them is built with any web technology (i.e. React/Angular/Vue/...). A thin code layer orchestrates them as a single product, keeping the UX intact. The approach enables companies to suspend rewrites of old production code and combine new technologies with legacy ones without breaking everything.

Microfronts orchestrates multiple front-end applications with shared runtime and fully controlled sandboxing at the same time.

Creating the shell application

The shell application should be a super-thin layer of html, css and a tiny javascript file.

The shell contains the configuration for your front-ends bound with the relevant routes. The microfronts library will orchestrate the front-ends whenever route changes. It will also provide shared runtime fully accessible by demand.

Quick example:
<!-- index.html -->
<body>
  <a href="#/home">HOME (react app)</a>
  <a href="#/home">SETTINGS (angular app)</a>
  <a href="#/side-by-side">SIDE-BY-SIDE (both apps)</a>
  <iframe is="app-container" app-id="react-app"></iframe>
  <iframe is="app-container" app-id="angular-app"></iframe>
  <iframe is="app-container" app-id="error"></iframe>
  <script src="./shell.js" type="module"></script>
</body>

npm install microfronts or yarn add microfronts Microfronts should be installed in your shell application and NOT in your applications. This ensures the orchestrator remains a singleton.

// shell.js
import { Microfronts } from 'microfronts';
const application = Microfronts();
const router = application.getRouter();
const context = application.getAppContext();

const REACT_APP = {
    base: 'https://my.domain.com/app-1',
    appId: 'react-app'
};
const ANGULAR_APP = {
    base: 'https://other.domain.com/app-2',
    appId: 'angular-app'
};
const NOT_FOUND = {
    base: './404.html',
    appId: 'error'
}

router.registerRoute('*', { active: [NOT_FOUND] });
router.registerRoute('home', { active: [REACT_APP] });
router.registerRoute('settings', { active: [ANGULAR_APP] });
router.registerRoute('side-by-side', { active: [REACT_APP, ANGULAR_APP] });

router.init();

NOTE: Ensure your servers provide cross-origin access from where the shell is deployed. This can be solved on the server side or by a reverse-proxy.

To see code example, take a look at the examples folder (available in the github repo).

For type declarations you may install Microfronts and import only the interfaces.d.ts file.

Accessing the shared context

Microfronts provides a runtime-shared application context, which can be consumed by running front-ends. The context also enables the front-ends to provide utilities to other front-ends.

For example

Angular holds the user service, containing the data, login and other actions. It exposes a RxJs Subject object and would like to expose it to the rest of the world.

// angular-app/services/User.service.ts
@Injectable() class UserService {
    public stream$ = new BehaviorSubject<UserData|null>(null);
    constructor() {
        window.AppContext.set('services.stream', this.stream$);
    }
}
// react-app/components/UserStatus.js
export default () => {
    const [ user, setUserData] = useState(null);
    useEffect(() => {
        const subscription = window.AppContext.get('services.stream')
                .subscribe(data => setUserData(data)));
        return () => subscription.unsubscribe();
        }
    });
    return user
        ? <div>{user.name}, {user.lastLogin}</div>
        : <div></div>
};

This way the Angular service can be consumed at runtime within the React application. No need to rewrite or duplicate the business logic.

For asynchronous consumption, The AppContext provides the provide and require methods. The require receives a promise, fulfilled one provide is triggered with a value. This enables async dependency management across frameworks.

Adding more modules to your project

Now that the shell contains at least one front-end, more screens can be added freely. Ensure all of your internal routers use the hash-strategy (for consistency). Use the Microfronts' Router.Navigate whenever possible, though the router can watch changes from the inside.

More to come

Microfronts provides more features, such as route guards (used for dirty-clean state checking, privileges, etc.), static data per-application, messaging and more.

We need your support!

If you wish to join - open an issue, suggest an improvement, create pull-request or join the team.

Currently documentation is only inside the code, we appreciate help wiring up a good documentation webpage.

#usetheplatform

microfronts's People

Contributors

0xflotus avatar dependabot[bot] avatar eavichay avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.