admin管理员组

文章数量:1290188

I recently started my new project with nextjs15 + supabase. I found out that from nextjs15 fetch function's default behavior is "no cache" which is different from version 14 when "force-cache" was default behavior.

Than I wondered how it works in my project. Most of my code uses supabase query function which looks like this.

(In server component)

const supabase = await createClient();
const {data, error} = await supabase.from("table_name").select(*);

In this case, is caching is automatically working just like nextjs14 fetch function? If it is not, then how can i apply caching strategy in this supabase project?

I tried writing code

export const revalidate = 60;

at the top of my page.tsx file where it fetches data from supabase, but it seems like it doesn't work because whenever I insert new data to my db, the data is shown immediately(before 1 minute pass).

I recently started my new project with nextjs15 + supabase. I found out that from nextjs15 fetch function's default behavior is "no cache" which is different from version 14 when "force-cache" was default behavior.

Than I wondered how it works in my project. Most of my code uses supabase query function which looks like this.

(In server component)

const supabase = await createClient();
const {data, error} = await supabase.from("table_name").select(*);

In this case, is caching is automatically working just like nextjs14 fetch function? If it is not, then how can i apply caching strategy in this supabase project?

I tried writing code

export const revalidate = 60;

at the top of my page.tsx file where it fetches data from supabase, but it seems like it doesn't work because whenever I insert new data to my db, the data is shown immediately(before 1 minute pass).

Share Improve this question asked Feb 20 at 4:13 Developer DDeveloper D 234 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Most of the data caching strategies in next.js server components are implemented as an extension of the native fetch function. The supabase SDK uses mostly postgres connections to get data so the caching will likely not work out of the box.

A good strategy would be to create an API route where you fetch your data from postgres and call this endpoint in your server component using fetch. Most of the magic happens in headers so this way you would have them implemented automatically.

https://nextjs./docs/app/building-your-application/data-fetching/fetching

Another alternative could be to use the cache function from react that is native to server components (not a next.js specific)

https://react.dev/reference/react/cache

This could wrap your call and provide caching.

本文标签: nextjsDoes caching works in supabasenextjs15Stack Overflow