GithubHelp home page GithubHelp logo

nvladimirovi / angular-dream-stack Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kylecannon/angular-dream-stack

0.0 0.0 0.0 358 KB

Example of running multiple Routers and Ngrx Instances for Complex Dashboards with Webpack Module Federation

License: MIT License

JavaScript 16.89% TypeScript 64.90% CSS 3.97% HTML 8.18% SCSS 6.07%

angular-dream-stack's Introduction

Netlify Status

Angular DREAM Stack

DOM-less Routing Engine Allowing Madness.

Demo

Dashboard

Motivation

This work was derived from a need for large enterprise internal facing applications to co-exist in a desktop like environment in the browser. The Angular Router was designed specifically for tying what is shown on the page to the location in the Browser. However, in some instances this can be behavior that is not desired. A good example of this is a dashboard with complex and rich widgets (charting, single metric, session tracing tools, etc). The default routing solution works really well until a widget such as a graph (with multiple routes) needs to be rendered multiple times on the page for different metrics.

Quick Start

Run yarn run-all to get all MFEs running,

Navigate to all below locally to see each application is able to open separately.

http://localhost:4001 - Counter

http://localhost:4002 - Scoreboard

http://localhost:4003 - Tour of Heroes

After verifying, navigate to http://localhost:4004 to play around with the Dashboard

The Solution

Sandboxing via Injector

Due to the power of Angular's Dependency Injection, we can create an isolated injector per widget. We can accomplish this by creating a custom service that provides code-splitting for each widget. This is accomplished by using the widget host component as the parent injector for each widget.

Allowing for Multiple Routers and Ngrx Stores

When we create our sandbox, we will also need to provide our own "override" for Router and Store and PlatformLocation. Overriding the PlatformLocation at the widget level is the key to allowing multiple routers to exist. The parent (root app router) will still have it's default behavior of deep linking, but the widgets will use an "in memory" url.

App Inception

Since each application has a sandboxed instance of the Router and Ngrx, we can have the application render a new instance of itself in itself.

Release the entire application as SPA and MFE App

Let's say we wanted to release the counter as a single page app but also release it in our MFE Dashboard. By taking what would be considered the AppModule and migrating it into a "library" will allow for the build process to consume the core functionality for two separate distribution styles.

  1. The AppModule is now responsible properly bootstrapping the application for SPA distribution. It's sole responsibility is NgModule that imports the CounterModule and has its own bootstrapped AppComponent that invokes the CounterContainerComponent.
  2. The MfeAppModule is responsible for creating a NgModule that implements the LoadableApp interface that provides the EntryComponent to be rendered in as a Federated Module. The MfeAppModule EntryComponent references the same CounterContainerComponent that was used for the SPA distribution, allowing for multiple distribution types of the same application.

Current Shortcomings

  • Hovering over a <a [routerLink]="...">...</a> will present a URL in the browser that will not work.
    • Should not be used with Angular Universal or any public facing site due to this.
    • Can become more supported by working with Angular team.
  • Functionality such as window.history.back() and window.history.forward() do not work since we essentially ripped out all communication with the browser.
  • To be successful with isolation with providers that hold state, refrain from using @Injectable({ providedIn: '...' }} syntax to avoid the compiler hoisting to an injector out of the sandbox.

angular-dream-stack's People

Contributors

kylecannon 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.