What you are actually deploying
vite build produces static files. That is the whole artifact — no server, no runtime, no Node process.
| Target | Suitable for | Requires |
|---|---|---|
| Static host or CDN | SPAs, static sites | Cache headers and an SPA fallback rewrite |
| Node server | SSR, or an API on the same origin | A supervised long-running process |
| Container | Either, when you want one deployable unit | An image build and an orchestrator |
If your app is client-only, a static host is the right answer and running Node to serve files is cost and attack surface for nothing. vite preview is not that server — it is a local check of the built output, not a production server.
Cache headers
This follows directly from content hashing, and getting it wrong is the most common deploy failure in this whole course:
| Path | Cache-Control |
|---|---|
/assets/* (hashed) | public, max-age=31536000, immutable |
/index.html | no-cache |
public/ files with fixed names | Short max-age, e.g. max-age=300 |
index.html must never be cached long-term. It is the file that names the current hashes; a stale copy points at chunks that no longer exist, and the visitor gets a blank page a reload cannot fix. Files in public/ are unhashed, so they need a short lifetime too — that is the price of a predictable name.
# nginx sketch
location /assets/ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location = /index.html {
add_header Cache-Control "no-cache";
}
Verify with devtools or curl -I against the deployed URL after the first deploy. Host defaults are frequently wrong in exactly this way, and the failure only appears on the second deploy — by which time nobody is looking at headers.
SPA fallback
Client-side routing means /settings/billing has no file behind it. The server must return index.html for unmatched routes:
# netlify.toml style
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
Status 200, not 301 — you are serving the app, not redirecting. Scope the rewrite so it does not swallow /assets/* or your API path; a catch-all that returns HTML for a missing JS file turns a clear 404 into a syntax error in the console. And if you deploy under a sub-path, base and your router's basename both need setting, as covered in module 3.
Build-time versus runtime configuration
VITE_ variables are inlined as literals during the build. Consequences:
- The value is public. Anything secret in a
VITE_variable is published; if it was ever deployed, rotate it. - Changing a value requires a rebuild. You cannot set an environment variable on the host and restart, the way you would with a server process. The string is already inside the JavaScript.
- One artifact cannot serve two environments. Building for staging and promoting that same bundle to production ships the staging API URL.
Two workable models:
| Model | How | Trade-off |
|---|---|---|
| Build per environment | vite build --mode staging, one artifact per target | Simple; what you test is not byte-identical to what ships |
| Runtime config fetch | Build once; app fetches /config.json at startup | One artifact promoted through environments; adds a request before boot |
For the second, serve config.json from public/ with a short cache lifetime and read it before rendering. Never put secrets in it — it is as public as the bundle.
Whichever you choose, do the environment-variable audit from module 2 before the first production deploy. Grep for VITE_ and ask of each one whether you would paste the value into a public issue.
Source maps
build: { sourcemap: true }
Source maps turn a minified stack trace into readable code. The question is not whether to generate them but whether to serve them.
| Approach | Result |
|---|---|
Generate, upload to the error tracker, do not deploy the .map files | Readable traces for you, source not public |
| Generate and deploy publicly | Anyone can read your source, including comments and internal names |
| Do not generate | Production errors are near-unreadable |
The first is the default recommendation. Note the caveat: everything in a client bundle is already public in minified form, so a source map is a readability difference, not a secrecy boundary. Do not use "the map is private" as a reason to leave a comment or a stray credential in client code.
If you do publish maps, make sure the build does not include commented-out experiments, internal endpoint lists, or TODOs naming unshipped security work.
Hardening the response
A static bundle still has server-side security decisions:
- HTTPS only, with HSTS. Anything less lets a network attacker rewrite your JavaScript in transit.
- A Content-Security-Policy. This is the highest-value header for a client-rendered app because it limits what an injected script can do. Expect to iterate: a strict policy usually breaks inline styles and third-party embeds on the first attempt, so develop it in report-only mode before enforcing.
X-Content-Type-Options: nosniffso a mistyped content type is not reinterpreted as script.Referrer-Policy, typicallystrict-origin-when-cross-origin, so full URLs with path data do not leak to third parties.- Check what the build directory contains. Source maps you did not intend to ship,
.envfiles copied in by a broken DockerCOPY, and stray fixture data all become public URLs. Files inpublic/are served verbatim — treat that directory as a publication decision.
Pre-launch checklist
[ ] vite build and vite preview pass; every route clicked through
[ ] tsc --noEmit clean, and running in CI
[ ] base set correctly, and the router basename matches
[ ] index.html served no-cache; hashed assets immutable
[ ] SPA fallback returns index.html with status 200, scoped away from assets
[ ] every VITE_ variable reviewed and safe to publish
[ ] no secrets in the bundle; anything previously exposed rotated
[ ] source maps uploaded to the error tracker, not served publicly
[ ] HTTPS with HSTS; CSP, nosniff and Referrer-Policy set
[ ] error tracking receiving events from a real deployed build
[ ] dist inspected for files you did not mean to publish
The two most common launch failures on this list are a wrong base producing a blank page, and a long-cached index.html breaking returning visitors on the second deploy. Both are configuration, both are five-minute fixes, and both are invisible until users hit them.
Exercise: Your team wants one build artifact promoted from staging to production, but the API URL differs between them and the app currently reads import.meta.env.VITE_API_URL. Explain why promoting the artifact cannot work as written, and describe the change that makes it possible.