Next.js Discord

Discord Forum

cacheLife() can only be called inside a "use cache" function

Unanswered
Brown Noddy posted this in #help-forum
Open in Discord
Brown NoddyOP
I've made an async function have "use cache" at the top, like this:
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 on

However, 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 (/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
interesting, i havent dwelled to much with multi-tenant app about whats the ideal way to architecture the project.
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 proxy.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?
Brown NoddyOP
It seems I was still importing it in proxy.ts. If I remove the import, the issue disappears
Brown NoddyOP
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