All posts

Blog

How to Host a React or Vue App: Static Hosting, Routing, and Deployment

Illustrated cover for the Developers guide "How to Host a React or Vue App: Static Hosting, Routing, and Deployment"

You have built something with React or Vue. It runs beautifully on localhost:5173, hot reloading every change. Now you want it on a real domain, and suddenly the questions start. Do you need a Node.js server? What do you upload? Why does every page except the home page return a 404 when you refresh?

The good news is that most React and Vue apps are easier to host than they look. This guide explains how to host a React or Vue app on regular web hosting or a VPS, and how to tell when your project needs something more.

The key idea: your app becomes static files

During development, tools like Vite run a dev server that compiles your code on the fly. That server is for development only. For production, you run a build, which compiles everything into plain HTML, CSS, JavaScript, and image files.

npm run build

With Vite, whether you use React or Vue, the output goes to a dist folder by default. Older Create React App projects output to build. Inside, you'll find an index.html and an assets folder of hashed files like index-a1b2c3.js.

Those files don't need Node.js to run. The browser downloads them and does all the work. That means any web server that can serve files, whether Apache, Nginx, or LiteSpeed, can host your single-page application (SPA).

When you do need a server

Static hosting covers classic SPAs. You need more if your project uses:

  • Server-side rendering (SSR) frameworks such as Next.js (React) or Nuxt (Vue) with features that render pages on each request. Many of these can also export a fully static site; check your framework's static export option.
  • Your own backend API written in Node.js, Python, PHP, or another language. The front end can still be static; the API runs separately, either on the same server or elsewhere.

If it's a pure client-side app talking to an external API, static hosting is all you need.

Step 1: Configure and build

Set the base path

If your app will live at the root of a domain (https://app.example.com/), the default settings are fine. If it will live in a subfolder (https://example.com/dashboard/), tell the build tool:

  • Vite: set base: '/dashboard/' in vite.config.js.
  • Create React App: set "homepage" in package.json.
  • Vue Router / React Router: configure the router's base or basename to match.

Skipping this is the usual cause of a blank white page with 404 errors for every JavaScript file.

Environment variables

Front-end build tools bake environment variables into the JavaScript at build time. In Vite, only variables prefixed with VITE_ are exposed to your code, for example VITE_API_URL. Set them in .env.production or in your build environment before running the build.

Remember: anything compiled into front-end code is visible to anyone who opens the browser's developer tools. Never put secret keys there. Secrets belong on a server.

Build

npm run build
npm run preview   # Vite: test the production build locally

Preview catches most path and configuration mistakes before you upload.

Step 2: Upload the files

Upload the contents of dist (not the folder itself, unless you want it as a subfolder) to your site's document root, typically public_html on shared hosting or something like /var/www/app on a VPS.

Options:

  • The control panel's File Manager: zip dist, upload the zip, and extract it.
  • SFTP with a client like FileZilla or WinSCP.
  • rsync over SSH for fast incremental updates: rsync -avz --delete dist/ user@server:/var/www/app/.
  • Git-based deployment, building in CI and pushing the output. See our guide on how to deploy a website with Git.

Load your domain, and the home page should appear.

Step 3: Fix the refresh 404 problem

Here's the issue nearly everyone hits. You click around your app and everything works. Then you refresh on /about or share a link to /products/42, and the server returns a 404.

That happens because client-side routing (React Router's browser router, Vue Router's history mode) changes the URL in the browser without asking the server. When you refresh, the browser asks the server for /about, a file that doesn't exist. The fix is to tell the server: "If a file isn't found, serve index.html and let the app handle the route."

Apache or LiteSpeed (.htaccess)

Create a .htaccess file in the same folder as index.html:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

If the app lives in a subfolder, change RewriteBase and the final path to match, for example /dashboard/index.html. Some build setups delete unknown files from dist on each build, so keep .htaccess in your project's public folder; Vite copies that folder into the output automatically.

Nginx

location / {
    try_files $uri $uri/ /index.html;
}

The alternative: hash mode

Both routers support hash-based URLs (example.com/#/about). They work on any server with no configuration, because the part after # never reaches the server. The trade-off is less clean URLs, so history mode with a rewrite rule is usually preferred.

Step 4: Caching and performance

Build tools add content hashes to file names, so index-a1b2c3.js changes name whenever its contents change. That lets you cache assets aggressively:

  • Cache files in /assets/ for a long time (for example, a year, marked immutable).
  • Keep index.html uncached or short-cached, so visitors pick up new builds right away.

Also make sure compression (gzip or Brotli) is enabled on your server, and serve the site over HTTPS.

Step 5: Connect to your API

If the front end calls an API on a different domain, that API must allow your site's origin with CORS headers. Alternatively, serve the API from the same domain under a path such as /api using a reverse proxy, which avoids CORS entirely.

Troubleshooting checklist

  • Blank page: open the browser console. 404s on JS files usually mean a wrong base path.
  • Refresh 404s: add the rewrite rule above.
  • Old version still showing: your index.html is being cached; adjust cache headers or clear your CDN.
  • API calls fail in production but not locally: check the production API URL variable and CORS settings.

Wrap-up

To host a React or Vue app, build it into static files, upload the contents of dist, set the right base path, and add a fallback rule so every route serves index.html. That works on shared hosting, a VPS, or a CDN. Only reach for a Node.js server when you need server-side rendering or your own backend.