Giter VIP home page Giter VIP logo

micro-frontends-in-action-code's Introduction

Sample Code - Micro Frontends in Action

This is the example code that goes along with the Manning book Micro Frontends in Action.

Hosted examples

You can interact with the running examples here:
https://the-tractor.store ๐Ÿšœ

Local machine

Prerequisites

  1. Clone this git repository or download & extract the zip to your local machine.

  2. Make sure you have Node.js (v12 or newer) installed.

    $ node -v
    v14.4.0
    
  3. Go into the main directory and install all required dependencies.

    cd micro-frontends-in-action-code
    npm install
    
  4. Install Nginx on your machine. The nginx executable only has to be present - no configuration required.

    • macOS (via Homebrew): brew install nginx
    • Debian/Ubuntu: sudo apt-get install nginx
    • Windows: Binaries included in this repository. No installation required.

    Five examples require Nginx:

    • 04_routing
    • 05_ssi
    • 06_timeouts_down
    • 16_universal
    • 18_asset_registration_include

Running the code

All examples involve starting up multiple applications which are owned by different teams. There is an NPM run for each directory. You can start examples like this: npm run [name_of_example].

The following command will start example #5:

npm run 05_ssi

The script starts all applications and shows a combined log output.
NOTE: Nginx stdout do not work on Windows.

console output

It waits until the servers are ready and opens the example page in your default browser.

example running in your browser

You can stop the example by hitting [CMD] + C in your terminal.

NOTE: The code should run on all platforms. However, if you're having troubles feel free to create an issue.

Folder structure and ports

Each example has its own folder (e.g. 05_ssi). Each example folder contains a subfolder that contains the actual application (e.g. nginx, decide, inspire).

Different port numbers are used to indicate ownership. This following table shows which teams owns which application. Make sure ports 3000 to 3003 are not allocated by another process on your machine.

Port Team Responsibility
3000 - shared - infrastructure (web-server, app shell)
3001 Team Decide product page
3002 Team Inspire homepage, search, recommendations
3003 Team Checkout cart, checkout process

List of all examples

Here is a list of all run commands with a reference to the chapter they belong to.

run script name chapter
npm run 01_pages_links Pages & Links 2
npm run 02_iframe iFrames 2
npm run 03_ajax Namespaces 3
npm run 04_routing Server-side Routing 3
npm run 05_ssi Server-side Integration 4
npm run 06_timeouts_down
npm run 06_timeouts_short_delay
npm run 06_timeouts_long_delay
Timeouts & Fallbacks 4
npm run 07_podium Podium 4
npm run 08_web_components Client-side Composition 5
npm run 09_shadow_dom Style Isolation & ShadowDOM 5
npm run 10_parent_child_communication Parent-Child Communication 6
npm run 11_child_parent_communication Child-Parent Communication 6
npm run 12_fragment_fragment_communication Fragment-Fragment Communication 6
npm run 13_client_side_flat_routing Client-side Routing (Flat) 7
npm run 14_client_side_two_level_routing Client-side Routing (Two-Tiered) 7
npm run 15_single_spa Single SPA 7
npm run 16_universal Universal Rendering 8
npm run 17_asset_client_redirect Asset Client-side Redirect 10
npm run 18_asset_registration_include Asset Registration Include 10
npm run 19_shared_vendor_webpack_dll Shared Vendor Libraries via Webpack DLLPlugin 11
npm run 20_shared_vendor_rollup_absolute_imports Shared Vendor Libraries via Rollup.js and Absolute ES Module Imports 11
npm run 21_local_development Local Development 14

micro-frontends-in-action-code's People

Contributors

naltatis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

micro-frontends-in-action-code's Issues

Links produce an HTTPS error on the live demos

I'm getting ERR_SSL_PROTOCOL_ERROR on chrome Version 83.0.4103.116 (Official Build) (64-bit)

image

and on latest firefox

An error occurred during a connection to 01_pages_links.the-tractor.store:3001. 
SSL received a record that exceeded the maximum permissible length.

Error code: SSL_ERROR_RX_RECORD_TOO_LONG

It also does not work on latest safari

