# googlechrome/web-vitals architecture

> Architecture diagram of the GitHub repository googlechrome/web-vitals, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/googlechrome/web-vitals
- Repository: https://github.com/googlechrome/web-vitals

## Overview

##### Main components
- Library API / Entry Points: At the highest level, the API functions (such as onCLS, onFCP, onLCP, onINP, onTTFB, and the deprecated onFID) act as the main interface to the library.
- Core Measurement Modules: In the “src” directory you have modules dedicated to each metric (for example, files under src/attribution for the attribution build and the separate metric files like onCLS.ts, onFCP.ts, etc.).
- Utility and Helper Modules: The “src/lib” folder contains utility functions (e.g., getUniqueID, doubleRAF, observe functions, polyfill implementations) that support the core metric calculations and event handling.
- Type Definitions: The “src/types” folder provides TypeScript type definitions (e.g., for defining Metric interfaces such as CLSMetric, FCPMetric, etc.) that ensure type safety and consistency.
- Build & Bundling Infrastructure: Files such as rollup.config.js and package.json indicate a modern build process where the code is bundled into different formats (ES module, UMD, IIFE) for different usage scenarios.
- Testing Infrastructure: The “test” directory (with subdirectories for unit tests, end-to-end tests, and integration tests) is used to verify the functionality of the library across different browsers and conditions.
- Documentation & Contribution Guides: The docs and markdown files (README.md, CONTRIBUTING.md, etc.) explain usage, integrations, and developer guidelines.

##### Detail the relationships and interactions
- The API Methods (e.g., onCLS, onLCP) act as the entry points for developers’ code. These functions use the Performance Observer API and other browser performance APIs under the hood.
- Each API method calls helper/util modules from the “src/lib” folder to handle low-level tasks (e.g., scheduling functions with doubleRAF, binding reporters, observing performance events).
- The computed metric objects are created and structured using types defined in “src/types” and potentially augmented with additional debugging data when using the attribution build (from files in src/attribution).
- From a build perspective, these source files are transpiled and bundled (using Rollup, as set up by rollup.config.js) into various module formats which are then published as npm packages and/or used as CDN-hosted scripts.

