Error "generateStaticParams()" so it cannot be used with "output: export" config. #56731
Replies: 4 comments 8 replies
-
|
Right, so if I make the assumption that you are using
To use Route handlers, you need a Node.js runtime running the Next.js Server (with next start). This is why the compiler wants you to define |
Beta Was this translation helpful? Give feedback.
-
|
TL;DR: The error fires when I hit this on Next.js 16.2.4 with What actually happens: Minimal fix — always return at least one param: export async function generateStaticParams() {
try {
const res = await fetch(`${process.env.API_URL}/posts`);
if (!res.ok) return [{ slug: "placeholder" }];
const posts: { slug: string }[] = await res.json();
if (posts.length === 0) return [{ slug: "placeholder" }];
return posts.map((p) => ({ slug: p.slug }));
} catch {
return [{ slug: "placeholder" }];
}
}The page component handles unknown slugs with Suggestion: The error message should differentiate between "function not exported" vs "function returned empty array." Something like: |
Beta Was this translation helpful? Give feedback.
-
|
This error is a fundamental incompatibility between Root Cause
Next Auth uses a catch-all route handler at Solution 1: Remove/Exclude the API Routes from the ExportIf your API routes (like next-auth) live on a separate server or you're handling auth differently in production, exclude them from the build: // next.config.ts
const nextConfig = {
output: 'export',
}Then delete or move Solution 2: Use a Separate Server for Auth (Most Common Pattern)For a fully static site that still needs authentication, handle auth at a different layer:
// middleware.ts
import { auth } from 'next-auth'
export default auth((req) => {
if (!req.auth && req.nextUrl.pathname !== '/login') {
return Response.redirect(new URL('/login', req.url))
}
})
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
}
Solution 3: Don't Use
|
Beta Was this translation helpful? Give feedback.
-
|
If you use a dynamic route like: app/blog/[slug]/page.tsx`
Next.js must know all possible slug values at build time. You can fix it by adding generateStaticParams():
`export async function generateStaticParams() {
const posts = await getPosts()
return posts.map((post) => ({
slug: post.slug,
}))
}`
Example:
`return [
{ slug: "post-1" },
{ slug: "post-2" },
{ slug: "post-3" },
]`
But if your route depends on data that is only available at runtime, then output: "export" is not suitable.
In that case, remove this from next.config.js:
`output: "export"`
and deploy using a Next.js server runtime such as Vercel, Node.js, Docker, or AWS EC2.
Summary:
Use output: "export" only when all dynamic params are known at build time.
Use generateStaticParams() for dynamic routes.
Remove output: "export" if you need runtime dynamic routes.
Static export cannot generate unknown dynamic routes at request time. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I can't build my project, because it gives the following error:
Page "/api/auth/[...nextauth]" is missing "generateStaticParams()" so it cannot be used with "output: export" config.Even using
use clientorexport const dynamic = 'force-static'in route.ts, my build still gives this errorI checked other discussions, but none of them solved my problem. If anyone knows what to do, I would be grateful.
versions used were:
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions