cacheLife() can only be called inside a "use cache" function
Unanswered
Brown Noddy posted this in #help-forum
Brown NoddyOP
I've made an async function have "use cache" at the top, like this:
I'm running the
However, when doing so I get this error:
What's going on? The function is definitely using "use cache"
export async function getConfig(domain: string) {
"use cache"
cacheLife("hours")
const res = await fetch(...)
return await res.json()
}I'm running the
getConfig function in the new proxy.ts in order to configure the page depending on what domain the site is being served onHowever, when doing so I get this error:
`cacheLife()` can only be called inside a "use cache" function.What's going on? The function is definitely using "use cache"
18 Replies
Brown NoddyOP
And the data isn't being cached either (running local dev). When I refresh the page, fresh data is being fetched every time
might want to elevate this to github discussion or issues
@alfonsüs ardani i dont think you can use "use cache" in proxy.ts...
Brown NoddyOP
Oh, that’s unexpected
@Brown Noddy Oh, that’s unexpected
proxy/middleware are supposed to be lightweight. would be preferable if it doesn't do external fetch at all
this is the big reason why they renamed it to proxy so that people dont misuse it as middleware
@alfonsüs ardani proxy/middleware are supposed to be lightweight. would be preferable if it doesn't do external fetch at all
Brown NoddyOP
Yeah, that's fair. In this case it's just to fetch what config to use for a multi-tenant application. After running once, it can be cached for days/weeks, so it shouldn't have to run very often
Would the solution be to just do the redirect to the subdomain pages (
Would the solution be to just do the redirect to the subdomain pages (
/s/[subdomain], similar to the example multi-tenant app provided by Vercel) and do the loading over there?>In this case it's just to fetch what config to use for a multi-tenant application. After running once, it can be cached for days/weeks, so it shouldn't have to run very often
can always do that in normal layout/page/route and "use cache" it with cacheLife of "days" or "weeks" so it wont have to run very often
can always do that in normal layout/page/route and "use cache" it with cacheLife of "days" or "weeks" so it wont have to run very often
interesting, i havent dwelled to much with multi-tenant app about whats the ideal way to architecture the project.
@alfonsüs ardani >In this case it's just to fetch what config to use for a multi-tenant application. After running once, it can be cached for days/weeks, so it shouldn't have to run very often
can always do that in normal layout/page/route and "use cache" it with cacheLife of "days" or "weeks" so it wont have to run very often
Brown NoddyOP
Seems quite reasonable. I'll ask on GitHub if anyone has run into a similar situation
allright! let us know if it is a bug or intended to be that way
@alfonsüs ardani allright! let us know if it is a bug or intended to be that way
Brown NoddyOP
I've tried to create a minimal reproduction of this, and it seems that even having a file like this in the project will produce the error, even if I don't call it from
Maybe I'm misunderstanding how
is supposed to be used with functions?
proxy.tsimport { cacheLife } from "next/cache"
export async function getConfig() {
"use cache"
cacheLife("weeks")
}Maybe I'm misunderstanding how
"use cache"is supposed to be used with functions?
Brown NoddyOP
It seems I was still importing it in
proxy.ts. If I remove the import, the issue disappearsBrown NoddyOP
Opened an issue about it now: https://github.com/vercel/next.js/issues/85475
@Brown Noddy I've tried to create a minimal reproduction of this, and it seems that even having a file like this in the project will produce the error, even if I don't call it from `proxy.ts`
ts
import { cacheLife } from "next/cache"
export async function getConfig() {
"use cache"
cacheLife("weeks")
}
Maybe I'm misunderstanding how `"use cache"`
is supposed to be used with functions?
yeah its supposed to be used with function, components, and top-level route file.
its just that im not sure if they want to add Cache API into middleware/proxy
i really doubt that they'd add it but you could try
the issue is because you wanted to use cached function in middleware which supposedly isn't its intended usage