Skip to content

Lesson 5 of 9 · 20m

Inside the Dev Server: Requests, Transforms, and Cache

How Vite serves source modules, where transformation cost is spent, and how to diagnose slow startup without guessing.

A Vite dev server is best understood as a specialized HTTP service. The browser asks for /src/main.ts, Vite transforms it and returns JavaScript, then the browser follows its imports. There is no giant upfront application bundle — which means performance is dominated by request shape, transform cost, and dependency optimization rather than one monolithic build bar.

Follow one request

GET /src/App.tsx
  → plugin resolve/load hooks
  → esbuild strips types and compiles JSX
  → import analysis rewrites specifiers
  → response sent with ETag/cache headers

Because the browser executes native modules, unchanged dependencies remain cached. Source files are invalidated when edited. This makes small edits fast, but it also exposes pathological module graphs: one route importing 800 tiny modules can still mean hundreds of requests.

Slow startup usually has boring causes

vite --debug
DEBUG=vite:* vite

Look for repeated dependency optimization, huge plugin transforms, or source files being treated as dependencies. The usual suspects:

  • a barrel file importing half the app (export * from "./all")
  • SVG or markdown transforms implemented with expensive parsing per request
  • monorepo linked dependencies not pre-bundled
  • plugins reading files synchronously or scanning the whole tree
  • CSS preprocessor rebuilds caused by broad imports

Middleware mode is a host integration

Vite can run as middleware inside another server rather than owning the whole port:

const vite = await createServer({
  server: { middlewareMode: true },
  appType: "custom",
});
app.use(vite.middlewares);

This is how SSR frameworks and backend-first apps integrate it. The key mental shift: your backend controls routing and HTML; Vite controls module transformation and asset serving. If cookies, auth headers, or HTML behavior differ, inspect the outer server before blaming module transforms.

Sourcemaps cost memory but save hours

Dev sourcemaps are normally cheap enough. When debugging production-only behavior, use vite preview after a production build; it is not a dev server, but it serves the exact artifacts shape. For deep artifact inspection:

vite build --sourcemap

Field notes

  • A high request count in the network panel often means a barrel-file fanout, not “HTTP is slow”.
  • Restart the dev server after changing dependency versions; optimization is intentionally sticky.
  • Do not put secrets in import.meta.env unless they are intentionally client-visible.
  • server.fs.strict is not bureaucracy. It prevents serving files outside the workspace that symlinks can accidentally expose.

Was this lesson helpful?

Sign in to rate