Common vercel.json Settings
🎯 Introduction
In this article, you will learn to use vercel.json in the project root, fix the classic single-page application (SPA) issue where refreshing a history-mode route returns 404, and configure redirects and static asset caching.
🧱 What Is vercel.json?
So far, we have configured everything through the dashboard, where settings are not available in your local checkout or a teammate's copy of the repository. vercel.json lets you store configuration in your code repository. Place it in the project root to track it with Git and apply it automatically on each deployment.
{
"rewrites": [
{ "source": "/(.*)", "destination": "/index.html" }
]
}
The file uses standard JSON. Three common fields are rewrites, redirects, and headers. We will cover each below.
🪤 A Common Pitfall: SPA Refreshes Return 404
With Vue Router in history mode, the homepage loads and navigating to /about works, but refreshing /about with F5 returns 404.
The reason is straightforward: /about is a client-side route, and there is no corresponding about.html file on the server. On refresh, the browser requests /about directly from the server, which cannot find it and returns 404.
The rewrites configuration above fixes this: return index.html for any requested path, then let Vue Router handle routing in the browser.
{
"rewrites": [
{ "source": "/(.*)", "destination": "/index.html" }
]
}
source: "/(.*)": Matches all paths.destination: "/index.html": Returns the entry file for those paths.
Commit and push the change. Refreshing a route will no longer return 404.
⚡ Rewrites vs. Redirects
The names are similar, but their behavior differs:
| Comparison | rewrites | redirects |
|---|---|---|
| Address bar | Unchanged; transparent to the visitor | Changes to the new URL |
| Server behavior | Serves a different resource internally | Returns 301/308 so the browser makes another request |
| Typical use | SPA routing fallback | Migrating old URLs |
For example, use redirects to permanently redirect /old-blog to /blog:
{
"redirects": [
{ "source": "/old-blog", "destination": "/blog", "permanent": true }
]
}
permanent: true returns a 308 permanent redirect, allowing search engines to transfer ranking signals from the old URL to the new one.
📦 Configure Caching with headers
Built JavaScript and CSS filenames contain content hashes, such as app-a1b2c3.js. When the content changes, so does the filename, making these files suitable for long-lived caching:
{
"headers": [
{
"source": "/assets/(.*)",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
}
]
}
Browsers can cache these static assets for a year, making repeat visits much faster. immutable indicates that the file's content will not change at that URL, which works well with content-hashed filenames.
vercel.json as needed. Omit fields you do not use.❓ Knowledge Check
Q1: Do hash-mode routes need rewrites?
No. Hash-mode paths appear after #, as in /#/about, and are not sent to the server. Refreshing them therefore avoids this 404 issue.
Q2: Will rewrites affect /api requests?
Vercel matches existing files and serverless functions first. The /(.*) fallback applies when no resource matches, so /api/* endpoints are unaffected.
Q3: What if vercel.json conflicts with dashboard settings?
vercel.json takes precedence. Maintain each setting in one place, preferably in vercel.json so it is tracked with the repository.
Q4: When do vercel.json changes take effect?
Trigger a new deployment with a push or vercel --prod. The configuration is applied during deployment.
🧪 Practice
- Add Vue Router in history mode to
vercel-demoand create an/aboutpage. Deploy it and reproduce the refresh 404 error. - Add
vercel.jsonto fix it and configure long-lived cache headers for/assets/.
{
"rewrites": [
{ "source": "", "destination": "" }
]
}
🎉 You now know how to use vercel.json! In the final article, we will add a real backend endpoint to a frontend project with a serverless function.
