TExcelTAD
Home › Vite › Vite Base Path & Deploying

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)
js
// 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:

js · try it yourself
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 its dist/ 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/vite would look for ./assets under /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:

js
// 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

yaml
# .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@v4

Deploying with a CLI

Many hosts have a CLI that uploads dist/ directly:

bash · try it yourself
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.

html · try it yourself
<!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/... means base (or the host's folder) is wrong.

Test yourself

Your site lives at https://ana.github.io/notes/. What should base be?

  1. '/'
  2. '/notes/'
  3. 'notes'
  4. '/ana/notes/'

Which file must NOT be cached long-term?

  1. /assets/index-C3k9pQ2x.js
  2. /assets/logo-Bq3y.png
  3. /index.html
  4. /assets/index-DiwrgTda.css

How does your code read the configured base path?

  1. process.env.BASE
  2. import.meta.env.BASE_URL
  3. window.base
  4. document.baseURI only