👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Issue with fetching/caching data from Sanity.io in Next.js app deployed on Vercel.

4 replies
Last updated: Sep 1, 2023
Hey, I’m using next for building an application and I’ve followed the documentation of
"next-sanity": "5.4.6"
for fetching/caching the data from my studio.I want a very basic fetch method since it’s for a very simple website. In development it seems to works fine, but when deployed to
vercel
I have 0 fresh data.
Here is my
/lib/sanity.ts
: (following the official doc )
import { createClient } from "next-sanity";

const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID;
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET;
const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION;

export const client = createClient({
  projectId,
  dataset,
  apiVersion,
  useCdn: true,
  perspective: "published",
});

export async function sanityFetch<QueryResponse>({
  query,
}: {
  query: string;
}): Promise<QueryResponse> {
  return client.fetch<QueryResponse>(query, {
    next: { revalidate: 10 },
  });
} 
Here is how I fetch my metadata inside
/app/layout.tsx
:
export async function generateMetadata(): Promise<Metadata> {
  const layoutMetadata = await sanityFetch<TYPEMetadata>({
    query: META_QUERY,
  });

  const metadata = layoutMetadata.meta;

  return {
    title: metadata.title,
  };
}

META_QUERY
is a valid
groq
querie.
Thanks!
Aug 31, 2023, 5:47 PM
Did you add your env vars to Vercel?
Aug 31, 2023, 9:10 PM
Thanks
user M
for the answer.Yes! everything fine this side
Aug 31, 2023, 9:32 PM
user M
It’s seems to works fine today, very very strange because I only did change and push UI since yesterday 😵‍💫.
Huge thanks for your time
user M
:saluting_face:
Sep 1, 2023, 1:48 PM
Glad it ended up working itself out!
Sep 1, 2023, 3:44 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?