Skip to content

Deploy Vite to the edge

A Vite app is built once and served as static files from Cloudflare's network. React, Vue, Svelte, Solid, Preact and plain Vite projects all deploy the same way.

Detection

A project is treated as Vite when vite is in dependencies or devDependencies, and either:

  • its build script runs vite build ("build": "vite build", or "tsc && vite build" as in create-vite's TypeScript templates);
  • or a vite.config.ts, .js, .mjs or .cjs file sits at its root.

A project with vite but neither fails to build: no supported framework is found. Add an empty config to opt in:

vite.config.ts
import { defineConfig } from 'vite';
 
export default defineConfig({});

Defaults

SettingDefault
Install commandFrom your lockfile, see Frameworks
Build commandnpx vite build
Output directorydist

If your config sets build.outDir, set the same output directory on the project.

Environment variables

Vite inlines import.meta.env.VITE_* values into your bundle at build time. Add them as build variables: in the dashboard, a new variable on a Vite project is a build variable by default.

src/api.ts
const response = await fetch(`${import.meta.env.VITE_API_URL}/products`);

Only VITE_-prefixed variables reach your code (unless you change Vite's envPrefix); the others are readable from vite.config.ts through process.env. Whatever ends up in the bundle is downloaded by every visitor, which is why a secret can't be a build variable.

For one deployment only, pass them to the CLI:

Terminal
hiraiship deploy --build-env VITE_API_URL=https://staging-api.example.com

Client-side routing

Client-side routers (React Router, Vue Router, TanStack Router…) work as they are. A request for a path with no matching file, like opening or reloading /settings, gets your index.html, and the router takes it from there.

This holds as long as your output has no 404.html at its root. With one, unknown paths serve that page with a 404 status instead: right for a multi-page site, wrong for a single-page app. See Static sites.