Next.js Discord

Discord Forum

How to turn off static page generation for api routes in app router

Answered
Allegheny mound ant posted this in #help-forum
Open in Discord
Allegheny mound antOP
Hey, I've got an api endpoint in the file src/app/api/events/route.ts that returns an SSE stream. When I run yarn dev, everything works as expected. However, when I run yarn build, it seems that nextjs is trying to compile this api endpoint into a static page, which naturally times out because the event stream returned from api/events doesn't terminate. Here is the error:

 ⚠ Sending SIGTERM signal to static worker due to timeout of 60 seconds. Subsequent errors may be a result of the worker exiting.
 ⚠ Restarted static page generation for /api/events because it took more than 60 seconds
 ⚠ See more info here https://nextjs.org/docs/messages/static-page-generation-timeout
 ⚠ Sending SIGTERM signal to static worker due to timeout of 60 seconds. Subsequent errors may be a result of the worker exiting.
 ⚠ Restarted static page generation for /api/events because it took more than 60 seconds
 ⚠ Sending SIGTERM signal to static worker due to timeout of 60 seconds. Subsequent errors may be a result of the worker exiting.

> Build error occurred
Error: Static page generation for /api/events is still timing out after 3 attempts. See more info here https://nextjs.org/docs/messages/static-page-generation-timeout
    at onRestart (/home/alex/Documents/Git/me/offline-search/visualizer/node_modules/next/dist/build/index.js:293:27)
    at /home/alex/Documents/Git/me/offline-search/visualizer/node_modules/next/dist/lib/worker.js:95:40


I'm relatively new to nextjs, but I'm not sure why api routes are even a candidate for static site generation? How do I turn off static generation for these endpoints?

(using nextjs 14.2.5)
Answered by joulev
Add export const revalidate = 0 to that route.ts file
View full answer

4 Replies

@Allegheny mound ant Hey, I've got an api endpoint in the file `src/app/api/events/route.ts` that returns an SSE stream. When I run `yarn dev`, everything works as expected. However, when I run `yarn build`, it seems that nextjs is trying to compile this api endpoint into a static page, which naturally times out because the event stream returned from `api/events` doesn't terminate. Here is the error: ⚠ Sending SIGTERM signal to static worker due to timeout of 60 seconds. Subsequent errors may be a result of the worker exiting. ⚠ Restarted static page generation for /api/events because it took more than 60 seconds ⚠ See more info here https://nextjs.org/docs/messages/static-page-generation-timeout ⚠ Sending SIGTERM signal to static worker due to timeout of 60 seconds. Subsequent errors may be a result of the worker exiting. ⚠ Restarted static page generation for /api/events because it took more than 60 seconds ⚠ Sending SIGTERM signal to static worker due to timeout of 60 seconds. Subsequent errors may be a result of the worker exiting. > Build error occurred Error: Static page generation for /api/events is still timing out after 3 attempts. See more info here https://nextjs.org/docs/messages/static-page-generation-timeout at onRestart (/home/alex/Documents/Git/me/offline-search/visualizer/node_modules/next/dist/build/index.js:293:27) at /home/alex/Documents/Git/me/offline-search/visualizer/node_modules/next/dist/lib/worker.js:95:40 I'm relatively new to nextjs, but I'm not sure why api routes are even a candidate for static site generation? How do I turn off static generation for these endpoints? (using nextjs 14.2.5)
Add export const revalidate = 0 to that route.ts file
Answer
@joulev Add export const revalidate = 0 to that route.ts file
Allegheny mound antOP
yep, that did it. Thanks! Though I still don't understand why api routes should be static to begin with...
normally endpoints will always do some logic
@Allegheny mound ant yep, that did it. Thanks! Though I still don't understand why api routes should be static to begin with...
Everything is static by default now.

You may need an api route that is only updated once a day for example.