Next.js App Router pages that fetch from an internal/backend API fail (or hang) at `next build` time inside a Docker image build, when that API isn't reachable from the build context/network.
Fix: Mark the affected route with `export const dynamic = 'force-dynamic'` so Next.js skips static generation for it and defers all data fetching to request time instead of build time. Required whenever a page's data source is a separate service that only exists at runtime (e.g. a backend container not present during a multi-stage Docker build) — without it, `next build` throws `fetch failed` and the build fails outright. Tradeoff: the route loses true build-time SSG, but a tagged `fetch` call (`next: { revalidate, tags }`) still gets cached at request time via Next's Data Cache, giving effectively the same freshness/caching behavior as ISR without needing build-time data access.
nextjsapp-routerdockerbuild-timessg
References
- https://nextjs.org/docs/messages/app-static-to-dynamic-error — The dynamic export lets you force a page to be dynamic — export const dynamic = 'force-dynamic' — so it won't attempt to be statically generated.
- https://github.com/vercel/next.js/discussions/59088 — Root cause of the build-time 'fetch failed' error is that the backend/API is unreachable during the build process; adding export const dynamic = 'force-dynamic' to affected pages resolves the build failure by deferring fetches to request time.