Next.js Discord

Discord Forum

Error when exporting revalidate on an API route

Unanswered
Asian black bear posted this in #help-forum
Open in Discord
Avatar
Asian black bearOP
I have an API route where I want the data to be revalidated every minute. So I export the following in the route.ts:
export const revalidate = 60;

But when I build the project I get this error:
Next.js can't recognize the exported config field in route "/api/test/route":
Unsupported node type "BinaryExpression" at "revalidate".

34 Replies

Avatar
Asian black bearOP
Is there a way to have an API route cached, instead of the data within it?
E.g. if the route is doing expensive calculations
Avatar
Asian black bearOP
I mean it's just a simple GET with no headers, cookies
but I don't want the function to be invoked, rather cached on a CDN level
unstable_cache?
Avatar
Asian black bearOP
I could, but it was nice when I could just specify revalidate for an entire route, like you can do for a page
Ideally the browser should just cache the response as well
Avatar
well as I said before, it's not possible to revalidate an entire route
Avatar
Asian black bearOP
Ok thanks, could you point me to the docs where those limitations are stated?
https://nextjs.org/docs/app/building-your-application/data-fetching/incremental-static-regeneration this page doesn't really specify if it's only for "page" or "route"
if you are using app router, you can also do this
res.setHeader('Cache-Control', 's-maxage=60, stale-while-revalidate')
Avatar
Asian black bearOP
yeah I'll probly end up using that, thanks. But the docs seem wrong:
Image
They specify that Page, Layout or route handler support these
Avatar
well, previously GET route handler was cached by default but that has been changed at some point
Avatar
Asian black bearOP
Yeah it was changed to default false
So based on the docs, it sounds like it's supported, but the build process fails
Avatar
Asian black bearOP
If my route exports revalidate like in the docs, it fails with an error
Avatar
Docs can't be stable indeed
Nextjs moves fast
Avatar
Asian black bearOP
Yeah I just need clarification if this is a bug in the docs or in NextJS
Avatar
am not pretty sure but I am mostly sure it's docs
I never expected that could be supported for the route handlers
where I can have POST and DELTE ...
Avatar
Asian black bearOP
Yeah exactly
Thanks for the help, much appreciated!
Avatar
yr welcome
Avatar
@Asian black bear Thanks for the help, much appreciated!
Avatar
mark solution to close this thread!