Please stop propagating the micro-frontends approach.

Yes, I know how that sounds.

The idea of splitting a codebase into smaller chunks isn't new. I think we can all agree on that. It originated way back in the desktop world where things like dynamically loaded libraries (be that .so, .dll, .jar or what have you) allow for sharing parts of the code between different projects but also allow parts of a single application to be developed independently (to a degree where the API still matches the user and the library). Please note that in all those cases, we're still talking about a single process which matches the way web applications are done.

That can be achieved, although not easily, with npm packages that can be separately deployed to the registry and then linked as a single application that runs in the single-process and single-threaded environment that the browser is. Of course there are separate threads that are being spawn by the browser itsel, but to the user (as in the developer) for all intent and purposes the environment is single threaded and single process.

In contrast, the idea of micro-services is very much different. Although living with just a single thread per process isn't all that unthinkable (if the platform you're running your code deals with it like e.g. node.js does) but running multiple processes inside the browser is just not doable in a performant way. The one and only way to simulate the microservices architecture in the browser is by using iframes. That's a fact.

Following what people think about micro-frontends and what has been proposed is very much different.

First of all, you don't want to have code duplication. Duplicating code makes the app load slower. We don't want that, because it will make our page/app rank lower. We definitely don't want that. This eliminates things like multiple copies of that same library.

