{post.title}
{post.content}
Vercel 배포 후 404 에러가 발생할 때 원인을 분석하고 해결하는 방법을 다룹니다.
Vercel에 프로젝트를 배포한 후 404 에러가 발생하는 경우가 많습니다. 이번 포스트에서는 404 에러의 원인과 해결 방법을 살펴보겠습니다.
vercel --version
# 33.0.0
node --version
# v20.11.0
# 프로젝트 구조
my-app/
├── pages/
│ ├── index.js
│ └── api/
│ └── users.js
└── vercel.json배포 후 특정 경로에서 404 에러가 발생했습니다:
# 브라우저 에러
404: NOT_FOUND
Code: NOT_FOUND
# API 응답
{
"error": "The deployment could not be found on Vercel",
"code": "DEPLOYMENT_NOT_FOUND"
}| 유형 | 설명 | 해결책 |
|---|---|---|
| 페이지 404 | 라우트 미정의 | 페이지 생성 |
| API 404 | API 미구현 | API 라우트 생성 |
| 정적 파일 404 | 파일 누락 | 파일 배포 |
| 설정 404 | 라우팅 설정 오류 | vercel.json 수정 |
// vercel.json
{
"rewrites": [
{
"source": "/old-path",
"destination": "/new-path"
},
{
"source": "/api/:path*",
"destination": "/api/:path*"
}
],
"redirects": [
{
"source": "/legacy",
"destination": "/modern",
"permanent": true
}
]
}// pages/blog/[slug].js
export async function getStaticPaths() {
const posts = await fetchPosts();
const paths = posts.map((post) => ({
params: { slug: post.slug },
}));
return { paths, fallback: false };
}
export async function getStaticProps({ params }) {
const post = await fetchPost(params.slug);
return {
props: { post },
};
}
export default function BlogPost({ post }) {
return (
{post.title}
{post.content}
);
}// pages/api/users/[id].js
export default function handler(req, res) {
const { id } = req.query;
const user = users.find(u => u.id === id);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
res.status(200).json(user);
}// next.config.js
const nextConfig = {
output: 'standalone',
async rewrites() {
return [
{
source: '/assets/:path*',
destination: '/public/assets/:path*',
},
];
},
};
module.exports = nextConfig;// pages/404.js
export default function Custom404() {
return (
);
}# 로컬에서 빌드 테스트
$ npm run build
# 빌드 결과 확인
$ ls .next/server/pages
# Vercel CLI로 배포 테스트
$ vercel --prodThis blog does not accept any external sponsorships, affiliate marketing, or ad revenue.