Next.js Discord

Discord Forum

unstable_cache

Unanswered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
Hi, how do i convert this to unstable _cache format. thanks

export async function getMembers(): Promise<ProfileReadResponseType> {
  const supabase = createClient();

  const { data, error } = await supabase
    .from("profiles")
    .select(
      `id,firstname,lastname,middlename,email,phone_number,role_name,email,status,created_at`
    );

  if (error) {
    console.error("Error fetching members:", error);
    return { data: null, error: error.message };
  }

  return { data, error: null };
}

29 Replies

Giant pandaOP
can you rewrite this pls
@Giant panda can you rewrite this pls
The cache stores the same data in the same format each time. Therefore, when you fetch data for the first time, it will be identical to what you would retrieve from the cache later.
Giant pandaOP
Oh I mean can you write it using unstable_cache. I'm not getting the results, I think I'm writing it through wrong way
@Giant panda Oh I mean can you write it using unstable_cache. I'm not getting the results, I think I'm writing it through wrong way
oh now I got you. It can look like this (just an example):
const getCachedMembers = unstable_cache(
  async () => {
      
      return getMembers();

    },
    ["some-unique-key"],
    {
      tags: ["cached-memebers-tag"],
      revalidate: 60,
    }
);
@Giant pandasolved?
Giant pandaOP
nah
it appears that you can not call cookies inside of a cache, its not permitted
its the way supabase configures its createClient for both server and client
it would have been the best way to cache data when working with supabase
@Giant panda it would have been the best way to cache data when working with supabase
the cache only handled the saving (caching) of the data that is returned by your function. The stuff that happens inside your function getMembers() is independend from your cache.
@Giant panda it appears that you can not call cookies inside of a cache, its not permitted
why do you think so? Errors? Unexpected Behavior? ... what happend?
Giant pandaOP
thats the expected behaviour right
but inside of unstable_cache, you can that function
by calling that function, it means you can access cookies and thats not permitted
@Giant panda by calling that function, it means you can access cookies and thats not permitted
what happend so you think that you cant access cookies with cache?
Giant pandaOP
unstable_cache does not allow accessing dynamic data sources like cookies or headers directly within the cached function. Next.js requires that you access such dynamic data outside the cached function and pass the relevant values as arguments to the cached function.
unstable_cache does not allow accessing dynamic data sources like cookies or headers directly within the cached function
Why do you think thats the case? Can you please answer that question.
Giant pandaOP
i think its because the purpose of caching is to store static data not dynamic data as i said above, that can be the only thing i can think for now
which kinda make sense
then you can modifiy your method like:
const getCachedMembers = () => {
  const cookieStore = cookies();
  const dynamicValue = cookieStore.get("somedynamic").value;

  return unstable_cache(
    async () => {
        
        return getMembers(dynamicValue);
  
      },
      ["some-unique-key-${dynamicValue}"],
      {
        tags: ["cached-memebers-tag-${dynamicValue}"],
        revalidate: 60,
      }
  );
}
Giant pandaOP
i will check it out thanks a bunch!
Argentine hake
Assuming you keep the supabase client on the server, you can also use a service_role key when you create your supabase client. That client is not tied to a user and doesn’t need to access cookies. https://supabase.com/docs/guides/api/api-keys
Giant pandaOP
nah
i am building a role based access application
if i use service role key everywhere, it bypasses all kinds of authorization
i only use the service role when i am creating a user or i dont care about who is doing what and when
@Giant panda i only use the service role when i am creating a user or i dont care about who is doing what and when
then you might want to check the auth first and then save the data inside the cache under a unique key like I showed you here: https://nextjs-forum.com/post/1275815367499059322#message-1277172300487200789
@Giant panda solved?