Problem with Sanity and Next.js
Unanswered
Rhinelander posted this in #help-forum
RhinelanderOP
I created Sanity project it works great when I fetch but it is not updating in real time is there a thing with Next like special caching stuff. When I useCdn it changes but it is not up to date even if I refresh 10 times. Here is the code:
import { createClient, groq } from "next-sanity";
export async function getPosts() {
const client = createClient({
projectId: "MY ID",
dataset: "production",
apiVersion: "2023-09-15",
useCdn: false,
});
return client.fetch(groq
}
import { createClient, groq } from "next-sanity";
export async function getPosts() {
const client = createClient({
projectId: "MY ID",
dataset: "production",
apiVersion: "2023-09-15",
useCdn: false,
});
return client.fetch(groq
*[_type == "post"]{
_id,
_createdAt,
title,
"slug": slug.current,
publishedAt,
author,
"mainImage": mainImage.asset->url,
body
});}
4 Replies
Cape lion
I had similar problem. Adding cache setting helped.
here is an example:
here is an example:
export async function getRatings(): Promise<Array<Rating>> {
const ratings: Array<Rating> = await sanityClient.fetch(
groq`*[_type == "rating"]{
_id,
rating,
review,
"wine" : wine->slug.current,
}`,
{
cache: 'no-store',
}
);
return ratings;
}also adding this to your page.tsx should prevent from showing the user cached data
export const dynamic = 'force-dynamic';Jersey Wooly
At this moment, there's no good way to set up query caching. Sanity team is currently looking into this with Vercel/Next team. Until it's fixed, you can't properly cache & revalidate data.
Workaround:
Workaround:
export const revalidate = 0 to the pages you want to keep updated, tag your fetch calls, and use revalidateTag() in an API route handler. Sanity team were super heplful, so you can probably also reach out to them here https://github.com/sanity-io/next-sanity/issues/639 and if you have some time, you can help raise the awareness of the issue here https://github.com/vercel/next.js/issues/55960Giant panda
Yeah you need to revalidate and maybe you also need to create web hook for your app first off all you should create in sanity panel in web hook and copy the given URL then go to vercel app then create web hook and paste url probably this is gonna fix your problem