How to Deploy a React App on Vercel (Fix 'Page Not Found' Error)

AI Summary
Tap Generate and let AI do the magic
π How to Deploy a React App/Website on Vercel (Fix "Page Not Found" Error)
If youβre deploying your React app on Vercel and getting a frustrating "Page Not Found" error on refresh or navigating to routes like /about or /dashboard, youβre not alone.
This guide will walk you through how to correctly deploy a React app to Vercel β and fix the routing issue that plagues many single-page applications (SPAs) by adding a vercel.json file.
β Step 1: Build Your React App
First, make sure your app is ready to be deployed.
If you used Vite:
cmd1npm run build
This creates a build/ folder with static assets.
β Step 2: Push Your Code to GitHub
- Create a GitHub repository.
- Push your React code to GitHub.
git1git init 2git remote add origin https://github.com/your-username/your-repo.git 3git add . 4git commit -m "Initial commit" 5git push -u origin main 6
β Step 3: Create a Vercel Account and Import Project
- Go to vercel.com.
- Sign up with GitHub and import your project.
- When asked for a framework, choose Other (not React) if it's a pure static React app (like CRA). Otherwise, if you're using Next.js or Remix, choose that.
Vercel automatically detects the build command and output folder (build).
β Common Issue: "404 Page Not Found" on Route Refresh
After deployment, when you go to:
url1yourdomain.vercel.app/about
You might see a 404 Page Not Found.
π§ Why?
React apps (built with Vite or similar) are Single Page Applications (SPA). They rely on client-side routing. But Vercel is a static hosting provider. So if you access /about directly, Vercel looks for a static file about/index.html, which doesnβt exist β hence the 404.
π οΈ Solution: Add vercel.json
You need to rewrite all routes to index.html so React can handle them.
π Create a file named vercel.json in the root of your project:
js1{ 2 "rewrites": [ 3 { "source": "/(.*)", "destination": "/index.html" } 4 ] 5} 6
This tells Vercel:
"No matter what the route is, serve
index.htmland let React Router take care of the rest."
β Step 4: Re-deploy Your App
Once you add vercel.json, push your changes to GitHub:
git1git add vercel.json 2git commit -m "Fix Vercel routing with vercel.json" 3git push 4
Vercel will automatically re-deploy the latest commit. Now test your app at:
url1your-app.vercel.app 2
Try refreshing on /about or /dashboard β it should work!
π§ͺ Bonus: Custom Domains & HTTPS
- You can add a custom domain in the Vercel dashboard.
- Vercel gives you free HTTPS with automatic renewals.
π¦ Full Folder Structure (Example)
md1my-app/ 2βββ public/ 3βββ src/ 4βββ vercel.json β 5βββ package.json 6βββ ... 7
β Recap
| Step | Description |
|---|---|
| 1. | Build the React app using npm run build |
| 2. | Push to GitHub |
| 3. | Import into Vercel |
| 4. | Add vercel.json to handle routes |
| 5. | Re-deploy and test |
π§ Pro Tip
If you're using React Router v6, make sure you also use:
js1<BrowserRouter basename="/" /> 2
And make sure your routes donβt rely on server-side logic.
π¬ Final Words
Vercel is incredibly fast and developer-friendly, but React SPAs require a small tweak (vercel.json) to work properly. Now you know how to fix it!