React / 08
The dev server, HMR and caching
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) | |
|---|---|---|
| For | You, while coding | Your users |
| Speed | Starts fast, updates on every save | Slower, runs once |
| Output | Served from memory, nothing written | Optimised files in dist/ |
| Code | Unminified, source maps, React warnings | Minified, 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
2 · dev server
Recompiles one file
3 · browser
Swaps the module
The watcher notices
A file watcher listens to the operating system's file-change events, so it knows the moment you save.Only that file is recompiled
Everything else comes from cache, which is why it takes milliseconds.The browser is told
The dev server keeps a WebSocket (a connection that stays open) to the page and sends "this module changed".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 itlocalhost:5173
Tasty Bites
Cart: 0
Running. Add some items, then save an edit.
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-cacheat 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 httpsIn 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.
