lochana.Back to portfolio
Vercel

Fix a 404 when you refresh a page on Vercel

Refreshing /dashboard asks Vercel for a file that does not exist. Add a rewrite to index.html in vercel.json so your app handles every path.

  • Beginner
  • ~6 min
  • 5 steps
  • Runs in your browser

What you’ll practise

0/4
  • See that clicking a link and refreshing are different requests.
  • Find why Vercel answers 404.
  • Fix it in the host’s config, not the app.
  • Apply the same fix on another host.

Before you start

  • You’ve deployed a site to Vercel
  • You know what a route like /dashboard is in a React app
HANDS-ON WORKSPACE

Simulated Vercel

Guided mode
STEP 01 / 05

Click a link, then refresh.

Your goal: make links to /dashboard work. A teammate says the dashboard link in an email shows a 404. Click Visit Production, open Dashboard from the app’s menu, then click Refresh.

Step 1 of 5 · Reproduce the 404 SIMULATION
acmeacme-dashboardHobby
Production Deployment Ready
Domain
acme-dashboard.sandbox.example
Source
main · c7d8e9f Add dashboard and settings pages
Deployment
dpl_3a20
Learn at your own pace.

TAKE IT INTO THE REAL WORLD

How this works in real projects

The ideas behind this lesson, with the official documentation to read next.

01

Clicks stay in the browser; refreshes reach the server

React Router changes the page without asking the server. A refresh, or a link pasted into the address bar, asks the host for /dashboard, and the build has no file with that name. The dev server hides this because it serves index.html for every path.

02

Serve index.html for every route

Add { "rewrites": [{ "source": "/(.*)", "destination": "/index.html" }] } to vercel.json. Vercel serves real files first, so /assets/app.js still loads, and every other path gets the app.

Vercel: Vite on Vercelvercel.com
03

Same idea on every host

Netlify: /* /index.html 200 in a _redirects file. nginx: try_files $uri /index.html;. Switching to a hash router also avoids the request, but changes every URL to /#/….

Vercel: vercel.json rewritesvercel.com

KEEP PRACTISING

Try another lesson

All lessons
Docker DesktopBeginner

Containers & Docker

Fix an app container that can't reach its database

Your app container restarts with ECONNREFUSED 127.0.0.1:5432 while Postgres runs. Inside a container, localhost is that container: connect to the db service on 5432.

~7 minStart lesson
PostgreSQLIntermediate

Databases

Fix a slow PostgreSQL query with a composite index

Start a PostgreSQL B-tree index with the equality filter, then add the sort column. For `WHERE customer_id = … ORDER BY created_at DESC`, use `(customer_id, created_at)`.

~8 minStart lesson