caching sdk's
Unanswered
British Shorthair posted this in #help-forum
British ShorthairOP
I am using the supabase sdk. However it is not caching the results. In the terminal I see:
'10ms (cache: SKIP)
│ │ │ Cache missed reason: (auto no cache)'
How can I still cache the results? I found a possible solution usint the unstable_cache from "next/cache". However, I hope there is a solution to configure this on a global scale?
'10ms (cache: SKIP)
│ │ │ Cache missed reason: (auto no cache)'
How can I still cache the results? I found a possible solution usint the unstable_cache from "next/cache". However, I hope there is a solution to configure this on a global scale?
5 Replies
that is the solution as its otherwise impossible to know what to cache (the reason it shows that is because supabase sdk uses
fetch and that can be cached, but the sdk manually disabled it)... id say make a proxy function that wrapps the unstable_cache and database req and then use that :)British ShorthairOP
Thanks @riský do you have an example how you did the procy function?
like you can be fancy and it caches the response based on output: https://github.com/RiskyMH/EmailThing/blob/main/app/(email)/mail/%5Bmailbox%5D/tools.ts#L9-L35 (this also uses react cache to debounce + has tags to allow for you to revalidate it)
British ShorthairOP
@riský thanks, I found another way to implement this on a global scope. I have used the custom-fetch approach when initializing the supabase client.
export const createFetch =
(options: Pick<RequestInit, "next" | "cache">) =>
(url: RequestInfo | URL, init?: RequestInit) => {
return fetch(url, {
...init,
...options,
});
}; export const createClient = () => {
const cookieStore = cookies();
return createServerClient<Database>(
// Pass Supabase URL and anonymous key from the environment to the client
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
// Define a cookies object with methods for interacting with the cookie store and pass it to the client
{
global: { fetch: createFetch({ cache: "force-cache" }) },
cookies: {
// The get method is used to retrieve a cookie by its name
get(name: string) {
return cookieStore.get(name)?.value;
},
// The set method is used to set a cookie with a given name, value, and options
set(name: string, value: string, options: CookieOptions) {
try {
cookieStore.set({ name, value, ...options });
} catch (error) {
// If the set method is called from a Server Component, an error may occur
// This can be ignored if there is middleware refreshing user sessions
}
},
// The remove method is used to delete a cookie by its name
remove(name: string, options: CookieOptions) {
try {
cookieStore.set({ name, value: "", ...options });
} catch (error) {
// If the remove method is called from a Server Component, an error may occur
// This can be ignored if there is middleware refreshing user sessions
}
},
},
},
);
};