Error when exporting revalidate on an API route
Unanswered
Asian black bear posted this in #help-forum
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:
But when I build the project I get this error:
export const revalidate = 60;
But when I build the project I get this error:
Next.js can't recognize the exportedconfig
field in route "/api/test/route":
Unsupported node type "BinaryExpression" at "revalidate".
34 Replies
@Asian black bear 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".
export const revalidate = 60;
this is not for api route
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
@Asian black bear Is there a way to have an API route cached, instead of the data within it?
show me your current codebase if it's okay
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
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
well as I said before, it's not possible to revalidate an entire route
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"
@Asian black bear 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"
it's only for page - as it's static regeneration (static page generation indeed)
if you are using app router, you can also do this
res.setHeader('Cache-Control', 's-maxage=60, stale-while-revalidate')
Asian black bearOP
yeah I'll probly end up using that, thanks. But the docs seem wrong:
They specify that Page, Layout or route handler support these
well, previously GET route handler was cached by default but that has been changed at some point
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
Asian black bearOP
If my route exports revalidate like in the docs, it fails with an error
Docs can't be stable indeed
Nextjs moves fast
Asian black bearOP
Yeah I just need clarification if this is a bug in the docs or in NextJS
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 ...
Asian black bearOP
Yeah exactly
Thanks for the help, much appreciated!
yr welcome
@Asian black bear Thanks for the help, much appreciated!
mark solution to close this thread!