Replies: 1 comment
-
Maybe something like import { createServerClient } from "@supabase/ssr";
import { cookies } from "next/headers";
import { Database } from "./database.types";
import { fetchSupabase } from "./fetch-supabase";
export async function createClient(tags: string[] = []) {
const cookieStore = await cookies();
return createServerClient<Database>(
process.env.NEXT_PUBLIC_SUPABASE_URL,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
{
global: {
fetch: fetchSupabase({
next: {
revalidate: 3,
tags: ["supabase", ...tags],
},
}),
},
cookies: {
getAll() {
return cookieStore.getAll();
},
setAll(cookiesToSet) {
try {
cookiesToSet.forEach(({ name, value, options }) =>
cookieStore.set(name, value, options),
);
} catch {
// The `setAll` method was called from a Server Component.
// This can be ignored if you have middleware refreshing
// user sessions.
}
},
},
},
);
} export const fetchSupabase =
(options: Pick<RequestInit, "next" | "cache">) =>
(url: RequestInfo | URL, init?: RequestInit) => {
return fetch(url, {
...init,
...options,
});
}; Then you could use it while fetching const businesses = await findAllBusinesses(
await createClient(['businesses'])
);
const businessProjects = await findAllBusinessProjects(
await createClient(['business_projects'])
); And in your actions : export const createBusinessProjectAction = async (
data: CreateBusinessProjectFormValues,
) => {
const client = await createClient();
insertBusinessProject(client, {
data: {
name: data.name,
business_id: data.businessId,
description: data.description,
},
});
revalidateTag("business-projects");
}; I hope it helps |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is there any way to place cache tags to supabase requests, without using REST?
in next you can provide {next: {tags: ['tag']}} to cache tag which i can invalidate in future. Is there any way to do the same for supabase client requests?
Beta Was this translation helpful? Give feedback.
All reactions