1. What is a micro frontend?Micro frontends are a technical means and method strategy for multiple teams to jointly build modern web applications by independently releasing functions. Micro frontends were formally proposed in the ThoughtWorks Technology Radar in 2016. Drawing on the architectural concept of microservices, a large frontend application is split into multiple independent and flexible small applications, each of which can be independently developed, run, and deployed, and then these small applications are combined into a complete application. Micro frontends can not only integrate multiple projects into one, but also reduce the coupling between projects and improve project scalability. Compared with a whole frontend warehouse, the frontend warehouse under the micro frontend architecture tends to be smaller and more flexible. Architecture Features
2. Currently Available Micro-frontend Solutions2.1 Complete Isolation Based on iframeAs front-end developers, we are already very familiar with iframes, which allow us to run another application independently within an application. advantage:
shortcoming:
2.2 Routing hijacking based on single-spasingle-spa (opens new window) is a currently mainstream micro-frontend using pedestal technology. Its main implementation ideas are: pre-register sub-applications, listen to route changes, load sub-application resources when matching activated routes, call lifecycle functions (initialization, mounting, unmounting) in sequence, and finally render to the container. advantage:
shortcoming:
2.3 Encapsulating components based on web componentsThe official componentization solution achieves decoupling between applications by encapsulating components to a higher degree and integrating micro-applications together in a component loading manner. advantage:
shortcoming:
2.4 quankunQuankun is a layer of encapsulation for single-spa, which mainly solves the pain points and shortcomings of single-spa. It parses HTML through the import-html-entry package to obtain the resource path, and then parses and loads the resources. advantage:
shortcoming:
2.5 EMPMainly based on Webpack5 Module Federation, it is a decentralized micro-frontend implementation solution that can not only isolate applications well, but also easily realize resource sharing and communication between applications. advantage:
shortcoming:
3. Problems we need to solveSplit giant applications to facilitate iterative updates and improve development and deployment efficiency After a monolithic application evolves from an ordinary application to a front-end monolith over a relatively long period of time due to the increase and change of the number of people and teams involved, the application becomes unmaintainable. This kind of problem is especially common in enterprise-level Web applications. The micro-front-end concept can split a huge front-end application into multiple independent and flexible small applications. Each application can be developed, updated, and deployed independently, reducing the coupling between modules and improving the scalability of the project. It is conducive to the gradual optimization of each sub-application without affecting the operation of new functions and other sub-applications. Collaborate across teams to achieve demand division of labor, strive for resource optimization, and improve work efficiency Large demands need to be split, and different parts require different business capabilities, which require collaborative development involving multiple teams or even multiple departments. The so-called professional team does professional things, refines its capabilities, maximizes the utility of its capabilities, improves the overall work efficiency of the team, and achieves the effect of 1+1>2. Compatible with historical applications to achieve incremental development The technology stack of the old project is not unified. The micro-frontend main framework does not restrict the technology stack of the access application and can load different technology stack modules at any time. There is no need to make large-scale transformation and reconstruction to be compatible with existing projects, thereby improving development, testing and maintenance efficiency. IV. Our Solution1️⃣Use the web component-based micro-app framework as the basis to split out sub-applications that can run independently; 2️⃣Further encapsulate the business logic layer to achieve code isolation based on js sandbox and shadow dom; 3️⃣ Extract common modules for ion applications to further reduce the code size of the Jushi project and improve iterative maintenance efficiency. 4.1 Technical FeaturesHigh scalability, independence, and openness: Based on micro-app, the micro-front-end main framework is implemented, and the main application is split into the main platform system and various subsystems, which can be independently delivered and run as front-end sub-applications. The business modules are further abstracted, and the public modules are extracted from the sub-applications. Modular development and deployment are carried out to improve the scalability of each sub-application/business module, reduce coupling, improve development efficiency, and reduce delivery costs. Unify the independence of functional modules and improve security: Common business functions such as user information and role permissions can be abstracted and developed and deployed separately to reduce repeated development and the amount of project code. However, because they need to be integrated into different sub-applications, in order to avoid conflicts with the host page style, a shadow dom+JS sandbox mechanism is used to establish an independent scope and implement custom element. Both JS and CSS are isolated in the dom scope to achieve code isolation, making the code cleaner and neater and reducing coupling. Extract common functional modules to further reduce project redundancy: Use the vite tool to package menus, headers, footers, logins, registrations, etc. into micro ESM modules. For business projects currently implemented with a single technology stack, there is no need to redevelop common modules for each submodule, making the total size of the code package smaller, improving development efficiency, and realizing on-demand asynchronous loading of modules to increase the overall loading speed of the page. 4.2 What is JS SandboxThe JS sandbox is mainly used to isolate variables mounted on the window to ensure the independence of the JS environment between applications. The window accessed when the sub-application is running is actually a Proxy object. All global variable changes of the sub-application are generated in the closure and will not be actually written back to the window, thus avoiding pollution between multiple instances. 4.3 What is shadow domShadow-dom is a node tree outside the DOM tree. It is created based on custom DOM elements (not documents), and the created shadow-dom nodes can be intuitively seen from the interface. More importantly, shadow-dom has good sealing, which can isolate CSS styles and avoid CSS global style pollution. 4.4 How do modules communicate with each other?This solution involves sub-applications split according to business and sub-modules split according to functions, and their communication methods are not connected. 1. The sub-application split by business is not a module, but an application that can run independently. However, frequent communication between applications will increase the complexity and coupling of applications. Therefore, communication between sub-applications should be minimized and interaction should not be allowed unless necessary. Here we can use the API officially provided by micro-app to achieve this. 2. The sub-modules split according to the functional modules are sub-modules implemented based on web components. The communication between them can follow the parent component loading the sub-component and the communication between the parent and child components. |
<<: Google now releases the second beta version of the Android 14 system developer preview
>>: Framework learning input touch event principle
Marketing, blindly imitating big companies and ca...
In the process of SEO, we often talk about the te...
As a good friend of mankind, chickens are of grea...
Nowadays, people may not remember to bring toilet...
The bookmark of the stratum - the "golden na...
Zeng Qiao——Teacher of Tsinghua CEO Class/Hejun CE...
In the microscopic world, every cell is a busy ci...
Why have fast-moving consumer goods such as Yuanq...
ASO provides free, high-quality users to your And...
During the Internet advertising process, the oper...
When it comes to iPhone 5c, I believe most people ...
A new exotic substance once again proves the magi...
Content: Changsha Tea Tasting Studio reservation ...
After PC, the next gaming battlefield for domesti...
On the morning of October 28, the 9th Cocos Devel...