Route handlers are running during build (cacheComponents) #85326
-
|
After I enabled |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
|
Hello. This is a common point of confusion, and you are correct: your That "runtime by default" behavior only applies to your UI (Pages and Layouts), not to your Route Handlers (route.js files). Your GET routes remain static by default, which is why you see them running at build time. To fix this and force your As the documentation for Route Segment Config states (https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic), you need to add this line to the top of your route.js file: export const dynamic = 'force-dynamic';
// Your GET function will now run at runtime
export async function GET(request) {
// ...
}This will solve the problem and ensure your routes are executed on request, not at build time. |
Beta Was this translation helpful? Give feedback.
-
|
You are right, i'm sorry. BTW, do you have the "use cache" directive on top of the file? If so, i guess the solution is just to remove it. |
Beta Was this translation helpful? Give feedback.
-
|
Since GET requests are eligible for pre- import { connection, type NextRequest } from 'next/server';
export async function GET(req: NextRequest) {
await connection();
// ...
}During the build step the GET function is still going to be called, so make sure you call |
Beta Was this translation helpful? Give feedback.

Since GET requests are eligible for pre-
renderingwhatever, you can easily opt out of that withconnection:During the build step the GET function is still going to be called, so make sure you call
await connectionearly. The best place to do it is right before introducing any side-effects (or doing dynamic IO) so the rest is still verified during build.