Garfish Micro Front-end Framework

repository·main·Indexed 25 days ago

https://github.com/web-infra-dev/garfish

A framework designed to compose multiple independently developed, tested, and deployed applications into a single cohesive user experience. Garfish is framework-agnostic and features a scalable core consisting of a Loader, Router, Sandbox for runtime isolation, Store for communication, and a Plugin mechanism. It supports dependency sharing, pre-loading, and multiple instances on a single page. Version 1.19.8 provides integration bridges for React 18 and templates for Angular, Vue 2, and Vue 3 with Vite.

Tokens
65.6K
Snippets
137
Records
391
Agent score
77%

What's inside Garfish

  1. Overview of Garfish Micro-frontend Framework

    main

    Garfish is a micro-frontend solution designed to decompose large, monolithic web applications into smaller, independently deliverable frontend applications. It allows multiple applications to be developed, tested, and deployed independently while appearing to the user as a single, cohesive product.

    Key Features

    • Framework Agnostic: Supports sub-applications built with any framework or technology stack.
    • Independent Lifecycle: Enables independent development, testing, and deployment for each sub-application.
    • Performance Optimization: Features powerful preloading capabilities that learn user loading habits to reduce application switching time.
    • Dependency Sharing: Supports sharing dependencies to reduce overall bundle size and prevent redundant loading.
    • Multi-instance Support: Allows running multiple sub-applications simultaneously on a single page.
    • Observability: Built-in data collection to monitor application status during runtime.

    Core Modules

    • Loader: Supports both HTML entry and JS entry for easy integration.
    • Router: Provides route-driven rendering and isolation between main and sub-app routes. Users only need to configure a route table to handle rendering and destruction.
    • Sandbox: Provides runtime isolation for JS and CSS to prevent side effects between applications.
    • Store: A simple mechanism for data exchange and communication between applications.
    • Plugin System: A highly extensible mechanism to meet custom business requirements.
  2. Overview of Garfish micro front-end features

    main

    Garfish is a micro front-end framework designed to compose multiple independently delivered applications into a single cohesive product.

    Key capabilities include:

    • Framework Agnostic: Supports any framework or technology system.
    • Independent Lifecycle: Enables independent development, testing, and deployment of sub-applications.
    • Pre-loading: Automatically records user loading habits to reduce application switching time.
    • Dependency Sharing: Reduces package size and prevents repeated loading of dependencies.
    • Multiple Instances: Ability to run multiple sub-applications on a single page simultaneously.
    • Debugging Tools: Provides tools specifically for the micro front-end development experience.
  3. Overview of Garfish Micro-frontend Features

    main

    Garfish is a micro-frontend solution designed to compose multiple independently delivered frontend applications into a single cohesive product. It enables independent development, testing, and deployment of sub-applications.

    Key Features

    • Framework Agnostic: Supports various frameworks and technical stacks for sub-applications.
    • Preloading: Automatically records user loading habits to increase loading priority and reduce application switching time.
    • Dependency Sharing: Reduces overall bundle size and prevents redundant dependency loading.
    • Data Collection: Provides visibility into application states during runtime.
    • Multi-instance Support: Allows running multiple sub-applications on a single page.
    • Debugging Tools: Efficient tools to assist with micro-frontend development workflows.

    Core Modules

    • Loader: Supports both HTML entry and JS entry points for easy sub-app integration.
    • Router: Provides route-driven rendering and isolation between main and sub-app routes. Users only need to configure a route table.
    • Sandbox: Provides runtime isolation for JS and Styles to prevent side effects.
    • Store: A simple mechanism for data exchange and communication.

    Extensibility

    • Plugin System: Highly extensible system to meet custom business requirements.
  4. Get started with Garfish Framework

    main

    Garfish is a micro-frontend framework designed to provide the essential capabilities needed to build micro-frontend systems. It allows you to combine multiple frontend applications (such as Vue, React, and Angular) into a single cohesive product with simple integration.

    To begin using Garfish, you can follow the Quick Start or Getting Started guides available in the documentation.

  5. Understand the Garfish Architecture

    main

    Garfish is a micro-frontend solution designed to provide a Single Page Application (SPA) user experience with the flexibility of a Multi-Page Application (MPA). The architecture consists of three main layers:

    1. Deployment Platform: Handles service discovery, registration, version control, gray releases, incremental upgrades, and dependency analysis to reduce redundant loading.
    2. Framework Runtime: The core engine that manages application lifecycles, sandboxing, routing, and communication.
    3. Debugging Tools: Tools to assist in the development and debugging of micro-frontend applications.

    Garfish uses an SPA-based architecture where the host (main) application controls the rendering and destruction of sub-applications.

  6. Understanding Micro-frontend Architecture

    main

    Micro-frontend is an architectural style similar to microservices, where a large frontend application is composed of multiple independently deliverable sub-applications. This approach decomposes a complex frontend into smaller, simpler applications that can be developed, tested, and deployed independently, while appearing to the end-user as a single, cohesive product.

    Core Objectives

    The goal is to resolve the conflict between Developer Experience (DX) and User Experience (UX) in large-scale web applications by achieving:

    • Isolation: Independent development and deployment for each sub-system.
    • Technology Agnostic: Ability for sub-systems to use different technical stacks.
    • Efficiency: Code reuse of base libraries and non-blocking requirement iterations.
    • Seamless UX: Providing a single-product experience (similar to an SPA) rather than fragmented jumps between platforms (MPA).
    • Granular Control: Centralized permission management and fine-grained monitoring per sub-system.
  7. Understand Garfish Sandbox Isolation Mechanisms

    main

    Garfish provides two primary sandbox isolation strategies to prevent sub-applications from polluting the global environment:

    1. Snapshot Sandbox: Captures a snapshot of the current running environment (e.g., window variables) at a specific stage. When the application is deactivated, it compares the current state with the snapshot and restores the original state.
    2. VM Sandbox: Uses a mechanism similar to Node.js's vm module to create a sandbox environment and execute code within it. This approach handles both style and DOM nodes and supports strict/non-strict modes.

    Garfish prioritizes using Proxy for isolation and falls back to the Snapshot Sandbox in environments where Proxy is not supported.

  8. Understand Garfish Sandbox Mechanisms

    main

    In micro-frontend architectures, multiple independent applications are organized together. Without native isolation like iframe, conflicts such as global variable collisions and style conflicts can occur, leading to broken styles or non-functional features.

    Garfish implements a sandbox mechanism to provide isolation between sub-applications. This mechanism must also support multi-instance scenarios, where multiple instances of the same sub-application can run simultaneously without interfering with each other.

  9. Configure router basename for Vue sub-applications

    main

    To ensure correct routing within the micro-frontend environment, you must set the router's base (Vue 2) or history base (Vue 3) using the basename passed from the main application. It is strongly recommended to use the basename provided by Garfish rather than a hardcoded value to ensure synchronization between the main and sub-applications. Note that Garfish currently only supports sub-applications using history mode.

    // Vue 2 Example
    function newRouter(basename) {
      const router = new VueRouter({
        mode: 'history',
        base: basename,
        routes: [
          { path: '/home', component: Home },
        ],
      });
      return router;
    }
    
    // Vue 3 Example
    function newRouter(basename) {
      const router = createRouter({
        history: createWebHistory(basename),
        base: basename,
        routes,
      });
      return router;
    }
  10. Add standalone execution logic to React micro-apps

    main

    To allow your micro-app to run independently (outside of the Garfish host) for development and deployment, add a check for window.__GARFISH__ in your entry file.

    // For React v16/v17
    if (!window.__GARFISH__) {
      ReactDOM.render(
        <RootComponent
          basename={
            process.env.NODE_ENV === 'production' ? '/examples/subapp/react18' : '/'
          }
        />, document.getElementById("root"));
    }
    
    // For React v18
    if (!window.__GARFISH__) {
      const container = document.getElementById('root');
      const root = createRoot(container!);
      root.render(
        <RootComponent
          basename={
            process.env.NODE_ENV === 'production' ? '/examples/subapp/react18' : '/'
          }
        />
      );
    }
  11. Choose between HTML and JS Entry Types

    main

    Garfish supports two types of application entry points. For cross-team collaboration and reduced communication costs, HTML entry type is highly recommended.

    • HTML Entry Type:

      • Mimics the independent nature of an iframe (allowing independent development, testing, and deployment).
      • The loader parses the HTML, decomposes Script, Style, and Link tags, and hands them to the sandbox.
      • Requires the sub-application to provide a provider containing lifecycle Hooks for rendering and destruction to enhance performance via caching.
      • Unlike an iframe, it runs in the same document flow as the host to avoid UX fragmentation.
    • JS Entry Type:

      • The loader provides a basic DOM container for the application to mount into.