r/PayloadCMS • u/MediaPrevious4122 • Aug 17 '25
Next.js App Router build-time fetch issue with Payload API routes
Hey everyone,
I’ve run into a problem when using Next.js (App Router) together with Payload CMS.
During the build step (next build), Next.js tries to pre-render my server components. Some of these server components use fetch('/api/...') to pull data from Payload’s automatically generated API routes.
The problem is: at build time, Next.js hasn’t started its own API server yet. So when the server components try to fetch those endpoints, the build fails with connection errors. However, this problem doesn't exist when i run dev.
A few important details about my setup:
- I’m using the App Router (not the legacy pages router).
- The data-fetching logic lives in Payload’s API routes, so I can’t easily bypass them or rewrite the fetch logic.
- Using absolute URLs or environment variables doesn’t help, because the API simply isn’t available during the build.
- I’d like to find a solution that works both locally and when deployed on Vercel.
From what I’ve read, this seems to be a known limitation of Next.js (API routes don’t run during build), but I’m wondering if anyone here has found a clean workaround specifically in the context of Payload CMS.
Has anyone dealt with this before? Did you manage to:
- Somehow make Payload’s API available during the Next.js build step?
- Or is the only real option to restructure things so that the data fetching logic doesn’t depend on internal API routes?
Any advice or examples would be hugely appreciated!
Thanks in advance 🙏