Luigi Micro Frontend Framework

repository·main·Indexed 21 days ago

https://github.com/luigi-project/luigi

A micro frontend framework providing a secure container (Luigi Core) and client libraries for communication, routing, and navigation between a host application and distributed micro frontends. Includes specialized support libraries for Angular (@luigi-project/client-support-angular) and UI5 (@luigi-project/client-support-ui5), as well as E2E testing utilities (@luigi-project/testing-utilities) with LuigiMockUtil for frameworks like Cypress, Protractor, and Playwright.

Tokens
136.4K
Snippets
404
Records
508
Agent score
74%

What's inside luigi

  1. Overview of the Luigi Client API

    main

    The Luigi Client API provides a set of tools for managing the lifecycle, navigation, and user experience of micro frontends within a Luigi container. Key functional areas include:

    • Lifecycle Management: Functions to control the lifecycle of various Luigi elements.
    • Callbacks: Event-driven hooks for initListener and customMessageListener.
    • Link Management: Using the linkManager as an alternative to the internal router for navigation.
    • Split View: Enabling a 'split screen' mode to open a micro frontend in the lower part of the content area.
    • UX Management: Functions for controlling user interface elements via uxManager.
    • Storage Management: Using the storageManager API to store and retrieve objects from the Luigi Core local storage.
  2. What is Luigi?

    main

    Luigi is a micro frontend JavaScript framework designed for creating administrative user interfaces driven by local and distributed views. It enables a host web application to communicate with its contained micro frontends.

    Luigi's architecture consists of two main parts:

    1. Luigi Core application: The main container.
    2. Luigi Client libraries: Libraries used within micro frontends.

    Communication between the Core and the micro frontends is established securely using the postMessage API.

  3. Overview of the Luigi Core API

    main

    The Luigi Core API provides a centralized set of functions and options for managing the lifecycle and behavior of a Luigi-based application. It allows developers to control configuration, navigation, localization, and user experience across micro frontends.

    Key functional areas include:

    • Configuration: Functions to manage Luigi's global settings.
    • Navigation: APIs to control how users move between micro frontends and routes.
    • Localization (i18n): Options for managing languages, translations, and localized content.
    • Routing: Tools to get and set search query parameters.
    • Theming & UX: Capabilities for controlling the visual appearance, user interface, and overall user experience.
    • Feature Toggles: A mechanism to enable or disable specific features dynamically.
    • Authorization: Options to manage access control and permissions.
    • Communication: Support for custom messages between Luigi Core and individual micro frontends.
    • Global Search: Integration with Luigi's global search capabilities.
  4. Container Package Features Tested in Test App

    main

    The Container Test App provides simple examples for the following Luigi Container features:

    • Compound Containers: Tests compound features, including:
      • clientAPI: LuigiClient API features.
      • nested: Nested compound component functionality.
      • selfRegistered: The selfRegistered feature.
    • iframe: Luigi Container implementation using iframes.
    • wc: Luigi Container implementation using Web Components.
  5. Create Compound Web Components

    main

    Compound web components allow you to include multiple micro frontends on a single page or nest them within each other using the compound parameter in your Luigi configuration.

    Key Concepts:

    • Slots: For nested web components, the parent component must define <slot> elements (e.g., <slot name="header"></slot>) which Luigi will use to inject child components.
    • Event Bus: Components can communicate via an event bus. You can configure eventListeners in the navigation config to map events from a source component to actions in another.
    • Layouts: You can use a renderer (like grid) to define how child components are arranged.

    Warning: This feature may not work correctly if you load the Luigi library from a CDN. It is recommended to use the @luigi-project/core package from npm.

    {
      pathSegment: 'wc_grid',
      compound: {
        renderer: {
          use: 'grid',
          config: {
            columns: '1fr 1fr 1fr',
            layouts: [{ minWidth: 0, maxWidth: 600, columns: '1fr' }]
          }
        },
        children: [
          {
            viewUrl: 'URL_TO_HEADER/panelHeader.js',
            eventListeners: [{
              source: 'input1',
              name: 'sendInput',
              action: 'update',
              dataConverter: (data) => 'new text: ' + data
            }]
          },
          {
            id: 'input1',
            viewUrl: 'URL_TO_INPUT/input.js'
          }
        ]
      }
    }
  6. Use Intent Mapping for abstract navigation

    main

    The intentMapping array contains abstract intent objects used with LuigiClient.linkManager.navigate(). This allows you to navigate through micro frontends using semantic notations rather than hardcoded paths.

    Intent Object Attributes:

    • semanticObject (string): An alphanumerical representation of a business entity (e.g., a sales order).
    • action (string): An operation to perform on the object (e.g., display, approve, edit). Supports alphanumerical characters and underscores.
    • pathSegment (string): The target path defined in your navigation nodes.
    • externalLink (object): If provided, pathSegment is ignored and the intent resolves to an external URL.
      • url (string): The target URL.
      • openInNewTab (boolean): Whether to open in a new tab.
  7. Understand the difference between `post_logout_redirect_uri` and `logoutUrl`

    main

    The OIDC PKCE plugin uses two different URLs for handling redirects during logout and authentication errors:

    • post_logout_redirect_uri: The OIDC-compliant callback sent to the Identity Provider (IdP). The IdP redirects the browser here after the session ends.
    • logoutUrl (optional): Luigi's internal target for errors. Luigi redirects here during onAuthExpired, onAuthError, silent-renew failures, or failed login callbacks, appending an ?error=... query string.

    Note: If logoutUrl is not set, it defaults to post_logout_redirect_uri. If your post_logout_redirect_uri points to the IdP's end-session endpoint, you should explicitly set logoutUrl to a local application page to avoid redirecting users back to the IdP on every auth error.

  8. Synchronize Angular routes with Luigi using LuigiAutoRoutingService

    main

    The LuigiAutoRoutingService enables synchronization between your Angular application's internal routing and the Luigi micro frontend framework. This is achieved by configuring route data properties in your Angular router.

    Route Configuration Options

    Data KeyDescription
    fromVirtualTreeRoot: trueUpdates Luigi when the component loads using linkManager().fromVirtualTreeRoot().withoutSync().navigate()
    fromVirtualTreeRoot: { truncate: '...' }Truncates the route before sending to Luigi. Supports * for truncation after the first occurrence of a string.
    luigiRoute: '/path'Navigates to a specific Luigi route using linkManager().withoutSync().navigate()
    luigiRoute: '/path', fromContext: trueNavigates using linkManager().fromClosestContext().withoutSync().navigate()
    luigiRoute: '/path', fromContext: 'localContext'Navigates using linkManager().fromContext('localContext').withoutSync().navigate()

    Customizing Route Reuse

    The library provides LuigiRouteStrategy, which is an implementation of Angular's RouteReuseStrategy. To customize how routes are reused, extend it:

    export class YourRouteStrategy extends LuigiRouteStrategy {
        retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
            super.retrieve(route);
            // custom logic
        }
    }
    
    // In your providers
    { provide: RouteReuseStrategy, useClass: YourRouteStrategy }

    Auto-routing for Modals

    If your routes use a modalPathParam, you can enable automatic URL updates and history management by adding these flags to the route data:

    • updateModalDataPath: true: Updates the URL automatically when navigating within the modal.
    • addHistoryEntry: true: Adds a new entry to the browser history for modal changes.
    { 
      path: 'my-modal-route', 
      component: MyModalComponent, 
      data: { updateModalDataPath: true, addHistoryEntry: true } 
    }
    {path: 'sample4', component: Sample1Component, data: { luigiRoute: '/home/sample4' }}
  9. How the Luigi Compound Container works

    main

    The Luigi Compound Container is a specialized container designed to host and manage multiple microfrontends within a single application. It specifically supports the insertion of multiple webcomponent-based microfrontends into one container.

    To interact with the container, you can use the standard JavaScript addEventListener function to react to events emitted by the Luigi Compound Container. This allows the host application to synchronize state or respond to lifecycle changes within the hosted microfrontends.