Web Fragments Documentation

repository·main·Indexed 19 days ago

https://github.com/web-fragments/web-fragments

A micro-frontend architecture designed to isolate client-side JavaScript execution into separate contexts while maintaining a shared DOM and browser history. It enables incremental modernization and decentralized scaling of large web applications through a platform-agnostic approach, providing middleware for standard Web request/response models (Cloudflare, Netlify, Vercel) and Node.js environments like Express.

Tokens
15.8K
Snippets
48
Records
78
Agent score
66%

What's inside Web Fragments

  1. What are Web Fragments?

    main

    Web Fragments is a framework, tooling, and platform-agnostic architecture for building micro-frontends. It provides a way to containerize web frontends on both the client-side and server-side, similar to how Docker containerizes applications.

    Key features include:

    • Isolation: Micro-frontends are isolated from each other by executing their client-side JavaScript in separate JavaScript contexts.
    • Cohesion: Despite execution isolation, fragments share the same DOM document, browser navigation, and history, allowing them to form a single, cohesive user experience.
    • Scalability: Enables decomposing monolithic frontends into independently developed and released micro-frontends.
    • Incremental Modernization: Supports low-risk, incremental migration and re-platforming of existing applications.
  2. What is Piercing (eager-rendering)?

    main

    Piercing (also known as eager-rendering) is the capability of Web Fragments to inline the fragment response directly into the initial HTML stream of a Single Page Application (SPA) during the client's initial request.

    This technique is used to improve user experience by reducing Largest Contentful Paint (LCP) and allowing users to interact with fragment portions of the application much sooner.

  3. What is Reframing in Web Fragments

    main

    Reframing is a core architectural feature of Web Fragments that provides isolation and encapsulation for applications running as fragments. It uses a JavaScript and DOM virtualization technique to prevent JavaScript collisions between different fragments and between a fragment and the host application.

    Key benefits include:

    • Isolation: Each fragment runs in its own clean JavaScript context.
    • Encapsulation: DOM operations are scoped to the fragment's Shadow DOM.
    • Automatic Cleanup: When a Web Fragment is destroyed, its associated JavaScript context is also destroyed, automatically freeing up data memory (objects), loaded code, the module registry, timers, and event listeners.
  4. How Reframing works internally

    main

    Reframing is enabled by default for all fragments and works through the following mechanism:

    1. Hidden Iframe: A same-origin, hidden iframe is used to create a clean JavaScript context.
    2. Location Synchronization: The iframe's window.location is synchronized with the host application's window.location.
    3. API Monkey-patching: The iframe is monkey-patched with lightweight versions of DOM and JavaScript APIs. These patches create the illusion that the iframe is the top-level context and document.
    4. DOM Scoping: Instead of operating within the iframe itself, the monkey-patched APIs are augmented to perform operations in the main DOM document, but scoped specifically to the Shadow DOM of the Web Fragment associated with that iframe.

    This process ensures that all DOM operations are "reframed" and executed safely within the host document without causing global collisions.

  5. How Web Fragments are encapsulated

    main

    Web Fragments operate in a virtualized environment that provides access to the viewport, navigation, and history while remaining isolated from the host application.

    Key encapsulation features:

    • DOM Isolation: The fragment's DOM is encapsulated within a Shadow Root to prevent style collisions.
    • JavaScript Isolation: A dedicated JavaScript context (e.g., wf:fragment-id) is created for the fragment, backed by a hidden iframe. This ensures scripts run in an isolated environment.
    • Lifecycle Management: When the <web-fragment> element is removed from the document, the associated JavaScript context, memory, handlers, and scheduled tasks are automatically released.
  6. How Web Fragments handle browser location and history

    main
    Web Fragments provide a consistent browser navigation experience by sharing the internal routing of a fragment with the top-level application's window.location and browser history. This sharing is transparent and works even when the fragment runs in a separate execution context. This allows fragment-level navigation to reflect in the browser's address bar and enables the browser's back/forward buttons to control the fragment's internal state.
  7. How fragment request routing works

    main

    Web Fragments uses a gateway middleware to manage requests.

    1. All requests initiated from the fragment's DOM or JavaScript are intercepted by the fragment gateway middleware.
    2. The middleware identifies requests originating from a specific fragment by checking the routePattern defined in that fragment's registration configuration.
    3. The middleware then reroutes asset requests to the correct fragment endpoint.

    For detailed implementation, see the gateway documentation.

  8. Create a new web fragment

    main
    A Web Fragment is a standalone application built with any web technology stack. It is deployed as a regular HTTP endpoint that serves HTML and static assets. It can also handle form submissions, data requests, and other HTTP requests. Fragments can range from simple UI elements (like a button) to complex, multi-tier full-stack applications with nested routes and layouts.