Secondly, the idea of having the ability to deploy (we'll get to that what it means in just a second) separately is very tempting. After all, who wouldn't like to be able to deploy part of the system so that the rest of it is oblivious to the change, right? Well, the only problem is that you're not deploying anything at any time other than when the browser requests the resources and runs them. That is where the rubber meets the road. And so any and all ideas that you "deploy to npm registry" or "deploy to server" means absolutely nothing because that is no deployment whatsoever. That's just copying bytes around. They do nothing on the server.

To that end, any mirage of server-side includes and frontend code that stems from it is just the wrong thing to do. Instead of even thinking how to marry the two worlds together, it is much more beneficial to organize the system in a more homogeneous way so that it is not only easier to develop but also to maintain.

In the end it is just the wrong thing to do with all the server-side includes and web components (which nobody uses besides google). Today's FE world runs on frameworks (Vue.js, Angular, React). All of those frameworks are HUGE. Doing what has been proposed, that every team has a certain degree of freedom in choosing their own tool of choice is just not going to fly. Even in the microservices peope tend to standardize on a single language and framework because that way it is far easier to find developer replacement in this world where a typical timespan over which a developer works for a single project (or company) is about 6 months.

To bottom line this it is my humble request that you stop promoting this solution that (as time has shown) only works in hardly-working examples, all of which violate the primary rule of what architecture should provide: ease of development and maintenance. This is the take-away from my 5 years with the idea, trying to implement it in real-world scenarios. Not a single one has led to better DX nor to faster load times.

Working with FE is hard enough. Don't make it harder for no reason, selling dreams that never come true.

Error when trying out the books' code samples

I have the following error when executing the books' code samples using Node.js version 14.16.0 with NPM version 6.14.11 on Powershell 7.1.3 Windows 10 :

`PS C:\Users\uzabu\dev\learn\microfrontend> npm run 01_pages_links

[email protected] 01_pages_links C:\Users\uzabu\dev\learn\microfrontend
concurrently --names "decide ,inspire" "mfserve --listen 3001 01_pages_links/team-decide" "mfserve --listen 3002 01_pages_links/team-inspire" "wait-on http://localhost:3001/product/porsche && opener http://localhost:3001/product/porsche"

[decide ] Error occurred when executing command: mfserve --listen 3001 01_pages_links/team-decide
[decide ] Error: spawn cmd.exe ENOENT
[decide ] at Process.ChildProcess._handle.onexit (internal/child_process.js:269:19)
[decide ] at onErrorNT (internal/child_process.js:465:16)
[decide ] at processTicksAndRejections (internal/process/task_queues.js:80:21)
[inspire] Error occurred when executing command: mfserve --listen 3002 01_pages_links/team-inspire
[inspire] Error: spawn cmd.exe ENOENT
[inspire] at Process.ChildProcess._handle.onexit (internal/child_process.js:269:19)
[inspire] at onErrorNT (internal/child_process.js:465:16)
[inspire] at processTicksAndRejections (internal/process/task_queues.js:80:21)
[2] Error occurred when executing command: wait-on http://localhost:3001/product/porsche && opener http://localhost:3001/product/porsche
[2] Error: spawn cmd.exe ENOENT
[2] at Process.ChildProcess._handle.onexit (internal/child_process.js:269:19)
[2] at onErrorNT (internal/child_process.js:465:16)
[2] at processTicksAndRejections (internal/process/task_queues.js:80:21)
[2] wait-on http://localhost:3001/product/porsche && opener http://localhost:3001/product/porsche exited with code -4058
[inspire] mfserve --listen 3002 01_pages_links/team-inspire exited with code -4058
[decide ] mfserve --listen 3001 01_pages_links/team-decide exited with code -4058
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] 01_pages_links: concurrently --names "decide ,inspire" "mfserve --listen 3001 01_pages_links/team-decide" "mfserve --listen 3002 01_pages_links/team-inspire" "wait-on http://localhost:3001/product/porsche && opener http://localhost:3001/product/porsche"
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 01_pages_links script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\uzabu\AppData\Roaming\npm-cache_logs\2021-04-27T07_42_34_137Z-debug.log`

Issues running on Ubuntu 20.10

When I run the Node examples using Nginx on Ubuntu 20.10 I get the following error (as a user in the terminal):

> [email protected] 04_routing /home/ian/dev/micro-frontends/micro-frontends-in-action-code
> concurrently --names "nginx  ,decide ,inspire" "node nginx.js 04_routing/webserver/nginx.conf" "mfserve --listen 3001 04_routing/team-decide" "mfserve --listen 3002 04_routing/team-inspire" "wait-on http://localhost:3000/product/porsche && opener http://localhost:3000/product/porsche"

[nginx  ] ๐Ÿ“ฎ ERROR: Unable to start Nginx
[nginx  ] nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
[nginx  ] 
[nginx  ] ๐Ÿฆ‘ Is Nginx installed?
[nginx  ] Install it via your system's package manager.
[nginx  ] e.g. $ sudo apt-get install nginx

To get around the permission issue I ran as root (in IntelliJ IDEA) but then I get:

> [email protected] 05_ssi /home/ian/dev/micro-frontends/micro-frontends-in-action-code
> concurrently --names "nginx  ,decide ,inspire" "node nginx.js 05_ssi/webserver/nginx.conf" "mfserve --listen 3001 05_ssi/team-decide" "mfserve --listen 3002 05_ssi/team-inspire" "wait-on http://localhost:3000/product/porsche && opener http://localhost:3000/product/porsche"

[nginx  ] ๐Ÿ“ฎ ERROR: Unable to start Nginx
[nginx  ] nginx: [emerg] open() "/dev/stdout" failed (6: No such device or address)
[nginx  ] 
[nginx  ] ๐Ÿฆ‘ Is Nginx installed?
[nginx  ] Install it via your system's package manager.
[nginx  ] e.g. $ sudo apt-get install nginx

The book advises just to install Nginx -- no configuration is necessary. I reinstalled it but to no avail.

Please advise.

Great book and a few suggestions

Michael, first of all thanks a lot for putting up such a huge effort for the book and the examples here. They are greatly helpful to follow along the book. I am trying the examples one by one and made some observations on why you have written code in such a way by tweaking the code in many different ways and learning from it. Things such as :

const template = product => {
  return `
    <a href="/">&lt; home</a> -
    <a href="/checkout/cart">view cart &gt;</a>
    <h1>${product.name}</h1>
    <img src="https://mi-fr.org/img/${product.img}" width="200">
  `;
};

this.innerHTML = template(product);

These things can be in the form of simple comments within the example code would be of great help. I can create PRs with these documentation comments if possible.

Once again, thanks a lot for the book, it is the goto-guide for micro frontends and I am enjoying each and every line of it, the sections this chapter covers, summary , the graphics and the demo page are all phenomenal, and overall a great learning experience.

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.