I keep running into an error: cacheTag() can only be called inside a "use cache" function
Unanswered
European hornet posted this in #help-forum
European hornetOP
Here's my code:
export async function getCachedSession(userCacheToken: string, token: string) {
"use cache";
cacheTag(
logInfo("fetchedSessionFromDB");
const session = await fetchQuery(
api.queries.auth.index.getSessionUser,
{},
{ token },
);
return session;
}
I am clearly using "use cache" directive (and have set cacheComponents to true in next config file) but getting an error: cacheTag() can only be called inside a "use cache" function. What am I doing wrong?
export async function getCachedSession(userCacheToken: string, token: string) {
"use cache";
cacheTag(
user-session:${userCacheToken});logInfo("fetchedSessionFromDB");
const session = await fetchQuery(
api.queries.auth.index.getSessionUser,
{},
{ token },
);
return session;
}
I am clearly using "use cache" directive (and have set cacheComponents to true in next config file) but getting an error: cacheTag() can only be called inside a "use cache" function. What am I doing wrong?
2 Replies
@European hornet Here's my code:
export async function getCachedSession(userCacheToken: string, token: string) {
"use cache";
cacheTag(`user-session:${userCacheToken}`);
logInfo("fetchedSessionFromDB");
const session = await fetchQuery(
api.queries.auth.index.getSessionUser,
{},
{ token },
);
return session;
}
I am clearly using "use cache" directive (and have set cacheComponents to true in next config file) but getting an error: cacheTag() can only be called inside a "use cache" function. What am I doing wrong?
Transvaal lion
You might need to place the "use cache" directive before the definition of the getCachedSession function and not when you call cacheTag.
Also for the code to be readable and for the future wrap the code with
Also for the code to be readable and for the future wrap the code with
```ts and end it with ```So you get:
export async function getCachedSession(userCacheToken: string, token: string) {
"use cache";
cacheTag(`user-session:${userCacheToken}`);
logInfo("fetchedSessionFromDB");
const session = await fetchQuery(
api.queries.auth.index.getSessionUser,
{},
{ token },
);
return session;
}