内容在重新部署Next.js 13和Sanity之前不会更新。

huangapple go评论67阅读模式
英文:

Content is not updating until re-deploy nextjs 13 and sanity

问题

Hello,我正在使用 Next.js 13 服务器组件,使用/app目录和Sanity Studio。我的代码看起来像这样:

const Page = async () => {
  const query = groq`*[_type == "university"]`;

  const data = await sanityClient.fetch(query);
}

每当我在Vercel重新部署我的网站时,前端加载最新更新的数据。但如果我在Sanity Studio上做了一些更改并刷新页面,我只能看到旧的更改。这里有什么帮助吗?谢谢。

英文:

Hello i am using nextjs 13 server components using /app directory and sanity studio. my code looks like this

const Page = async () => {
  const query = groq`*[_type == "university"]`;

  const data = await sanityClient.fetch(query);
}

every time i re-deploy my site on vercel, my content loads latest updated data on the frontend.
but if i change something on sanity studio and refresh my page, i only see the old changes.
any help here ? thank you

答案1

得分: 5

Seems like you are using Next.js 13 app directory. You may try this out

const Page = async () => {
  const revalidate = 60 // 时间间隔
  const query = groq`*[_type == "university"]`;

  const data = await sanityClient.fetch(query, { next: { revalidate } });
}
英文:

Seems like you are using Next js 13 app directory. You may try this out

const Page = async () => {
  const revalidate = 60 //Time interval
  const query = groq`*[_type == "university"]`;

  const data = await sanityClient.fetch(query, {next: {revalidate}});
}

答案2

得分: 2

In nextjs 13的应用程序目录中,您可以像这样声明一个revalidate变量:

// page.tsx

export const Page = async () => {
  const query = groq`*[_type == "university"]`;

  const data = await sanityClient.fetch(query);
}

export const revalidate = 60; // 每60秒重新验证此页面

这会导致您的页面在60秒后变得无效。如果您在60秒后尝试打开页面,页面的缓存将被清除并进行更新。

如果您不希望页面完全静态,请在page.tsx的第一行插入'use client'

// page.tsx
'use client';

export const Page = async () => {
  const query = groq`*[_type == "university"]`;

  const data = await sanityClient.fetch(query);
}
英文:

In nextjs 13's app directory you are able to declare a revalidate variable like this:

// page.tsx

export const Page = async () => {
  const query = groq`*[_type == "university"]`;

  const data = await sanityClient.fetch(query);
}

export const revalidate = 60; // revalidate this page every 60 seconds

This causes your page to get invalid after 60 seconds. If you try to open your page after 60 seconds, the pages cache gets cleared and it will be updated.

If you don't want your page to be static at all, insert a 'use client' in the first line of your page.tsx;:

// page.tsx
'use client';

export const Page = async () => {
  const query = groq`*[_type == "university"]`;

  const data = await sanityClient.fetch(query);
}

huangapple
  • 本文由 发表于 2023年2月6日 03:33:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定