3

In my next.js website, I have an api endpoint at /pages/api/views/[slug].js, which fetches the amount of views from a database (supabase) of each of my blogs. The setup works fine in my development server, (npm run dev), but when I deploy the app to production, all the API calls to the /pages/api/views/[slug].js endpoint return 404, saying that there is no page there.

I have set env variables in vercel's dashboard.

Additionally, when I try calling the default /pages/api/hello endpoint, I also get a 404 error.

Production website: https://www.ganning.me/blog

GitHub Repo: https://github.com/ganning127/ganning.me

hello.js

// Next.js API route support: https://nextjs.org/docs/api-routes/introduction

export default function handler(req, res) {
  res.status(200).json({ name: 'John Doe' })
}
juliomalves
  • 42,130
  • 20
  • 150
  • 146
Ganning Xu
  • 263
  • 4
  • 13
  • Are you running `next export` on your build? – Shawn Yap Jan 05 '22 at 17:25
  • Your production website does not seem to resolve, the dev website works – Ramakay Jan 05 '22 at 17:27
  • @Ramakay hmm it works on my side (i'm able to access the production website), not sure why it doesn't work for you though – Ganning Xu Jan 05 '22 at 17:29
  • @ShawnYap `next build` errors out saying: `Only absolute URLs are supported`, when its Generating static pages (9/10). The error seems to come from my `blog.js` file? `at BlogPosts (/Users/ganningxu/programming/programming_web/ganning.me/.next/serverless/pages/blog.js:148:5)` – Ganning Xu Jan 05 '22 at 17:31
  • But i'm not running `next export`, do i need to? – Ganning Xu Jan 05 '22 at 17:39
  • 1
    https://stackoverflow.com/questions/44342226/next-js-error-only-absolute-urls-are-supported This post might help you with debugging. You shouldn't export if your site is not completely static but your build command shows `next build && next export` – Shawn Yap Jan 05 '22 at 17:43
  • Yeah, since I have blogs its not completely static, so I don't think I'll need to do that. I think its something with my `next.config.js` though since the API routes are not working – Ganning Xu Jan 05 '22 at 17:57
  • Did you solve the issue? The API routes https://www.ganning.me/api/hello & https://www.ganning.me/api/views/creating-a-hello-world-website work for me. – juliomalves Jan 07 '22 at 18:33
  • Yes! Sorry I forgot to reply to this thread, but I think the issue was something with the example API fetch, so once i removed that it worked fine :) – Ganning Xu Jan 08 '22 at 04:41
  • Faced Same problem and find [solution](https://stackoverflow.com/questions/73233293/next-js-bad-request-when-trying-any-request-in-production/74128966#74128966) – Devam Sanghvi Oct 20 '22 at 11:54

1 Answers1

0

/pages/api/hello

Please remove pages from url then try it will work.(/api/hello)

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 18 '23 at 09:54