Next.js Discord

Discord Forum

unstable_cache revalidating cache based on dynamic parameter

Answered
Golden paper wasp posted this in #help-forum
Open in Discord
Golden paper waspOP
currently my code
const getAllCourses = nextCache(
  async (userId: string, search?: string) => {
    return await db.query....
  },
  [`get-all-courses`],
  { revalidate: 7200, tags: ["get-all-courses"] }
);

Firstly it identifies the dynamic param coz it is not sending same response to different user. So that's good thing

But when revalidating it revalidates for every user. So is there any way I could revalidate for particular user
like If I could pass userId to the key and tags
Answered by B33fb0n3
yes, you can do that by wrapping your nextCache function inside another function and the wrapper function takes the variables and pass them to the tags and keys. An code example would look like this:

const getAllCourses2 = async (userId: string, search?: string) => {
    return nextCache(
        async () => {
            return await db.query()
        },
        [`get-all-courses-${userId}`],
        { revalidate: 7200, tags: [`get-all-courses-${userId}`] }
    );
}
View full answer

15 Replies

@Golden paper wasp currently my code ts const getAllCourses = nextCache( async (userId: string, search?: string) => { return await db.query.... }, [`get-all-courses`], { revalidate: 7200, tags: ["get-all-courses"] } ); Firstly it identifies the dynamic param coz it is not sending same response to different user. So that's good thing But when revalidating it revalidates for every user. So is there any way I could revalidate for particular user like If I could pass userId to the key and tags
yes, you can do that by wrapping your nextCache function inside another function and the wrapper function takes the variables and pass them to the tags and keys. An code example would look like this:

const getAllCourses2 = async (userId: string, search?: string) => {
    return nextCache(
        async () => {
            return await db.query()
        },
        [`get-all-courses-${userId}`],
        { revalidate: 7200, tags: [`get-all-courses-${userId}`] }
    );
}
Answer
oh sorry, you need to change your part:
return await db.query()

of course to the part on how to get data
@B33fb0n3 oh sorry, you need to change your part: tsx return await db.query() of course to the part on how to get data
Golden paper waspOP
Sorry but do I need to change above code

const getAllCourses2 = async (userId: string, search?: string) => {
    return nextCache(
        async () => {
            return await db.query.course...db_logic
        },
        [`get-all-courses-${userId}`],
        { revalidate: 7200, tags: [`get-all-courses-${userId}`] }
    );
}


then how to get data like
const memberCourses= getAllCourses2(userId)

on logging I am not getting accurate result
you use it excatly the same as before. The new function just wraps it. The result of the function is still your code (db.query. ...)
@B33fb0n3 you use it excatly the same as before. The *new* function just wraps it. The result of the function is still your code (db.query. ...)
Golden paper waspOP
Okay now I get it.
I have to do this

const fetchMemberCourses = await getAllCourses(currentUser.userId, search);
  const memberCourses = await fetchMemberCourses();
@B33fb0n3 looks good for me (if you rewrote the name of the function) 👍
Golden paper waspOP
Yes I did. Thanks for help
@Golden paper wasp Yes I did. Thanks for help
sure thing. Please mark solution
Golden paper waspOP
Uh oh unfortunately this breaks the behaviour of unstable_cache

Like previously it was managing the search tag separately but now even after search tag is gone it's returning the result with the search tag
@B33fb0n3
Golden paper waspOP
Caching behaviour get broken here while implementing this
Mossyrose gall wasp
The cache key is a static string ("get-all-courses"). If the cache should be invalidated based on the userId or search parameters, the key should be dynamic.
const getAllCourses = nextCache(
async (userId: string, search?: string) => {
return await db.query....
},
[get-all-courses-${userId}],
{ revalidate: 7200, tags: [get-all-courses-${userId}]}
);
@Mossyrose gall wasp const getAllCourses = nextCache( async (userId: string, search?: string) => { return await db.query.... }, [`get-all-courses-${userId}`], { revalidate: 7200, tags: [`get-all-courses-${userId}`]} );
Golden paper waspOP
Yes I tried this and this fixes it. Just it messes up with search param which it was automatically handling before.