Search code examples
next.jsvercelcustom-error-pages

Next.js Deployed to Vercel returns a 404 not found


I'm trying to deploy a Next.js app to Vercel, it's a portfolio with only one page(index) and a couple of API routes. Vercel dashboard displays that the deployment is successful but when I visit the app it displays the layout component(the navbar and the footer but some styles are lost) with a 404 page like the attached imagehere. I tried to deploy with getStaticProps and with getServerSideProps and ended up with the same result. The API routes are working fine though both in SSG and SSR, I can retrieve the data from them weather from the browser or postman for example. I've been searching for a while now and this issue here is the most relative one to mine, but I tried all solution mentioned in it and non of them worked for me. I made sure that the framework preset is next.js and that the root directory is correct, the app isn't served through a sub directory. Also I'm using a variable for the domain name stored in the .env file, I tried to deploy and leave it as the default(localhost:3000) and tried to change it to on of the vercel auto assigned domain names and tried to add the variable from the project settings on vercel, and also tried to deploy without it at all and all the scenarios leaded to the same result(the 404 page and the APIs are working fine). This is the first time I deploy on vercel, I tried to contact them but I received no answers, I guess because I'm on a hobby tier. This is the project repo. The app works fine in development and also when I build and start the server locally.


Solution

  • The issue is that you're calling internal endpoints (e.g. functions on api folder) in getStaticProps. This is a Next.js anti-pattern (More about this on the docs). The reason being is that getStaticProps fetches the necessary data required to build the static page on build-time, but at this point of the process, the serverless functions contained on the api folder have not been deployed yet. You're getting a 404 page because you are using a try/catch block in getStaticProps, so when axios throws, catch block takes the control and returns notFound: true.