Next.js on App Platform API routes 404

How do we get a Next.js app to work on App Platform? All my routes to /api are returning 404. The only way I’ve been able to get routing working with Next.js and App Platform has been with getStaticPaths and have it build the routes on npm run export – but you can’t use the getStaticPaths trick with the /api route since it server-side renders at runtime.

Any suggestions, please??

Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
May 23, 2022

Hi @dladendorf, by default the /api prefix is dropped from request paths before being forwarding to your service/static site. If you would like Next.js to see the /api prefix, you can set the preserve_path_prefix option:

  1. Navigate to your app’s settings page
  2. Scroll down to the App Spec section
  3. Download the app spec and open it in a text editor
  4. Find the route definition and set preserve_path_prefix: true:
      - path: /api
        preserve_path_prefix: true
  5. Save the file and upload it using the control panel

Hi @dladendorf,

It may be that you added getStaticPaths without getStaticProps. Could you show me how you have set the routes?

Additionally I have found you a good tutorial Deploy a Next.js App to App Platform that I think can help you.

Let me know how this goes. I’d be happy to continue help you.

Sergio Turpín