##### Architectural patterns and design principles
- Modularity and Separation of Concerns: Each metric function is isolated in its own module, and utility functions are decoupled from main functionality, making maintenance and future extensions easier.
- Dual Build Strategy: There are two distinct “builds” – the standard one and an attribution-enhanced variant. This abstraction allows developers to choose between minimal payloads and a more detailed debugging version.
- Event-driven Architecture: The library reacts to performance events (exposed via PerformanceObserver), and these events uniquely drive the measurement reporting.
- Safe use of Browser APIs: The design relies on modern browser APIs and gracefully handles scenarios where an API may not support certain metrics (e.g., falling back when the navigation entry isn’t available).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Browser Layer
    subgraph "Client Browser"
        BPERF["PerformanceObserver API"]:::browser
        BNAV["Navigation Timing API"]:::browser
    end

    %% Web Vitals Library
    subgraph "Web Vitals Library"
        %% API Layer
        subgraph "API Layer"
            APIonCLS["onCLS()"]:::api
            APIonFCP["onFCP()"]:::api
            APIonFID["onFID() (Deprecated)"]:::api
            APIonINP["onINP()"]:::api
            APIonLCP["onLCP()"]:::api
            APIonTTFB["onTTFB()"]:::api
        end
        %% Measurement / Processing Layer
        subgraph "Measurement/Processing"
            UTIL["Utility Functions"]:::util
            ATTR["Attribution Modules"]:::attribution
        end
        %% Data Typing and Structuring
        TYPES["Type Definitions"]:::types
    end

    %% External Integrations and Infrastructure
    EXTINT["Analytics Reporting (e.g., GA, Tag Manager)"]:::external
    BUILD["Build & Bundling (Rollup, TS)"]:::build
    TEST["Test Suite (Unit, E2E)"]:::test
    DOCS["Docs & Contribution Guides"]:::docs

    %% Connections from Browser APIs to API Entry Points
    BPERF -->|"feeds-events"| APIonCLS
    BPERF -->|"feeds-events"| APIonFCP
    BPERF -->|"feeds-events"| APIonFID
    BPERF -->|"feeds-events"| APIonINP
    BPERF -->|"feeds-events"| APIonLCP
    BPERF -->|"feeds-events"| APIonTTFB

    %% API calls utility functions and uses type definitions
    APIonCLS -->|"calls"| UTIL
    APIonFCP -->|"calls"| UTIL
    APIonFID -->|"calls"| UTIL
    APIonINP -->|"calls"| UTIL
    APIonLCP -->|"calls"| UTIL
    APIonTTFB -->|"calls"| UTIL

    UTIL -->|"utilizes"| BPERF

    APIonCLS -->|"creates-metric"| TYPES
    APIonFCP -->|"creates-metric"| TYPES
    APIonFID -->|"creates-metric"| TYPES
    APIonINP -->|"creates-metric"| TYPES
    APIonLCP -->|"creates-metric"| TYPES
    APIonTTFB -->|"creates-metric"| TYPES

    %% Branching for Attribution Build
    APIonCLS -->|"enhances"| ATTR
    APIonFCP -->|"enhances"| ATTR
    APIonFID -->|"enhances"| ATTR
    APIonINP -->|"enhances"| ATTR
    APIonLCP -->|"enhances"| ATTR
    APIonTTFB -->|"enhances"| ATTR

    %% Reporting to External Analytics
    APIonCLS -->|"reports"| EXTINT
    APIonFCP -->|"reports"| EXTINT
    APIonFID -->|"reports"| EXTINT
    APIonINP -->|"reports"| EXTINT
    APIonLCP -->|"reports"| EXTINT
    APIonTTFB -->|"reports"| EXTINT

    %% Build Process
    UTIL -->|"bundled-by"| BUILD
    ATTR -->|"bundled-by"| BUILD
    TYPES -->|"bundled-by"| BUILD

    %% Testing Infrastructure
    TEST -->|"validates"| APIonCLS
    TEST -->|"validates"| APIonFCP
    TEST -->|"validates"| UTIL

    %% Documentation connections
    DOCS -->|"describes"| APIonCLS
    DOCS -->|"describes"| TYPES
    DOCS -->|"describes"| EXTINT

    %% Click Events for API Entry Points
    click APIonCLS "https://github.com/googlechrome/web-vitals/blob/main/src/onCLS.ts"
    click APIonFCP "https://github.com/googlechrome/web-vitals/blob/main/src/onFCP.ts"
    click APIonFID "https://github.com/googlechrome/web-vitals/blob/main/src/onFID.ts"
    click APIonINP "https://github.com/googlechrome/web-vitals/blob/main/src/onINP.ts"
    click APIonLCP "https://github.com/googlechrome/web-vitals/blob/main/src/onLCP.ts"
    click APIonTTFB "https://github.com/googlechrome/web-vitals/blob/main/src/onTTFB.ts"

    %% Click Events for Other Component Mappings
    click ATTR "https://github.com/googlechrome/web-vitals/tree/main/src/attribution/"
    click UTIL "https://github.com/googlechrome/web-vitals/tree/main/src/lib/"
    click TYPES "https://github.com/googlechrome/web-vitals/tree/main/src/types/"
    click BUILD "https://github.com/googlechrome/web-vitals/blob/main/rollup.config.js"
    click TEST "https://github.com/googlechrome/web-vitals/tree/main/test/"
    click DOCS "https://github.com/googlechrome/web-vitals/blob/main/README.md"

    %% Styles
    classDef api fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef util fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef attribution fill:#FFD700,stroke:#000,stroke-width:1px;
    classDef types fill:#FFB6C1,stroke:#000,stroke-width:1px;
    classDef browser fill:#D3D3D3,stroke:#000,stroke-width:1px;
    classDef external fill:#FFA07A,stroke:#000,stroke-width:1px;
    classDef build fill:#E6E6FA,stroke:#000,stroke-width:1px;
    classDef test fill:#FAFAD2,stroke:#000,stroke-width:1px;
    classDef docs fill:#F0E68C,stroke:#000,stroke-width:1px;
```
