Skip to content
ClapelloNotes by Barath
ReactCh 02 · 5/6
Core3 min readUpdated

React / 08

The dev server, HMR and caching

TL;DR

While you code, the dev server rebuilds on every save and hot-swaps just the changed file, so the page updates and your state survives.

Dev mode vs a production build

The same tool runs in two modes, tuned for different people:

Dev server (npm run dev)Production build (npm run build)
ForYou, while codingYour users
SpeedStarts fast, updates on every saveSlower, runs once
OutputServed from memory, nothing writtenOptimised files in dist/
CodeUnminified, source maps, React warningsMinified, tree-shaken, hashed names

Never measure performance on the dev server. It's deliberately unoptimised, and React runs extra checks in development.

What happens when you save

1 · you

Save MenuCard.jsx

⌘S / Ctrl+S
file watcher notices

2 · dev server

Recompiles one file

everything else comes from its cache
message over a WebSocket

3 · browser

Swaps the module

React re-renders MenuCard. Counter still says 3.
HMR swaps only the module you edited into the running page. No full reload, so state like a typed input or a counter usually survives.
  1. The watcher notices

    A file watcher listens to the operating system's file-change events, so it knows the moment you save.
  2. Only that file is recompiled

    Everything else comes from cache, which is why it takes milliseconds.
  3. The browser is told

    The dev server keeps a WebSocket (a connection that stays open) to the page and sends "this module changed".
  4. The module is swapped

    The page fetches the new module and replaces the old one. With React, the edited components re-render.

That last step is Hot Module Replacement (HMR). React's flavour of it is called Fast Refresh, and both Vite's React plugin and Parcel switch it on for you.

Try it

Full reload vs HMR

Try it

localhost:5173

Tasty Bites

Cart: 0

Running. Add some items, then save an edit.

Add a few items, then save an edit in each mode. A full reload throws away state; HMR keeps it.

A full reload restarts JavaScript from zero, so all state is lost. HMR keeps the running app and replaces only the edited code.

Caching makes the second start fast

The first npm run dev does extra work, and the results are saved for next time:

  • Vite serves your source files to the browser as native ES modules, transforming each one on request, so it doesn't bundle your app in dev at all. It does pre-bundle your dependencies (React and friends) once with Rolldown and caches them in node_modules/.vite.
  • Parcel builds a bundle in dev too, and caches every step in .parcel-cache at the project root. Later builds reuse whatever didn't change.

Both caches are safe to delete. The next start is just slower.

Handy dev server options

npx vite --port 3000      # use another port
npx vite --open           # open the browser for you
npx vite --force          # ignore the dependency cache
npx parcel src/index.html --port 3000 --https   # Parcel can serve over https

In a script you'd write "dev": "vite --port 3000".

Gotchas

Recall

Can you answer these?

0/4

Answer out loud (or in your head) before you tap. That's what makes it stick.

Saved on this device only. No login.