Vite

Tuning Vite's first page load with server.warmup

Measure Vite startup separately from first navigation, select early client and SSR modules for warmup, and verify the transform cache gains.

Tuning Vite's first page load with server.warmup

Vite can report that the dev server is ready while the first browser visit still waits on a chain of module transforms. I use server.warmup to move selected transforms off that request path and measure the difference separately from server startup.

Measure startup and first navigation separately

An on-demand server can listen without transforming the entire application. The browser then requests the entry module, reads its imports, and requests the next group. Until those responses arrive, deeper imports are unknown. A large React application can have a quick VITE v5.4 ready in 150 ms line and still sit on a blank page long enough to notice.

sequenceDiagram
    autonumber
    actor Browser as Browser
    participant Vite as Vite Dev Server (on demand)
    Browser->>Vite: 1. GET /index.html
    Vite-->>Browser: Serve index.html
    Browser->>Vite: 2. GET /src/main.tsx
    Note over Vite: Wait for main.tsx to parse and transform
    Vite-->>Browser: Return main.tsx (browser discovers 30 imports)
    Browser->>Vite: 3. GET /src/App.tsx, GET /src/router.tsx ... (30 concurrent requests)
    Note over Vite: Wait for module transforms and discover more dependencies
    Vite-->>Browser: Return 30 modules (browser discovers 100 more imports)
    Browser->>Vite: 4. Request deeper modules as imports are discovered
    Note over Browser,Vite: First render waits for the import chain (1,500–3,000 ms in this example)

Each import adds work for network scheduling or for the server transforms. A wide graph can send many requests in parallel. A deep graph still waits for each discovery step. I record the browser request waterfall and keep the module transform timings separate from JavaScript execution and rendering in the browser.

Warm the modules the first route usually needs

Vite added server.warmup in version 5. The option schedules transforms for paths I expect the browser to request. Subsequent requests can reuse those results when the warmup has completed and the cache entry is still valid. A request that arrives first can still pay on-demand transform cost.

flowchart TD
    subgraph ServerStartup["1. Vite startup with server.warmup"]
        Start["Run pnpm dev"] --> Ready["Bind the development port"]
        Ready --> WarmupWorker["Start warmup"]
        WarmupWorker --> PreTransform["Transform selected clientFiles"]
        PreTransform --> Cache["Cache transforms in memory"]
    end
    subgraph BrowserConnect["2. Browser first navigation"]
        UserReq["Navigate to localhost:5173"]
        UserReq --> CacheHit{"Transform cached?"}
        CacheHit -- "Hit" --> InstantServe["Serve cached transform"]
        CacheHit -- "Miss" --> OnDemand["Transform on demand"]
    end
    Cache -.-> CacheHit

clientFiles identifies modules for the browser. ssrFiles identifies modules for server-side rendering, where transforms can use a different environment. I start with the client entry, first layout, router, and known expensive dependencies directly involved in the landing route.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  server: {
    port: 5173,
    // Tune the first development page load
    warmup: {
      clientFiles: [
        // 1. Application entry points and global styles
        './src/main.tsx',
        './src/App.tsx',
        './src/index.css',

        // 2. Top-level layouts and router for the first render
        './src/routes/rootLayout.tsx',
        './src/routes/dashboard.tsx',

        // 3. Expensive shared components on the initial route
        './src/components/common/DataGrid.tsx',
        './src/components/common/ChartWidget.tsx',
        './src/components/ui/**/*.tsx'
      ],
      // Warm the server entry separately for SSR projects
      ssrFiles: [
        './src/entry-server.tsx'
      ]
    }
  }
});

Keep the warmup set narrow. In this example, the target is roughly 30–50 modules on the default route. Treat that as a starting set drawn from the project trace. Warming every file competes for CPU and memory, including on routes a developer may never open.

Compare the first request before and after

The example measures a cold development start on a Vite 5 React project with roughly 1,200 components. The results depend on which route loads first, the machine, and whether dependency and browser caches were also cold.

MetricBeforeAfterChange
Development server startup158 ms162 ms+4 ms (+2.5%)
First page time to first byte1,420 ms135 ms90.5% shorter
Time blocked on module transforms1,280 ms42 ms96.7% shorter
DOMContentLoaded1,650 ms198 ms88.0% shorter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{
  "type": "bar",
  "data": {
    "labels": [
      "Cold Start",
      "First page TTFB",
      "Transform blocking",
      "DOMContentLoaded"
    ],
    "datasets": [
      {
        "label": "Before warmup (ms)",
        "data": [158, 1420, 1280, 1650]
      },
      {
        "label": "After warmup (ms)",
        "data": [162, 135, 42, 198]
      }
    ]
  },
  "options": {
    "plugins": {
      "title": {
        "display": true,
        "text": "Initial loading before and after Vite warmup (ms)"
      }
    },
    "scales": {
      "y": {
        "beginAtZero": true,
        "title": {
          "display": true,
          "text": "Duration (ms)"
        }
      }
    }
  }
}

The small startup difference and large navigation difference answer different questions. The server can bind its port while scheduled work is still in progress. The first navigation benefits if that work fulfills eagerly needed transforms before the browser reaches them.

Inspect which transforms happened before navigation

Terminal showing Vite warming 34 selected modules after server startup Figure 1. The sample run reports 34 pre-transformed modules in 72 ms.

Use DEBUG=vite:warmup pnpm dev for the version shown and inspect which files the warmup selected. If the debug namespace differs in the installed Vite version, check the documentation and look at the transform logs and network waterfall.

Terminal comparing cold first page latency before and after warmup Figure 2. The benchmark keeps server readiness, browser TTFB, and completion of initial loading distinct.

Trim the warmup list with measurements

  • Begin with the route developers open most frequently, then add the shared layout or router.
  • Check wrappers around expensive editor, chart, or table components if they are encountered on that route. A warmed source file can reduce server transform time, while browser parsing and rendering remain separate costs.
  • Compare cold runs using the same route and cache conditions.
  • Remove paths with no measurable benefit. Leave rarely visited routes to on-demand transformation.

The Vite server option reference describes the client and SSR patterns supported by each version.

This post is licensed under CC BY 4.0 by the author.