critical-script
Service

critical-script

2026.08.14
·Web·by Homin.Lee
#CLS#LCP#Optimization#TypeScript#Vite

Key Points

  • 1The `critical-script` Vite plugin optimizes web performance by executing essential TypeScript modules as inline scripts before the main bundle loads.
  • 2By triggering API prefetching and resource loading early in the document head, the plugin significantly improves LCP and CLS metrics.
  • 3This approach mitigates delays caused by JavaScript bundle execution, allowing web applications to fetch critical data and render key visual elements much faster.

The paper introduces a Vite plugin designed to optimize web performance metrics, specifically Largest Contentful Paint (LCP) and Cumulative Layout Shift (CLS), by executing critical TypeScript modules as inline scripts before the main application bundle.

Core Methodology and Technical Implementation

The plugin addresses the "waterfall" delay inherent in modern web application loading. In a standard architecture, critical data fetching (e.g., API calls for home page data) is often bundled within the main JavaScript file. This creates a dependency chain where:
  1. The browser must first download the main bundle.
  2. The browser must execute the bundle.
  3. The API request is initiated only after the bundle execution begins.

This latency severely impacts the perception of performance, as essential resources—such as hero images referenced within API payloads—cannot be discovered until the data arrives.

The critical-script methodology bypasses this by injecting logic directly into the <head><head> of the HTML document. By transpiling and inlining critical TypeScript modules as early-executing scripts, the plugin enables:

  • Early API Prefetching: The fetch operation is triggered immediately upon the browser parsing the <head><head>, allowing the API response to stream in parallel with the download of the main JavaScript bundle.
  • Resource Preloading: By decoupling the data fetch from the application lifecycle, images or other critical assets identified in the API response can be prioritized or preloaded before the main UI framework (e.g., React) initializes.
  • Bridge Communication: Facilitates immediate execution of Webview native bridge calls, which is essential for hybrid applications to avoid UI flickers or layout shifts during initial render.

Performance Impact and Modeling

The effectiveness of this approach is evaluated by reducing the total time to first meaningful render, defined by the relationship between network latency, bundle processing, and API availability. Let TtotalT_{total} represent the time to first render:

Without critical-script:
Ttotal=Tnetwork_bundle+Texec_bundle+Trequest_api+Trender_uiT_{total} = T_{network\_bundle} + T_{exec\_bundle} + T_{request\_api} + T_{render\_ui}

With critical-script:
Ttotal=max⁡(Tnetwork_bundle+Texec_bundle,Trequest_api)+Trender_uiT_{total} = \max(T_{network\_bundle} + T_{exec\_bundle}, T_{request\_api}) + T_{render\_ui}

By shifting Trequest_apiT_{request\_api} to occur concurrently with Tnetwork_bundleT_{network\_bundle}, the plugin minimizes the idle time of the main thread and the network, effectively hiding the API latency behind the resource-heavy bundle download process. This technique is particularly impactful in constrained environments, such as "Slow 4G" networks, where the cost of sequential execution (Tnetwork_bundle+Trequest_apiT_{network\_bundle} + T_{request\_api}) would otherwise result in significant LCP degradation.