Understanding "API Routes in Static Export" Warning in Next.js
Why This Warning Occurred​
The "API Routes in Static Export" warning is typically raised when an exportPathMap path is matched to an API route while trying to statically export a Next.js application via the next export command. This command disables API routes as it is designed for a static-only setup.
Running next export is not necessary to make your application static. Pages in your application that do not have server-side data dependencies will be automatically statically optimized when you run next build. This includes pages powered by getStaticProps.
Possible Ways to Fix It​
To resolve this issue, you have two main options:
- Use the
next buildcommand instead ofnext exportif you're deploying your application on platforms that don't requirenext export. For example, Vercel is a popular hosting platform for Next.js applications that supports this feature. - If you still need to use
next export, make sure to remove any paths that use API routes from yourexportPathMapin yournext.config.jsfile. - Consider incrementally adopting the App Router, which supports Route Handlers. These "API Routes" can be used to create endpoints that can be statically exported in your application.
Useful Links​
- Static HTML export - Learn more about how you can create a static HTML export of your Next.js application.
- Route Handlers - Learn more about how you can use Route Handlers to create endpoints that can be statically exported in your application.