Vite Base Path & Deploying
A Vite build is a folder of static files, so it can be hosted almost anywhere: GitHub Pages, Netlify, Vercel, Cloudflare Pages, an S3 bucket behind a CDN, or your own nginx. The one setting you must get right is base — the public path where the site will live.
What base does
By default base is '/', so the build references files like /assets/index-C3k9pQ2x.js. That only works if the site is served from the root of a domain.
| Deployed at | base to use |
|---|---|
https://example.com/ |
'/' (default) |
https://user.github.io/my-repo/ |
'/my-repo/' |
https://cdn.example.com/static/app/ |
'https://cdn.example.com/static/app/' |
Unknown / any sub-path / file:// |
'./' (relative) |
// vite.config.js
import { defineConfig } from 'vite';
export default defineConfig({
base: '/my-repo/',
});Or on the command line: vite build --base=/my-repo/.
How URLs change
Vite rewrites every URL it controls — script and link tags, CSS url(), imported assets — and exposes the value as import.meta.env.BASE_URL for your own code. The playground shows the same asset under three configs:
const files = ['assets/index-C3k9pQ2x.js', 'assets/logo-Bq3y.png', 'favicon.svg'];
function urlsFor(base) {
return files.map((f) => base + f);
}
for (const base of ['/', '/my-repo/', './']) {
console.log(`base ${JSON.stringify(base)}:`);
for (const url of urlsFor(base)) console.log(' ' + url);
}base "/":
/assets/index-C3k9pQ2x.js
/assets/logo-Bq3y.png
/favicon.svg
base "/my-repo/":
/my-repo/assets/index-C3k9pQ2x.js
/my-repo/assets/logo-Bq3y.png
/my-repo/favicon.svg
base "./":
./assets/index-C3k9pQ2x.js
./assets/logo-Bq3y.png
./favicon.svg
Note: This course site itself uses
base: './'so itsdist/works from any sub-path. Relative base is convenient for simple sites, but it does not combine well with client-side routing on nested URLs (e.g./courses/vitewould look for./assetsunder/courses/). For SPAs with path-based routing, prefer an absolute base.
Routers need the base too
Your client-side router should know about the base, otherwise links go to the wrong place:
// React Router
createBrowserRouter(routes, { basename: import.meta.env.BASE_URL });
// Vue Router
createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes });SPA fallback on the host
Hosts serve files. A request for /courses/vite has no file, so the host must return index.html instead of a 404:
| Host | How |
|---|---|
| Netlify | public/_redirects containing /* /index.html 200 |
| Vercel | Detects Vite automatically; rewrites in vercel.json if needed |
| Cloudflare Pages | Serves index.html for unknown routes when there's no 404.html |
| GitHub Pages | No rewrites — use hash routing or copy index.html to 404.html |
| nginx | try_files $uri $uri/ /index.html; (see the Docker lesson) |
Deploying to GitHub Pages with Actions
# .github/workflows/deploy.yml
name: Deploy
on:
push:
branches: [main]
permissions:
contents: read
pages: write
id-token: write
jobs:
deploy:
runs-on: ubuntu-latest
environment:
name: github-pages
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run build -- --base=/${{ github.event.repository.name }}/
- uses: actions/upload-pages-artifact@v3
with:
path: dist
- uses: actions/deploy-pages@v4Deploying with a CLI
Many hosts have a CLI that uploads dist/ directly:
npm run build
npx netlify deploy --prod --dir=dist> my-app@0.0.0 build
> vite build
vite v8.3.4 building client environment for production...
✓ 5 modules transformed.
dist/index.html 0.46 kB │ gzip: 0.30 kB
dist/assets/index-DiwrgTda.css 1.24 kB │ gzip: 0.63 kB
dist/assets/index-C3k9pQ2x.js 3.10 kB │ gzip: 1.48 kB
✓ built in 140ms
Deploy path: /home/me/my-app/dist
Deploying to main site URL...
✔ Finished uploading 3 assets
✔ Deploy is live!
Production URL: https://my-vite-app.netlify.app
Caching headers
Files in assets/ have content hashes, so they can be cached forever. index.html must always be revalidated, otherwise users keep loading old hashed file names after a deploy.
| Path | Recommended Cache-Control |
|---|---|
/assets/* |
public, max-age=31536000, immutable |
/index.html |
no-cache |
Other public/ files |
Short, e.g. max-age=3600 |
Checking the base path visually
If the base is wrong, the HTML loads but its scripts don't — the classic "blank page". This page demonstrates: the first script uses a path that doesn't exist on this "server", the second one is correct.
<!doctype html>
<html>
<body style="font-family: system-ui">
<p id="wrong">❌ script with wrong base never ran</p>
<p id="right">…</p>
<script type="module" src="/my-repo/assets/index-C3k9pQ2x.js" onerror="document.querySelector('#wrong').textContent += ' (404 → blank page in real life)'"></script>
<script type="module">
document.querySelector('#right').textContent = '✅ inline/correct script ran';
</script>
</body>
</html>Tip: After every deploy, open DevTools → Network and filter by "JS". Any red 404 for
/assets/...meansbase(or the host's folder) is wrong.
Test yourself
Your site lives at https://ana.github.io/notes/. What should base be?
- '/'
- '/notes/'
- 'notes'
- '/ana/notes/'
Which file must NOT be cached long-term?
- /assets/index-C3k9pQ2x.js
- /assets/logo-Bq3y.png
- /index.html
- /assets/index-DiwrgTda.css
How does your code read the configured base path?
- process.env.BASE
- import.meta.env.BASE_URL
- window.base
- document.baseURI only